5 Iterative Control Flow and For Loop
5 Iterative Control Flow and For Loop
By:
Safeer Khan
Lecturer in Computer Science
[email protected]
1
Contents
• Iterative Control Flow
• Types of Iterative Control Flow
• Counter-Controlled Repetition
• Counter-Controlled Repetition – Example
• Sentinel-Controlled Repetition
• Sentinel-Controlled Repetition – Example
• for Loop/Statement in C++
• for Loop/Statement – Syntax
• for Loop/Statement – Flow Chart
• for Loop/Statement - Example
2
Iterative Control Flow
• Iterative control flow is also referred to as repetition logic or loop.
• It is one of the order in which the program instructions are executed.
• It executes the instructions repetitively multiple number of times.
• Repetitive logic allows us to execute a statement or set of statements
multiple number of times just by writing down them once.
• Iteration is the act of repeating the statements, and each of the repetition
is also called as iteration.
• In iterative logic:
• Statements are executed multiple number of times on the basis of the condition.
• All statements are repeated until a certain condition is reached.
• A condition may be open-ended as in a "sentinel" repetition or it may be predefined
as in the "counter-controlled" repetition.
3
Types of Iterative Control Flow
• In programming there are two types of iterative/repetitive control flow or
loops:
Counter-Controlled Repetition
• In counter-controlled repetition, a counter variable is used that specifies
how many times the statements are to be repeated.
• The case, when we know exactly how many times we have to repeat the
statements, we will use counter-controlled repetition logic.
• Suppose the set of statements need to be executed is N number of times.
We will first set the counter to 1, and every time we check the counter
(counter <= N) and increment it (counter = counter + 1).
• Example:
• Display word “PAKISTAN” 5 times on the screen.
Counter-Controlled Repetition
• In counter-controlled repetition:
• The condition is predefined.
• The statements are repeated if the condition is satisfied (true).
• The repetition structure is terminated when condition is not satisfied
(false).
Counter-Controlled Repetition
• Following is the flow of execution of counter-controlled repetition:
counter = 1
TRUE
Statements
counter = counter + 1
Counter-Controlled Repetition - Example
• Problem Statement: Display the integer numbers from 1 to 10.
i=1
i <= 10 FALSE
TRUE
Print i
i=i+1
Sentinel-Controlled Repetition
• In sentinel-controlled repetition, a condition specifies how many times the
statements are to be repeated.
• The case, when we do not know exactly how many times we have to repeat
the statements, we will use sentinel-controlled repetition logic.
• Suppose we have to create a program that continuously reads lines from a
text file and displays them until it reaches the end of the file. In this case
we do not know how many lines will be there in different text files.
Sentinel-Controlled Repetition
• In sentinel-controlled repetition :
• The condition is open-ended.
• The statements are repeated if the condition is satisfied (true).
• The repetition structure is terminated when condition is not satisfied
(false).
Sentinel-Controlled Repetition
• Following is the flow of execution of sentinel-controlled repetition:
TRUE TRUE
Statements Statements
Sentinel-Controlled Repetition - Example
• Problem Statement:
• The user continuously Input num
{
statement;
statement; Multiple Statements for loop body
statement;
} Note: No Semicolon here
for Loop/Statement – Syntax
Initialization Expression
Test Expression
Increment Expression
Initialization
Test FALSE
TRUE
for block
Increment
Example Program
• Write a program in c++ to print “Hello world” ten times.
19
Example Program
• Writea program that displays • Write a program that displays
counting from 1 to 10 using for first five numbers and their sum
loop using for loop
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
for(int i=1; i<=10;i++) int sum=0;
{ for(int i=1; i<=5;i++)
cout<<i<<endl; {
} cout<<i<<endl;
return 0; sum=sum+i;
} }
cout<<“sum=“<<sum;
return 0;
}
20
for Loop/Statement - Example
• Problem Statement 1: Display all the integers between 10 and 20.
#include<iostream.h>
using namespace std;
int main()
{
for(int i=10; i<=20; i++)
{
cout<<i<<endl;
}
return 0;
}
Example Program
• Write a program that inputs table number and length of table and then
displays the table using for loop
22
Example Program
• Problem Statement: Write a program in C++ that generates and displays the
following series of numbers:
1, 4, 9, 16, 25, 36, 49, 64, 81, 100
#include<iostream.h>
using namespace std;
int main()
{
for(int i= 1; i<= 10; i++)
{
cout<< i * i << " ";
}
return 0;
}
23
Example Program
• Problem Statement: Write a program in C++ that generates and displays the
following series of numbers:
15, 30, 45, 60, 75, 90, 105, 120, 135, 150
#include<iostream.h>
using namespace std;
int main()
{
for(int i= 15; i<= 150; i+= 15)
{
cout<< i << " ";
}
return 0;
}
24
Lab Work Problems
• Write a program in c++ to print “Hello world” ten times.
• Write a program using a for loop to print numbers from 1 to 10
• Write a program to print numbers from 10 to 1 in reverse order using a for
loop.
• Write a program to calculate the sum of the first 10 natural numbers
using a for loop.
• Write a program to print all even numbers from 1 to 20 using a for loop.
• Write a program to find the sum of odd numbers between 1 and 20 using
a for loop.
• Write a program to display the multiplication table of a number entered
by the user. Use a for loop to print the table up to 10.
25
26