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);
//code statements…;
}
while (condition);
Where,
Name | Description |
---|---|
condition | It 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++
// Program for illustrating do...while loop in C++ #include <iostream> using namespace std; int main() { int k = 10; do{ cout<<k<<"\n"; k = k + 10; } while (k <= 100) ; }
The output should be:
10
20
30
40
50
60
70
80
90
100
20
30
40
50
60
70
80
90
100
2. Infinitive do…while Loop in C++
// Program for illustrating for loop in C++ #include <iostream> using namespace std; int main() { do{ cout<<"This is infinitive do-while Loop, Press Ctrl+c to STOP"; } while(true); }
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
……..
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
……..
LATEST POSTS
-
numpy.ndarray.flatten() in Python
-
numpy.median() in Python
-
How to Upgrade PIP in Windows
-
numpy.arange() in Python
-
Binary Search in Java
-
Java Math abs() method with examples
-
C++ do-while loop with Example
-
C++ while loop with Examples
-
C strcmp() function with example
-
How to Print Without Newline in Python
-
numpy.dtype() in Python
-
numpy.dot() in Python