0% found this document useful (0 votes)
3 views30 pages

PF For Loops

This document covers control structures in programming, specifically focusing on repetition (looping) control structures. It explains counter-controlled and sentinel-controlled loops, along with the use of break and continue statements, and provides examples of for loops in C++. The document emphasizes the importance of loops for executing statements multiple times and outlines the syntax and logic of for loops.
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)
3 views30 pages

PF For Loops

This document covers control structures in programming, specifically focusing on repetition (looping) control structures. It explains counter-controlled and sentinel-controlled loops, along with the use of break and continue statements, and provides examples of for loops in C++. The document emphasizes the importance of loops for executing statements multiple times and outlines the syntax and logic of for loops.
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/ 30

CSC103-Programming

Fundamentals
(PF)
Asma Khalid
Control Structures
(Repetition)
Programming Fundamentals
Objectives
In this Lecture, you will:
 Learn about repetition (looping) control
structures
 Explore how to construct and use counter-
controlled and sentinel-controlled repetition
structures
 Examine break and continue statements
 Discover how to form and use nested
repetition structures
3
Why Is Repetition Needed?
 Repetitionstatements allow us to execute
statement/s multiple times
 Often they are referred to as loops
 Loopscause a section of your program to be
repeated a certain number of times
 Repeats until the condition remains true
 Terminates when the condition becomes
false
4
Loops
 Counter-controlled Loops
 Depends on the value of a variable known as counter variable.
The value of the variable is incremented or decremented in
each iteration.
 Example: for loop
 Sentinel-Controlled Loops / Conditional loop
 A loop that terminates when something happens inside the
loop body indicating that loop should be exited. Also known as
conditional loops
 Example: while and do loops
Introduction to Computer Programming 5
Types of Loops in C++

C++ have 3 types of Loops


The for loop
The while loop
The do while loop

Introduction to Computer Programming 6


Repetition
The for Loop
The for Loop (Syntax)

The initialization The statement is


is executed once executed until the
before the loop begins condition becomes false

for ( initialization ; condition ; variable update ){


statement;
}
The variable update portion is
executed at the end of each
iteration

Introduction to Computer Programming 8


The for Loop
 Initialization is done only once before first iteration
of loop.
 Can have multiple initialization statements
 Can be used to declare a variable
 Condition is checked in each iteration before
executing the body ( block of code) of loop.
 Increment / decrement or any expression written at
statement 3 (variable update) is executed at the end of
each Iteration
 Can have multiple Statements
Introduction to Computer Programming 9
Logic of a for Loop (Flow Chart)

Introduction to Computer Programming 10


Simple Algo of for Loop
 First step: In for loop, initialization happens first and only once,
which means that the initialization part of for loop only executes once.
 Second step: Condition in for loop is evaluated on each loop
iteration, if the condition is true then the statements inside of for loop
body gets executed. Once the condition returns false, the statements
in for loop does not execute and the control gets transferred to the
next statement in the program after for loop.
 Third step: After every execution of for loop’s body, the
increment/decrement part of for loop executes that updates the loop
counter.
 Fourth step: After third step, the control jumps to second step and
condition is re-evaluated.
 The steps from second to fourth repeats until the loop condition
returns false.
Introduction to Computer Programming 11
An example of a for loop:

 Problem Statement:
Write a program which displays “Hello world” on
screen 10 times.

Introduction to Computer Programming 12


Solution 1 (without loop)
int main()
{
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
cout<<“Hello World”<<endl;
return 0;
Introduction to Computer Programming 13

}
Solution 2 (with for loop)
int main()
{
int i;
for( i=1; i<=10; i++)
{
cout<<“Hello World”<<endl;
}
return 0;
}
Introduction to Computer Programming 14
Another Example

int main(){
for(int i=1; i<=6; i++){
/* This statement would be executed
* repeatedly until the condition
* i<=6 returns false.
*/
cout<<"Value of variable i is: "<<i<<endl;
}
return 0;
}
Introduction to Computer Programming 15
Another Example

