Loop Statement in CPP
Loop Statement in CPP
Topperworld.in
Loops Statment
In this type of loop, the test condition is tested before entering the loop
body.
For Loop and While Loop is entry-controlled loops.
In this type of loop the test condition is tested or evaluated at the end of the
loop body.
Therefore, the loop body will execute at least once, irrespective of whether
the test condition is true or false. the do-while loop is exit controlled loop.
©Topperworld
C++ Programming
while loop
1.
– First checks the condition, then executes the body.
for loop
2.
– firstly initializes, then, condition check, execute body, update.
do-while
3. loop
– firstly, execute the body then condition check
For Loop-
©Topperworld
C++ Programming
valid in the scope of the loop. Variable defined before the loop with the
same name are hidden during execution of the loop.
Condition: This statement gets evaluated ahead of each execution of the
loop body, and abort the execution if the given condition get false.
Iteration execution: This statement gets executed after the loop body,
ahead of the next condition evaluated, unless the for loop is aborted in
the body (by break, goto, return or an exception being thrown.)
Flowchart:
©Topperworld
C++ Programming
Example:
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i
<= 5; i++) {
cout <<
"Topper
World\
n";
}
return 0;
Output:
}
Topper World
Topper
World
Topper
World
Topper
World
Topper
World
©Topperworld
C++ Programming
While Loop-
While studying for loop we have seen that the number of iterations
is known beforehand, i.e. the number of times the loop body is needed
to be executed is known to us.
while loops are used in situations where we do not know the exact
number of iterations of the loop beforehand. The loop execution is
terminated on the basis of the test conditions.
We have already stated that a loop mainly consists of three statements
– initialization expression, test expression, and update expression. The
syntax of the three loops – For, while, and do while mainly differs in the
placement of these three statements.
Syntax:
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
Flow Diagram:
©Topperworld
C++ Programming
Example:
// test expression
while (i < 6) {
cout <<
"Hello
World\n";
// update expression
i++;
}
return 0;
}
Output:
Hello World
Hello World
Hello
World
Hello
World
Hello
World
©Topperworld
C++ Programming
Do-while loop
Flow Diagram:
©Topperworld
C++ Programming
Example:
// C++ program to Demonstrate do-while loop
#include <iostream>
using namespace std;
int main()
{
int i = 2; //
Initialization
expression
do {
// loop
body
cout <<
"Hello
World\
n";
// update expression
i++;
Output:
return 0;
}Hello World
©Topperworld
C++ Programming
Infinite Loop
Important Points
Use for a loop when a number of iterations are known beforehand, i.e.
the number of times the loop body is needed to be executed is known.
Use while loops, where an exact number of iterations is not known but
the loop termination condition, is known.
Use do while loop if the code needs to be executed at least once like
in Menu-driven programs
©Topperworld