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

PF Ue Lec 5

Uploaded by

hamzaamir9733
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 views19 pages

PF Ue Lec 5

Uploaded by

hamzaamir9733
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/ 19

Programming Fundamentals

(COMP1112)
Lecture 4
Repetition Structure

Division of Science & Technology


University of Education, Lahore.
Repetition structure - Loops
• A loop consists of two parts, a body of a loop and a control
statement. The control statement is a combination of some
conditions that direct the body of the loop to execute until the
specified condition becomes false.
Types of loop
• for loop
• while loop
• do-while loop
for loop
for (initial value; condition; increment or decrement statement)
{ statements; }

• The initial value of the for loop is performed only once.


• The condition is a Boolean expression that tests and compares the
counter to a fixed value after each iteration, stopping the for loop
when false is returned.
• The increment/decrement increases (or decreases) the counter by a
set value.
For loop working flow
Examples
1)
for(int number=1;number<=10;number++)
cout<<number;
• We can skip the initial value expression, condition and/or increment
by adding a semicolon.
2) int i=0;
int max = 10;
for (; i < max; i++)
cout<<i;
Nested loops
• loops can also be nested where there is an outer loop and an
inner loop.
• For each iteration of the outer loop, the inner loop repeats its
entire cycle
Example
int table = 3;
int max = 5;

// outer loop
for (int i = 2; i <= table; i++)
{
for (int j =1 ; j <= max; j++) // inner loop
{
cout<< i*j;
}

cout<<“\n”;
}
}
While Loop

while (condition) {
statements;
}

If a condition is true then and only then the body of a loop is executed.
After the body of a loop is executed then control again goes back at the
beginning, and the condition is checked if it is true, the same process is
executed until the condition becomes false. Once the condition becom
es false, the control goes out of the loop.
Example
int num =0;
while (num<=10)
{
cout<<num;
num++;
}
do while loop
do
{
Statements
} while (expression);

• A do-while loop is similar to the while loop except that the condition is
always executed after the body of a loop. It is also called an exit-controlled
loop.
• The body is executed if and only if the condition is true. In some cases, we
have to execute a body of the loop at least once even if the condition is
false.
Example
int num =1;

do {
cout<<2*num;
num++;
} while (num<=10);
Break Statement
• The break statement is used in the switch statement. It is also useful for immediately stopping a
loop.
int main()
{
int num = 5;
while (num > 0)
{
if (num == 3)
break;
cout<<num;
num--;
}
}
Continue Statement
• When you want to skip to particular iteration but remain in the loop, you should
use the continue statement.
int main()
{
int nb = 7;
while (nb > 0)
{ nb--;
if (nb == 5)
continue;
cout<<nb;}
}
Example: C++ code to find the sum of first n
natural numbers till the positive integer entered
by user
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;

for (int i = 1; i <= num; ++i) {


sum += i;
}

cout << "Sum = " << sum << endl;


Example: C++ code to display multiplication
table of a number entered by user
int n;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
Infinite for loop

If the condition is always true, the loop becomes infinite.

for(int i = 1; i > 0; i++) {


// block of code
}

In order to ensure termination of loop, it needs to be checked that the


condition for any iteration needs to be false.
Which loop to Select?
• Selection of a loop is always a tough task for a programmer; to select
a loop do the following steps:
• Analyze the problem and check whether it requires a pre-test or a post-test
loop.
• If pre-test is required, use a while or for loop.
• If post-test is required, use a do-while loop.
• For and while loop are entry-controlled loops.
• Do-while is an exit-controlled loop.
References
• C++ How to Program
By Deitel & Deitel

• The C++ Programming Language


By Bjarne Stroustrup

• Object oriented programming using C++ by Tasleem Mustafa, Imran Saeed, Tariq Mehmood, Ahsan Raza

• https://www.tutorialspoint.com/cplusplus

• http://ecomputernotes.com/cpp/introduction-to-oop

• http://www.cplusplus.com/doc/tutorial

• https://www.programiz.com

• https://www.guru99.com/c-loop-statement.html

You might also like