int main(){
for(int i=1; i<=6; i++){ Output:
/* This statement would be executed Value of variable i is: 1
* repeatedly until the condition Value of variable i is: 2
* i<=6 returns false. Value of variable i is: 3
*/ Value of variable i is: 4
cout<<"Value of variable i is: Value of variable i is: 5
"<<i<<endl;
Value of variable i is: 6
}
return 0;
}
Introduction to Computer Programming 16
Infinite for loop
 When it executes repeatedly and never stops
 Cause:
When you set the condition in for loop in such a way that it
never return false, it becomes infinite loop.
 Example
int main(){
for(int i=1; i>=1; i++){
cout<<"Value of variable i is: "<<i<<endl;
}
return 0;
}
Introduction to Computer Programming 17
for loop – Multiple Expressions
Multiple Initialization Only one Multiple Increment/Dec
expressions Test Condition expressions

for (int j=0, k=9; j<10; j++,k--)


{
cout << j * j <<endl;
cout<< k*k <<endl;
}
Introduction to Computer Programming 18
for loop – Scope of Variable

int main()
{
int j;
for(j=0; j<10; j++) {
int k=0;
k = j*j;
cout<<“\nValue of k: “<<k;
}
// k = 23; Cannot do this!
return 0;
}
Introduction to Computer Programming 19
The for Loop
Optional Expressions
The for Loop (Syntax)

The initialization The statement is


is executed once executed until the
before the loop begins condition becomes false

for ( initialization ; condition ; variable update ){


statement;
}
The variable update portion is
executed at the end of each
iteration

Introduction to Computer Programming 21


for Loop – Optional Expression
 Option 1  Option 3 Output:
int j=0; int j=0; Hello world
for(; j<10; j++) for(; j<10; ) Hello world
Hello world
cout<<“\nHello { Hello world
world“; Hello world
cout<<“\nHello Hello world
 Option 2 world“; Hello world
int j; j++; Hello world
Hello world
for(j=0; j<10; ) } Hello world
{
cout<<“\nHello
world“;
Introduction to Computer Programming 22

j++;
for Loop – Optional Expression

 Option 4
int j=0;
for(; ; )
{
cout<<“\nHello world“;
j++;
}
 Infinite loop

Introduction to Computer Programming 23


break & continue
Used to alter the flow of control
The break statement
 break statement is used for two purposes:
 To exit early from a loop even if the loop condition is
true
 To skip the remainder of the switch structure
 After the break statement executes, the
program continues with the first statement
after the structure

Introduction to Computer Programming 25


Using break
 Option 4  Option 4 (Corrected) Output:
int j=0; int j=0;
Hello world
for(; ; ) for(; ; ) Hello world
Hello world
{ { Hello world
cout<<“\nHello if(j >= 10) Hello world
world“; Hello world
break; Hello world
j++; Hello world
} cout<<“\nHello Hello world
world“; Hello world
 Infinite loop
j++;
}
Introduction to Computer Programming 26
The continue Statement

 continue is used in while, for, and do…while


structures
 When executed in a loop It skips remaining
statements after the continue statement
 Skips to the bottom of the loop
 Update statement is executed in for loops
 Condition check is evaluated in do…while loops
Introduction to Computer Programming 27
Continue example
int main() Output:
{
Hello world
int i; Hello world
for( i=0; i<10; i++) Hello world
Hello world
{ Hello world
cout<<“Hello World”<<endl; Hello world
Hello world
continue; Hello world
cout<<“This line will never be executed”<<endl; Hello world
Hello world
}
return 0;
}
Introduction to Computer Programming 28
Sum of +ve values entered by user
int v = -1, total = 0; Output:
for(int i=1; i<=5; i++) Enter a positive value
{ 1
Enter a positive value
cout << "Enter a positive value " 2
<< endl; Enter a positive value
3
cin >> v; Enter a positive value
if (v < 0) -6
continue; Enter a positive value
-7
total = total + v; Total is 6
}
cout<<"Total is "<<total;
Introduction to Computer Programming 29
The for Loop (Summary)
 A for loop is well suited for executing statements a
specific number of times that can be calculated or
determined in advance
 Each expression in the header of a for loop is optional
 If the initialization is left out, no initialization is
performed
 If the condition is left out, it is always considered to be
true, and therefore creates an infinite loop .Use break
statement.
 If the increment is left out, no increment operation is
performed
Introduction to Computer Programming 30

You might also like