WHAT ARE LOOPS
Loops are basically used when there is a need to perform some operation
more than once or n number of times.
Loops come into use when we need to repeatedly execute a block of
statements.
ENTRY CONTROLLED EXIT CONTROLLED
Do-While
For While
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.
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.
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
}
Example:- for(int i=0; i<n; i++)
Initialization Expression:
In this expression, we have to initialize the
loop counter to some value. for example: int
i=1;
Test Expression:
In this expression, we have to test the condition. If
the condition evaluates to true then we will execute the
body of the loop and go to update expression
otherwise we will exit from the for a loop. For example: i
<= 10;
Update Expression:
After executing the loop body this expression
increments/decrements the loop variable by
some value. for example: i++;
Start
Initialization
expression
Test
Condition
Update
Expression
Blocks of
statements
Stop
False
True
FlowDiagramof Forloop
// C program to illustrate for loop
#include <stdio.h>
int main()
{
int i=0;
for (i = 1; i <= 10; i++)
{
printf( "Hello Worldn");
}
return 0;
}
A program using for loop
WHILE LOOP:-
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.
Syntax:-
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
Example:- while(i<9)
{
……
i++;
}
Start
Test
Condition
Update
Expression
While
loop end
False
True
FlowDiagramof While loop
// C program to illustrate while loop
#include <stdio.h>
int main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6)
{
printf( "Hello Worldn");
// update expression
i++;
}
return 0;
}
A program using while loop
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.
Syntax:-
initialization expression;
do
{
// statements update_expression;
}
while (test_expression);
Start
Test
Condition
Update
Expression
While
loop end
False
True
FlowDiagramof DO While loop
// C program to illustrate do-while loop
#include <stdio.h>
int main()
{
int i = 2; // Initialization expression
do
{
// loop body
printf( "Hello Worldn");
// update expression
i++;
} while (i < 1); // test expression
return 0;
}
A program using DO while loop
DIFFERENCE BETWEEN FOR, WHILE, AND DO-WHILE
FOR LOOP
• If the condition is not
true at first time than
control will never
enter in a loop
• Initialization and
updating is a part of
syntax.
WHILE LOOP
• If the condition is not
true at first time than
control will never
enter in a loop
• Initialization and
updating is not a part
of syntax.
DO-WHILE LOOP
• Even if the condition is
not true at first time
the control will enter
in a loop
• Initialization and
updating is not a part
of syntax.
Nowadays C++ is used in many fields like gaming ,
development, Analytics etc. For Competitive
Programming also C++ is the best choice other than
any language because of its STL Libraries.
If you also wants to learn more concepts of C++
then you can enroll in C++ Course at CETPA Infotech,
Noida
Query@cetpainfotech.com www.cetpainfotech.com +91-9911417779

Loops In C++

  • 2.
    WHAT ARE LOOPS Loopsare basically used when there is a need to perform some operation more than once or n number of times. Loops come into use when we need to repeatedly execute a block of statements.
  • 3.
    ENTRY CONTROLLED EXITCONTROLLED Do-While For While
  • 4.
    ENTRY CONTROLLED LOOPS Inthis type of loop, the test condition is tested before entering the loop body. For Loop and While Loop is entry-controlled loops. 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.
  • 5.
    FOR LOOP:- A forloop 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 } Example:- for(int i=0; i<n; i++)
  • 6.
    Initialization Expression: In thisexpression, we have to initialize the loop counter to some value. for example: int i=1; Test Expression: In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression otherwise we will exit from the for a loop. For example: i <= 10; Update Expression: After executing the loop body this expression increments/decrements the loop variable by some value. for example: i++; Start Initialization expression Test Condition Update Expression Blocks of statements Stop False True FlowDiagramof Forloop
  • 7.
    // C programto illustrate for loop #include <stdio.h> int main() { int i=0; for (i = 1; i <= 10; i++) { printf( "Hello Worldn"); } return 0; } A program using for loop
  • 8.
    WHILE LOOP:- while loopsare 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. Syntax:- initialization expression; while (test_expression) { // statements update_expression; } Example:- while(i<9) { …… i++; }
  • 9.
  • 10.
    // C programto illustrate while loop #include <stdio.h> int main() { // initialization expression int i = 1; // test expression while (i < 6) { printf( "Hello Worldn"); // update expression i++; } return 0; } A program using while loop
  • 11.
    DO WHILE LOOP:- Indo-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. Syntax:- initialization expression; do { // statements update_expression; } while (test_expression);
  • 12.
  • 13.
    // C programto illustrate do-while loop #include <stdio.h> int main() { int i = 2; // Initialization expression do { // loop body printf( "Hello Worldn"); // update expression i++; } while (i < 1); // test expression return 0; } A program using DO while loop
  • 14.
    DIFFERENCE BETWEEN FOR,WHILE, AND DO-WHILE FOR LOOP • If the condition is not true at first time than control will never enter in a loop • Initialization and updating is a part of syntax. WHILE LOOP • If the condition is not true at first time than control will never enter in a loop • Initialization and updating is not a part of syntax. DO-WHILE LOOP • Even if the condition is not true at first time the control will enter in a loop • Initialization and updating is not a part of syntax.
  • 15.
    Nowadays C++ isused in many fields like gaming , development, Analytics etc. For Competitive Programming also C++ is the best choice other than any language because of its STL Libraries. If you also wants to learn more concepts of C++ then you can enroll in C++ Course at CETPA Infotech, Noida
  • 16.