C++ do-while loop with Example

The C++ do-while loop is used to execute a specific block of code multiple times. Unlike for loop or while loop the do...while loop checks the condition after executing the block of code. When you need to execute the block code at least once when iterations are not fixed then you need to use do...while loop.

Basic Syntax

Following is the basic syntax for do…while loop in C++:

do {
//code statements…;
}
while (condition);

Where,

NameDescription
conditionIt can be any expression
code statements…A single statement or block of statements to execute

Examples

Following are the examples of do…while loop in C++.

1. Simple do…while loop in C++

The output should be:

10
20
30
40
50
60
70
80
90
100

2. Infinitive do…while Loop in C++

The output should be:

This is infinitive do-while Loop, Press Ctrl+c to STOP
This is infinitive do-while Loop, Press Ctrl+c to STOP
This is infinitive do-while Loop, Press Ctrl+c to STOP
This is infinitive do-while Loop, Press Ctrl+c to STOP
……..

LEAVE A REPLY

Please enter your comment!
Please enter your name here