0% found this document useful (0 votes)
43 views13 pages

For Loop & Do-While Loop

For loops allow programmers to repeatedly execute a block of code. They contain three expressions: (1) initialization, which runs once before the loop, (2) test condition, which is checked each iteration to determine if the loop should continue, and (3) update, which runs after each iteration. Do-while loops are similar but check the test condition at the end of the loop body, ensuring it runs at least once. Examples demonstrate printing "hello" multiple times using for and do-while loops with different initialization, test, and update expressions.

Uploaded by

Tonmoy
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)
43 views13 pages

For Loop & Do-While Loop

For loops allow programmers to repeatedly execute a block of code. They contain three expressions: (1) initialization, which runs once before the loop, (2) test condition, which is checked each iteration to determine if the loop should continue, and (3) update, which runs after each iteration. Do-while loops are similar but check the test condition at the end of the loop body, ensuring it runs at least once. Examples demonstrate printing "hello" multiple times using for and do-while loops with different initialization, test, and update expressions.

Uploaded by

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

Lecture 7

For Loop & Do-While Loop


Loops

In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is reached.

There are mainly two types of loops:

1. Entry Controlled loops: In this type of loops the test condition is tested before entering the loop body. For
Loop and While Loop are entry controlled loops.
2. Exit Controlled Loops: In this type of loops the test condition is tested or evaluated at the end of 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

for (initialization expr; test expr; update expr)

// body of the loop

// statements we want to execute

}
For Loop Continued.

In for loop, a loop variable is used to control the loop. First initialize this loop variable to some value, then check
whether this variable is less than or greater than counter value. If statement is true, then loop body is executed and
loop variable gets updated . Steps are repeated till exit condition meets.
1. Initialization Expression: In this expression we have to initialize the loop counter to some value. for
example: int i=1;
2. Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we
will execute the body of loop and go to update expression otherwise we will exit from the for loop. For
example: i <= 10;
3. Update Expression: After executing loop body this expression increments/decrements the loop variable by
some value. for example: i++;
For Loop Flow Chart
For Loop Example

// C++ program to illustrate for loop


#include <iostream>
using namespace std;
int main(){
for (int i = 1; i <= 10; i++){
cout << "Hello World\n";
}
return 0;
}
For Loop Example Continued.

for (int x = 0; x <20; x+=2)

cout<< “hello”<<endl;

for (int x = 0; x <20; x=x+2)

cout<< “hello”<<endl;
For Loop Example Continued.

for (int x = 5; x >0; --x)

cout<< “hello”<<endl;

for (int x = 20; x >0; x-=2)

cout<< “hello”<<endl;
For Loop Example Continued.

for (int x = 20; x > 0; x = x-2)

cout<< “hello”<<endl;

for (int x = 0; x < 100; ++x / x++)

cout<< “hello”<<endl;
Nested For Loop Example

int X = 2; int Y = 3;

for (int i = 0; i < X; ++i){

for( int j = 0; j < Y; ++j ){

cout<< j<< “ ”;

}
Do-While Loop

In do while loops also the loop execution is terminated on the basis of test condition. The main difference between
do while loop and while loop is in do while loop the condition is tested at the end of loop body. In do while loop the
loop body will execute at least once irrespective of test condition.

do{
// statements
update_expression;
} while (test_expression);
Do-While Loop Flow Chart
Do-While Example

// C++ program to illustrate 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
return 0;
}

You might also like