do while statement in c

Preview
You must first complete while statement in c language before viewing this Lesson

do while statement in C

It is an exit controlled looping statement. It is basically used when we want that loop should execute at least once. In do while statement initialization, condition and updation are performed in separate lines. The syntax of do while statement is:

initialization;
do
{
Statement-Block;
updation;
} while(Condition);
Statement-X;

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

Initialization is the way to store some initial value in the control variable. Control variable is the variable which is used to control how many times loop should execute.

Control variable can of any data type like int, float, char etc. In this part, control variable is initialized with some value using assignment operator.

It is basically used to specify the value from which the loop should start. In this part, we can also initialize more than one variable depending upon the requirement.

Example

i=1,j=2;

If more than one variable have to be initialized, they should be separated by commas.

Condition refers to any relational or logical expression which is always tested to see whether the loop should continue or not. If condition is true, the loop will execute.

The loop will continuously execute as long as condition is true. Loop will terminate when the condition becomes false.

In condition part, usually control variable is tested to check whether the loop should continue or not. 

Condition is always checked after executing the statements within do while statement at least once.

Multiple conditions can appear in this part but they should be combined with the help of logical operators.

Example

i<=10, Age>=0&&Age<=18

Updation is the way to increment or decrement the value of a control variable. Updation can be done by using increment/decrement operator, shorthand operators or arithmetic operators.

Multiple updation statements can appear in this part depending upon the requirement.

Example

i++; count+=3;

If there are more than one updation statements, they should be separated by commas.

Statement Block is the set of instructions which are part of loop and which will execute as a part of for statement. Statement block can contain one or more statements. These instructions will repeatedly execute as long as condition is true.

while is a keyword which specifies that do while statement has ended. The keyword while  must be followed by a semicolon.

Statement-X may be set of one or more statements which will be executed after completion of do while statement.



do while statement can be represented with the help of following flowchart:

 Program to demonstrate the use of do while statement.
#include<stdio.h>
int main()
{
int i,n;
printf(“\nEnter value of n=”);
scanf(“%d”,&n);
i=1;
do
{
printf(“\n%d”,i);
i++;
}while(i<=n);
return(0);
}
Output
1
2
3
4
5

 Description of Above Program

In the above program, initially variable i has  been assigned value 1.

Control of program enters the loop where do starts.

Then printf(“\n%d”,i); would be executed followed by updation statement i++.

Value of i  is then compared with 5 to check whether it is less than 5 or not.

If it is less than five, control of program would transfer to  next iteration of loop i.e. to do statement.

This process is repeated again and again as long as condition is true.


 Difference between for,while and do while statement

for Statementwhile Statement
do while Statement
1. for is an entry controlled looping statement.1. while is an entry controlled looping statement.1. do while is an exit controlled looping statement.

2. The syntax of for statement is:

for(Initialization;Condition;Updation)
{
Statement-Block;
}
Statement-X;

2. The syntax of while statement is:

Initialization;
while(Condition)
{
Statement-Block;
Updation;
}
Statement-X;

2. The syntax of do while statement is:

Initialization;
do
{
Statement-Block;
Updation;
} while(Condition);
Statement-X;

3. Initialization, condition and updation are performed in same line.3. Initialization, condition and updation are performed in separate lines.3. Initialization, condition and updation are performed in separate lines.
4. For statement is used when we know in advance how many times we want the loop to get executed.4. while statement is used when we don’t know in advance how many times the loop would get executed.4. do while statement is used when we want that the loop should get executed at least once.
5. There should not be a semicolon at the end of for statement.5. There should not be a semicolon at the end of while statement.5. There must be a semicolon at the end of do while statement.

6.

#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
printf(“\n%d”,i*i);
return(0);
}

6. 
#include<stdio.h>
int main()
{
int i;
i=1;
while(i<=5)
{
printf(“\n%d”,i);
i++;
}
return(0);
}
6. 
#include<stdio.h>
int main()
{
int i;
do
{
printf(“\n%d”,i);
i++;
}while(i<=5);
return(0);
}




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