top of page
Search

Do-while Loop Explained with Syntax and Examples of C programming



In general, statements in a program are executed sequentially. The first statement is executed first, followed by the second, and so on. But in some situations, it is required to execute a block of code over and over again. Loops are used to execute a block of statements a specified number of times until a condition is met.

C supports the following types of loops:


DO-WHILE LOOP:

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.

A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time since the loop is executed once before the condition is tested.

Syntax:

do

{

statement(s);

} while (condition);


NOTE:

In do…while loop, while should be terminated using;


Examples:

To print all numbers from 50 to 30.

# include<stdio.h>

int main()

{

int n; n=50;

do

{

printf("\n%d",n);

n--;

}while (n>=30);

return 0;

}




29 views
bottom of page