do while statement in c

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



do while statement in C

In do while statement initialization, condition and updation are performed in separate lines. This looping statement executes at least once.

The syntax of do while statement is:

Initialization;
do
{
Statements;
updation;
}
while(Condition);
do is a keyword which specifies that do while statement has started

Initialization is the way to assign some initial value to loop control variable which controls number of times loop should execute.This is the value from where do while loop starts.

Example

i=1;

Condition is a  relational or logical expression used to check whether the loop should continue or not.  The loop will continuously execute as long as condition is true.

Example

i<=10

Updation is to increase or decrease the value of loop control variable. 

Example

i++;

Statements refers to C language instructions which are part of do while statement.

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



 Program of do while statement.
#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.



 

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