C - Iteration
C - Iteration
• 1- For statement
• 2- Nested For
• 3- while statement
• 4- Usage of while
• 5- Avoid Infinite loop
• 6- do-while statement
• Loop entry
– The point where flow of control passes to the body
• Loop exit
- The point at which the iteration ends and control
passes to the next statement after the loop
PostExpr
OUTPUT
int num;
OUTPUT
int num;
true
for ( num = 1 ; num <= 3 ; num++ )
OUTPUT
int num;
OUTPUT
Hello 1
int num;
OUTPUT
Hello 1
int num;
true
for ( num = 1 ; num <= 3 ; num++ )
OUTPUT
Hello 1
int num;
OUTPUT
Hello 1
Hello 2
(C) Copyright 2020 by Prof. Abeer ElKorany
num 3 Example of Repetition
int num;
OUTPUT
Hello 1
Hello 2
(C) Copyright 2020 by Prof. Abeer ElKorany
num 3 Example of Repetition
int num;
true
for ( num = 1 ; num <= 3 ; num++ )
OUTPUT
Hello 1
Hello 2
(C) Copyright 2020 by Prof. Abeer ElKorany
num 3 Example of Repetition
int num;
OUTPUT
Hello 1
Hello 2
Hello 3
(C) Copyright 2020 by Prof. Abeer ElKorany
num 4 Example of Repetition
int num;
OUTPUT
Hello 1
Hello 2
Hello 3 (C) Copyright 2020 by Prof. Abeer ElKorany
num 4 Example of Repetition
int num;
false
for ( num = 1 ; num <= 3 ; num++ )
OUTPUT
Hello 1
Hello 2
Hello 3
(C) Copyright 2020 by Prof. Abeer ElKorany
Simple For Example
i 0
Example
for (int i = 0; i < 3; ++i) i 1
{ i 2
cout << "i is " << i << endl;
i 3
}
cout << "all done" << endl;
i is 0
i is 1
This code produces
i is 2
all done
(C) Copyright 2020 by Prof. Abeer ElKorany
#include <iostream>
using namespace std;
int main()
{
const int MIN_NUMBER = 1, // Starting value
MAX_NUMBER = 10; // Ending value
int num;
cout << "Number\t Number Squared\n";
cout << "-------------------------\n";
for (num = MIN_NUMBER; num <= MAX_NUMBER; num++)
cout << num << "\t\t" << (num * num) << endl;
return 0;
}
return 0;
}
(C) Copyright 2020 by Prof. Abeer ElKorany
The for statement (cont.)
More than one control variable in for loop
#include <iostream>
95% 11=7
using namespace std; This code produces
92% 12=8
int main)(
89% 13=11
{
86% 14=2
int n,m; 83% 15=8
for (m=95, n=11; m%n > 0; n++)
{
cout << m << "%" << n << " = " << m%n << endl;
m-=3;
}
return 0;
}
(C) Copyright 2020 by Prof. Abeer ElKorany
#include <iostream>
using namespace std;
The for statement (cont.)
int main()
{
int x;
for (x = 3 ; x < 13 ; x++)
{
switch (x)
{
case 3 : cout<<"The value is three\n"; break;
case 4 : cout<<"The value is four\n"; break;
case 5 :
case 6 :
case 7 :
case 8 : cout<<"The value is between 5 and 8\n"; break;
case 11 : cout<<"The value is eleven\n";break;
default : cout<<"It is one of the undefined values\n";break;
} /* end of switch */
} /* end of for loop */
return 0; (C) Copyright 2020 by Prof. Abeer ElKorany
}
• The value is three
• The value is four
• The value is between 5 and 8
• The value is between 5 and 8
• The value is between 5 and 8
• The value is between 5 and 8
• It is one of the undefined values
• It is one of the undefined values
• The value is eleven
• It is one of the undefined values
for (j = 0; j < n)
{
int row,col;
for (row=1; row<=3; row++) //outer
{
Output is
for (col=1; col<=3; col++)//inner 1 2 3
cout << row * col << "\t ";
2 4 6
cout<< endl;
} 3 6 9
return 0;
}
product = 2 * product;
(C) Copyright 2020 by Prof. Abeer ElKorany
#include <iostream>
loop control variable
using namespace std;
pls enter the max number 4
int main()
{
Number Number Squared
const int MIN_NUMBER = 1; 1 // Starting number to square 1
int MAX_NUMBER ; 2 // Maximum number to square 4
int num = MIN_NUMBER; //3Counter: a variable that must be initialized 9
before entering loop
}
return 0;
}
int number = 1;
while (number <= 5)
{
cout << "Hello\n";
}
false
j is 50
j is 60
We are out of the(C)loop
Copyrightas
2020j=70.
by Prof. Abeer ElKorany
The continue Statement
int j = 40;
while (j < 80){
j += 10;
if (j == 70)
continue; //skips the 70
cout << “j is “ << j<< ‘\n’;
}
cout << “We are out of the loop” << endl;
j is 50
j is 60
j is 80
We are out of the loop.
(C) Copyright 2020 by Prof. Abeer ElKorany
Break and Continue statement (cont.)
#include <iostream>
using namespace std;
int main()
{
int x;
for(x = 5 ; x < 15 ; x ++)
{
if (x == 8)
break;
cout<<"In the break loop, x is \t"<< x<<"\n";
}
cout<< "\n";
for (x = 5 ; x < 15 ; x ++)
{
if (x == 8)
continue;
cout<<"In the continue loop, x is \t" <<x<<"\n";
}
return 0;
(C) Copyright 2020 by Prof. Abeer ElKorany
}
Program output:
In the break loop, x is 5
In the break loop, x is 6
In the break loop, x is 7