Do while loop in c++ with example

Do While loop is another loop in c++

Syntax:-

do
{
Body of the loop
}
while(condition);
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not needed.
# Body of the loop may have one or more statement.
# Semicolon is present after while.
# At least once body of loop is executed even condition is false.

Execution:-

In do while loop 1st body of the loop is executed than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

Hence body is 1st executed before the condition check so body of loop is always executed at least once.

Example:-

Program to count N numbers



Program to Display even number up to 20



Example:-

To count N number to show condition is checked at bottom


Output:-
Enter a number 4
1
2
3
4
5

Here 5 also displayed even we put condition (num =< i)because condition is checked at bottom of loop.

Previous Code:-
While loop

List Code:-
C++ Codes

Next Code:-
Nested loop

C++ Programs
List of c++ Programs

2 Comments

  1. A very nice and simple tutorial about do while loop in C++

    ReplyDelete
  2. great tutorial for beginners, Here is another one in detail that explains how the do while loop in c++ executes with a simple program example. have a look: http://www.byte-notes.com/do-while-loop-c

    ReplyDelete

Add your comments here

Back to Top