do while statement in c

Preview
You must first complete for statement in c language | Nesting of for in c language before viewing this Lesson



do while statement in C

do while statement is an exit controlled looping statement. initialization, condition and updation are performed in separate lines. It executes at least once.

The syntax of do while statement is:

Initialize;
do
{
Statements;
update;
}
while(Condition);

do is a keyword which specifies that do while statement has started

Initialize:  In this part, we assign some value to the control variable which controls number of times the loop should execute.

Example

counter=1;

Condition: It is a relational or logical expression to verify whether the for statement should continue or not.

Example

counter<=5;

Update: Increasing or decreasing the value of control variable comes under this part.

Example

counter++

Statements are set of instructions which will repeatedly execute as long as condition evaluates to true.

while is a keyword to specify that do while has ended. It should  be followed by a semicolon.



 Program
#include<stdio.h>
int main()
{
int k;
k=1;
do
{
printf(“\n%d”,k);
k++;
}while(k<=4);
return(0);
}
Output
1
2
3
4

Description

In the program, initially loop control variable k is assigned value 1.

Program enters the loop from keyword do.

Then printf(“\n%d”,k); will execute followed by statement k++.

k  is then compared to 4 . It is less than 4, so program control jumps to keyword do.

This process repeatedly executes as long as condition part is true.



 

Best Books of Computer Science





Lesson tags: difference between for, difference between for and while in c, difference between while and do while in c, do while statement in c, while and do while in c
Back to: C Programming Language