0% found this document useful (0 votes)
18 views26 pages

5 Iterative Control Flow and For Loop

English vocabulary C++

Uploaded by

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

5 Iterative Control Flow and For Loop

English vocabulary C++

Uploaded by

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

Programming Fundamentals

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

counter <= N FALSE

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:

variable ≠ sentinel FALSE condition FALSE

TRUE TRUE

Statements Statements
Sentinel-Controlled Repetition - Example
• Problem Statement:
• The user continuously Input num

enters integer numbers, the


program stops when the num >= 100
AND FALSE
user enters any number num <= 200

other than the numbers TRUE


between 100 and 200.
sum = sum + num
Finally it displays the sum of
all the numbers entered by Input num
the user.
Print sum
for Loop/Statement in C++
• for loop/statement implements counter-controlled repetition logic.
• The for loop is used to execute the particular statements of code fixed number of
times with out writing those statements that much number of time.
• The for loop is used when we know that how many times the loop will be
executed.
• It repeats statements on the basis of a condition.
• Here the condition is predefined.
• If the condition is true, statements are repeated again.
• If the condition is false, the loop is terminated.
• It uses one counter variable that counts the number of times, the loop is to be
repeated.
for Loop/Statement – Syntax

for(initialization; test; increment)


statement;

for(initialization; test; increment)


{
statement set ;
}
for Loop/Statement – Syntax

for (i=1; i<=10; i++) Note: No Semicolon here

{
statement;
statement; Multiple Statements for loop body

statement;
} Note: No Semicolon here
for Loop/Statement – Syntax
Initialization Expression

Test Expression

Increment Expression

for (i=1; i<=10; i++) Note: No Semicolon here

statement; Single Statement for loop body


Working of ‘for’ loop
• The number of iterations depends on the initialization, condition and
increment/decrement parts.
• The initialization part is executed only once when control enter the loop.
• After initialization, the given condition is evaluated. If it is true, the
control enters the body of loop and executes all statement in it.
• Then the increment/decrement part is executed that changes the value of
counter variable.
• The control again moves to the condition part.
• This process continues while the condition remains true.
• The loop is terminated when the condition becomes false.
17
for Loop/Statement – Flow Chart

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

You might also like