#loops #whileloops #dowhileloops #loopsinprogramming
@kingoftechniques6864
In computer programming, loops are used to repeat a block of code.
For example, let's say we want to show a message 100 times. Then instead of writing the print statement 100 times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in our programs by making effective use of loops.
There are 3 types of loops in C++.
for loop
while loop
do...while loop
In the previous tutorial, we learned about the C++ for loop. Here, we are going to learn about while and do...while loops.
A while loop evaluates the condition
If the condition evaluates to true, the code inside the while loop is executed.
The condition is evaluated again.
This process continues until the condition is false.
When the condition evaluates to false, the loop terminates.
The do...while loop is a variant of the while loop with one important difference: the body of do...while loop is executed once before the condition is checked.
The body of the loop is executed at first. Then the condition is evaluated.
If the condition evaluates to true, the body of the loop inside the do statement is executed again.
The condition is evaluated once again.
If the condition evaluates to true, the body of the loop inside the do statement is executed again.
This process continues until the condition evaluates to false. Then the loop stops.
Introduction to Do While Loop in C++. Do while loop is a control statement that controls the flow of the program. Unlike for loop and while loop that checks the condition at the top of the loop, do-while loops check the condition at the bottom of the loop.
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.