0% found this document useful (0 votes)
5 views

Loop Statement in CPP

Uploaded by

sanjivsem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Loop Statement in CPP

Uploaded by

sanjivsem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C++ Programming

Topperworld.in

Loops Statment

 Loop statements in C++ execute a certain block of the code or statement


multiple times
 Mainly used to reduce the length of the code by executing the same
function multiple times and reducing the code’s redundancy.

 There are mainly two types of loops:

1. Entry Controlled loops:

In this type of loop, the test condition is tested before entering the loop
body.
For Loop and While Loop is entry-controlled loops.

2. Exit 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

S.No. Loop Type and Description

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-

A For loop is a repetition control structure that allows us to write a loop


that is executed a specific number of times. The loop enables us to
perform n number of steps together in one line.
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}

Explanation of the Syntax:


 Initialization statement: This statement gets executed only once, at the
beginning of the for loop. You can enter a declaration of multiple
variables of one type, such as int x=0, a=1, b=2. These variables are only

©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:

// C++ program to Demonstrate for loop

#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:

// C++ program to Demonstrate while loop


#include <iostream>
using namespace std;
int main()
{
// initialization expression
int i = 1;

// 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

 In Do-while loops also the loop execution is terminated on the basis of


test conditions.
 The main difference between a do-while loop and the while loop is in
the do-while loop the condition is tested at the end of the loop body,
i.e do-while loop is exit controlled whereas the other two loops
are entry-controlled loops.
 Note: In a do-while loop, the loop body will execute at least
once irrespective of the test condition.
Syntax:
initialization expression;
do
{
// statements
update_expression;
} while
(test_expression);

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++;

} while (i < 1); // test expression

Output:
return 0;
}Hello World

©Topperworld
C++ Programming

 Infinite Loop

An infinite loop (sometimes called an endless loop ) is a piece of coding


that lacks a functional exit so that it repeats indefinitely.
An infinite loop occurs when a condition is always evaluated to be true.
Usually, this is an error.

 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

You might also like