The do-while loop is used to repeat a block of code while
a condition is true. The important feature of a do-while loop
is that the loop body executes at least once, because the
condition is checked after the loop body.
do-while loop, the statements are
executed first and the condition is checked afterward.
A do-while loop executes a block of statements and then checks
whether the condition is true.
do
{
// statements
}
while (condition);
If the condition is true, the loop repeats.
do
{
statement;
}
while (condition);
Notice the semicolon ; after the while condition.
It is required in a do-while loop.
int i = 1;
do
{
printf("%d\n", i);
i++;
}
while (i <= 5);
Output:
1
2
3
4
5
The execution order of a do-while loop is:
The main difference between while and do-while is
that the do-while loop executes its body before checking the condition.
int i = 10;
do
{
printf("Hello");
}
while (i < 5);
Even though i < 5 is false, Hello is printed once.
| while Loop | do-while Loop |
|---|---|
| Condition is checked first. | Condition is checked after the body. |
| May execute zero times. | Executes at least once. |
| No semicolon after the condition. | Semicolon is required after the condition. |
int i = 1;
do
{
printf("%d ", i);
i++;
}
while (i <= 5);
Output:
1 2 3 4 5
int i = 5;
do
{
printf("%d ", i);
i--;
}
while (i >= 1);
Output:
5 4 3 2 1
int i = 2;
do
{
printf("%d ", i);
i += 2;
}
while (i <= 10);
Output:
2 4 6 8 10
int i = 1;
do
{
printf("%d ", i);
i += 2;
}
while (i <= 10);
Output:
1 3 5 7 9
int i = 1;
int sum = 0;
do
{
sum = sum + i;
i++;
}
while (i <= 5);
printf("Sum = %d", sum);
Output:
Sum = 15
int number = 5;
int i = 1;
do
{
printf("%d x %d = %d\n",
number, i, number * i);
i++;
}
while (i <= 10);
This program prints the multiplication table of 5.
int number = 5;
int factorial = 1;
int i = 1;
do
{
factorial = factorial * i;
i++;
}
while (i <= number);
printf("Factorial = %d", factorial);
Output:
Factorial = 120
#include <stdio.h>
int main()
{
int number;
do
{
printf("Enter a positive number: ");
scanf("%d", &number);
}
while (number <= 0);
printf("You entered: %d", number);
return 0;
}
The program keeps asking for input until the user enters a positive number.
int choice;
do
{
printf("\n1. Add");
printf("\n2. Subtract");
printf("\n3. Exit");
printf("\nEnter choice: ");
scanf("%d", &choice);
}
while (choice != 3);
A do-while loop is useful for menus because the menu should normally be displayed at least once.
int i = 1;
do
{
if (i % 2 == 0)
{
printf("%d ", i);
}
i++;
}
while (i <= 10);
The if statement checks whether each number is even.
char choice;
do
{
printf("Hello\n");
printf("Continue? (y/n): ");
scanf(" %c", &choice);
}
while (choice == 'y');
The loop repeats when the user enters y.
int number;
do
{
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered %d\n", number);
}
while (number != 0);
The program continues until the user enters 0.
int i = 1;
do
{
printf("Number = %d\n", i);
printf("Welcome to C programming\n");
i++;
}
while (i <= 3);
All statements inside the braces execute during every iteration.
int count = 5;
do
{
printf("%d\n", count);
count--;
}
while (count > 0);
printf("Start!");
The loop prints a countdown from 5 to 1.
int number;
do
{
printf("Enter number between 1 and 10: ");
scanf("%d", &number);
}
while (number < 1 || number > 10);
The loop continues while the entered number is outside the valid range.
The semicolon after the while condition is required.
do
{
printf("Hello");
}
while (condition);
Without the semicolon, the program will produce a syntax error.
int i = 10;
do
{
printf("%d", i);
i++;
}
while (i < 5);
Output:
10
The condition is false initially, but the body still executes once.
The loop variable should normally change so that the condition can eventually become false.
int i = 1;
do
{
printf("%d ", i);
}
while (i <= 5);
Here, i never changes, so the condition remains true and the
loop continues indefinitely.
Correct:
int i = 1;
do
{
printf("%d ", i);
i++;
}
while (i <= 5);
The update should move the variable toward the condition becoming false.
int i = 1;
do
{
printf("%d ", i);
i--;
}
while (i <= 5);
Here, i becomes smaller while the condition
i <= 5 remains true, so the loop does not terminate normally.
| for Loop | do-while Loop |
|---|---|
| Initialization, condition, and update are commonly written together. | Initialization and update are usually written separately. |
| Condition is checked before the body. | Condition is checked after the body. |
| Can execute zero times. | Executes at least once. |
#include <stdio.h>
int main()
{
int age;
do
{
printf("Enter your age between 1 and 100: ");
scanf("%d", &age);
}
while (age < 1 || age > 100);
printf("Valid age: %d", age);
return 0;
}
This program repeatedly asks for an age until the user enters a value between 1 and 100.
#include <stdio.h>
int main()
{
int choice;
do
{
printf("\n--- MENU ---\n");
printf("1. Say Hello\n");
printf("2. Show Message\n");
printf("3. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Hello!\n");
}
else if (choice == 2)
{
printf("Welcome to C programming!\n");
}
else if (choice != 3)
{
printf("Invalid choice.\n");
}
}
while (choice != 3);
printf("Program ended.");
return 0;
}
#include <stdio.h>
int main()
{
int n;
int i = 1;
int sum = 0;
printf("Enter a positive number: ");
scanf("%d", &n);
if (n > 0)
{
do
{
sum += i;
i++;
}
while (i <= n);
printf("Sum = %d", sum);
}
else
{
printf("Please enter a positive number.");
}
return 0;
}
Practice the following programs:
y/n.Practice these programs to understand why a do-while loop is useful when the statements should execute at least once.
do-while loop executes the body before checking the condition.while condition.break statement.Question: What is the main feature of a do-while loop?