In a programming language, a loop is useful for running iterative tasks. Using a loop a program can execute code block for multiple times as required.
Mainly there are 3 types of loops available in C programming language. Where while loop is one of them. Below is the list if loop available.
- for loop
- while loop
- do-while loop
do while Loop
A do while loop is similar to while loop. It is exit restricted loops. It means the condition is checked at end of the loop. The do while loop must be run for once.
// code block
do while Loop Working
- Step 1 – Loop start
- Step 2 – Code block is executed.
- Step 3 – The conditions are checked. If the condition evaluates to false go to step 4. If the condition evaluates to true then go to step 2.
- Step 4 – Exit loop
do while Loop Example
A sample C programme to calculate the sum of user input integers. The program will exit when the user enters 0.
// C Program to add integers until user enters 0
int num, sum = 0;
printf("Enter a number: ");
sum += num;
}while(num != 0);
printf("Total = %d",sum);
Enter a number: 5 Enter a number: 18 Enter a number: 1 Enter a number: 0 Total = 24