0% found this document useful (0 votes)
109 views16 pages

Loops in C++

This PPT contains different types of Loops in C++ and if you want to learn more concepts of C++ then you can take C++ Course in Noida from CETPA Infotech.

Uploaded by

Sakshi Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views16 pages

Loops in C++

This PPT contains different types of Loops in C++ and if you want to learn more concepts of C++ then you can take C++ Course in Noida from CETPA Infotech.

Uploaded by

Sakshi Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

T PA

E
Loops In c++
C
WHAT ARE LOOPS

PA
Loops are basically used when there is a need to perform some operation

T
more than once or n number of times.

E
Loops come into use when we need to repeatedly execute a block of

C
statements.
Types of loops in C++

T PA
E
ENTRY CONTROLLED EXIT CONTROLLED

For

CWhile Do-While
A
ENTRY CONTROLLED LOOPS

P
In this type of loop, the test condition is tested before entering the loop body.

T
 For Loop and While Loop is entry-controlled loops.

C E
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.

do-while loop is exit controlled loop.


A
FOR LOOP:-

P
A for loop is a repetition control structure that allows us to write a loop that is executed a

T
specific number of times. The loop enables us to perform n number of steps together in one
line.

E
Syntax:-
for (initialization expr; test expr; update expr)

C
{
// body of the loop
// statements we want to execute
}

Example:- for(int i=0; i<n; i++)


Initialization Expression: Start
In this expression, we have to initialize the loop

A
counter to some value. for example: int i=1;

P
Initialization
expression
Test Expression:

T
In this expression, we have to test the condition. If the
condition evaluates to true then we will execute the body

E
of the loop and go to update expression otherwise we will Test False
exit from the for a loop. For example: i <= 10; Condition

C
True
Blocks of
Update Expression:
statements
After executing the loop body this expression
increments/decrements the loop variable by some
value. for example: i++; Update Expression

Flow Diagram of For loop Stop


A program using for loop

A
// C program to illustrate for loop
#include <stdio.h>

P
int main()

T
{
int i=0;

E
for (i = 1; i <= 10; i++)

C
{
printf( "Hello World\n");
}

return 0;
}
A
WHILE LOOP:-

P
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.

T
Syntax:-

E
initialization expression;
while (test_expression)

C
{
// statements

update_expression;
}
Example:- while(i<9)
{
……
i++;
}
Start
Flow Diagram of While loop

T PA
E
Test
False
Condition

C
True

While loop
Update Expression end
A program using while loop

A
// C program to illustrate while loop
#include <stdio.h>

P
int main()

T
{
// initialization expression

E
int i = 1;

C
// test expression
while (i < 6)
{
printf( "Hello World\n");

// update expression
i++;
}

return 0;
}
DO WHILE LOOP:-

A
In do-while loops also the loop execution is terminated on the basis of test conditions. The

P
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

T
the other two loops are entry controlled loops.

E
Syntax:-
initialization expression;

C
do
{
// statements update_expression;
}
while (test_expression);
Start
Flow Diagram of DO While loop

T PA
E
Test
False
Condition

C
True

While loop
Update Expression end
A program using DO while loop
// C program to illustrate do-while loop

A
#include <stdio.h>

P
int main()
{

T
int i = 2; // Initialization expression

E
do
{

C
// loop body
printf( "Hello World\n");

// update expression
i++;

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

return 0;
}
A
DIFFERENCE BETWEEN FOR, WHILE, AND DO-WHILE

FOR LOOP

• If the condition is not true

E T P
WHILE LOOP

• If the condition is not true


DO-WHILE LOOP

• Even if the condition is

C
at first time than control at first time than control not true at first time the
will never enter in a loop will never enter in a loop control will enter in a
loop
• Initialization and • Initialization and
updating is a part of updating is not a part of • Initialization and updating
syntax. syntax. is not a part of syntax.
Nowadays C++ is used in many fields like gaming ,

A
development, Analytics etc. For Competitive

P
Programming also C++ is the best choice other than any

T
language because of its STL Libraries.

C E
If you also wants to learn more concepts of C++ then
you can enroll in C++ Course at CETPA Infotech, Noida
T PA
THANK YOU

C
[email protected]
Ewww.cetpainfotech.com +91-9911417779

You might also like