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.

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

0
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

Enter a number: 5 5 10 15 20 25 30 35 40 45 50

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

Enter a number to generate its multiplication table: 12 12 x 1 = 12 12 x 2 = 24 12 x 3 = 36 12 x 4 = 48 12 x 5 = 60 12 x 6 = 72 12 x 7 = 84 12 x 8 = 96 12 x 9 = 108 12 x 10 = 120

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 text This 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

Counter: 0 Counter: 1 Counter: 2 Counter: 3 Counter: 4 Loop terminated.

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

1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9

Explanation of the code:

  1. Outer Loop (do while (i <= 3)):
    • Manages the number (i) for which the multiplication table is printed.
    • Executes once per number.
  2. 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.
  3. Flow
    • Outer loop runs once.
    • Inner loop completes all iterations for the current i.
    • Outer loop increments i and repeats.