do while statement in C++ | do while loop in C++

You must first complete for statement in C++| Nesting of for statement in C++ | for loop in C++ before viewing this Lesson

do while statement in C++ | do while loop 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 This is the value from where do while statement starts.

Example

i=1;

Condition: It refers to any relational or logical expression used to check whether the loop should continue or not.

Example

i<=5;

Update: It is the way to increase or decrease the value of control variable.

Example

i++

Statements-Block  refers to set of C+++ statements which will repeatedly execute as long as condition is true.

 Program of do while statement in C++

#include<stdio.h>
int main()
{
int k;
k=1;
do
{
  cout<<endl<<k;
  k++;
}while(k<=4);
return(0);
}
Output
1
2
3
4

Description of above program

In the program, control variable k has been strored value 1. Value of k is then compared with 4. It is less than equal to  4, so statement cout<<endl<<i;  gets executed displaying value of k i.e. 1.

Then control goes to updation statement k++which increases the value of k by 1. k becomes 2.

Again k<=4 is tested. which is true so cout<<endl<<k; gets executed.

Again updation k++ is performed followed by condition k<=4. This process goes on as long as condition is true.

Spread the love
Lesson tags: do while loop in C++, do while statement in c, Program of do while statement in C++
Back to: C++ Programming Notes
Spread the love