Do-While Statement
A loop is a programming control structure that allows you to execute a block of code indefinitely if a specific condition is met. Loops are used to execute repeating activities and boost programming performance. There are multiple loops in the C programming language, one of which is the "do-while" loop.
A "do-while" loop is a form of a loop in C that executes the code block first, followed by the condition. If the condition is true, the loop continues to run; else, it stops. However, whether the condition is originally true, it ensures that the code block is performed at least once.
- The do keyword marks the beginning of the Loop.
- The code block within curly braces {} is the body of the loop, which contains the code you want to repeat.
- The while keyword is followed by a condition enclosed in parentheses (). After the code block has been run, this condition is verified. If the condition is true, the loop continues else, the loop ends.
Syntax
do {
// code to be executed
} while (condition);
Example:1
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d n", i);
i++;
} while (i < 5);
return 0;
}
Output
1
2
3
4
2.Program to print table for the given number using do while Loop
#include <stdio.h>
int main(){
int i=1, number=0;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i++;
} while(i<=10);
return 0;
}
Output
3.Program that prints the multiplication table of a given number N using do while Loop.
#include <stdio.h>
int main() {
int N;
printf("Enter a number to generate its multiplication table: ");
scanf("%d", &N);
int i = 1;
do {
printf("%d x %d = %d\n", N, i, N * i);
i++;
} while (i<= 10);
return 0;
}
Output
Infinite Do-While Loop
An infinite do-while loop in C occurs when the loop's condition always evaluates to true. This kind of loop runs continuously until interrupted (e.g., by a break statement, an error, or external intervention).
Syntax
do {
// code to execute repeatedly
} while (1); // Condition is always true
Example 1: Simple Infinite Loop
#include <stdio.h>
int main() {
do {
printf("This will print forever.\n");
} while (1); // Always true
return 0;
}
Output
The textThis will print forever.
will be displayed repeatedly until the program is terminated manually (e.g., by pressing Ctrl+C).
Infinite Do-While Loop with Break Condition
You can use a break statement to exit an infinite loop based on certain logic.
Example:2
#include <stdio.h>
int main() {
int counter = 0;
do {
printf("Counter: %d\n", counter);
counter++;
if (counter >= 5) {
break; // Exit the loop when counter reaches 5
}
} while (1); // Always true
printf("Loop terminated.\n");
return 0;
}
Output
Nested Do-While Loop
A nested do-while loop is when one do-while loop is placed inside another do-while loop. This structure allows for complex iterative logic, such as iterating over multi-dimensional data or implementing nested conditions.In a nested do-while loop, the outer loop contains another do-while loop (inner loop) inside it. Each loop has its own condition.
Syntax
do {
// Outer loop body
do {
// Inner loop body
} while (inner_condition);
} while (outer_condition);
Example 1:Print a multiplication table for numbers 1 to 3.
#include <stdio.h>
int main() {
int i = 1; // Outer loop counter
do {
int j = 1; // Inner loop counter
do {
printf("%d x %d = %d\n", i, j, i * j);
j++; // Increment inner counter
} while (j <= 3); // Inner loop condition
printf("\n"); // Add space between tables
i++; // Increment outer counter
} while (i <= 3); // Outer loop condition
return 0;
}
Output
Explanation of the code:
- Outer Loop
(do while (i <= 3))
: - Manages the number (i) for which the multiplication table is printed.
- Executes once per number.
- Inner Loop
(do while (j <= 3))
: - Handles the multiplication of the current number (i) with numbers 1 to 3.
- Executes for every value of j.
- Flow
- Outer loop runs once.
- Inner loop completes all iterations for the current i.
- Outer loop increments i and repeats.