You are on page 1of 23

LOOPING STRUCTURES

Muhammad Kamran
Lecturer Civil Engineering Department
Sarhad University of Information and Technology
MOTIVATION
• Suppose that you need to print a string (e.g., "Welcome to C++!") a hundred
times. It would be tedious to have to write the following statement a hundred
times:

cout << "Welcome to C++!";

So, how do you solve this problem?


LOOPS
• Control structure that repeats a statement or set of statements.

• Also known as iterative or repetitive structure.

• 3 Types of loops available in C++ .


• for
• while
• do-while
PURPOSE

• Basically used for 2 purposes.

• To execute a statement or number of statements for a specified number of


times. E.g. a user may display his name on screen for 5 times.

• To use a sequence of values. E.g. a user may want to display integers from 1
to 10.
“for” LOOP

• “for” loop executes one or more statements for a specified number of times.

• Also called counter controlled loop.

• Most frequently used loop by the programmers.


FLOWCHART FOR “for loop”

Initialize variable

Condition true
Test the variable statement Increment variable

false
“for” LOOP

• Syntax
for ( initialization ; condition ; increment/decrement )
{

Statement1;
Statement2;
}
TRACE FOR LOOP

Declare i
int i;
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP
Variable initialized

i is now 0
int i;
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP
(i < 2) is true

since i is 0
int i;
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP

int i; Print Welcome to C++!


for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP
Execute adjustment
statement
int i; i now is 1
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP
(i < 2) is still true

since i is 1
int i;
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP

int i; Print Welcome to C++!


for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP
Execute adjustment
statement
int i; i now is 2
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP
(i < 2) is false

since i is 2 now
int i;
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
TRACE FOR LOOP

Exit the loop. Execute the


int i; next statement after the loop
for( i=0; i<2; i++)
{
cout<<“Welcome to C++\n”;
}
EXERCISE (for)

• Write a program that displays “I love


Pakistan” for five times using “for” loop??

• Write a program that displays integers from 1


to 10 using “for” loop ??

• Write a program that displays first 5 numbers


and their sum using “for” loop ??
INCREMENT/DECREMENT
• int a = 4;
cout<<a;
cout<<a*4;
a--;
cout<<a+4;
a+=4;
cout<<a;
cout<<a*4;
EXERCISE (for)
• Write a program that inputs a number from
the user and displays a table of that number
using “for” loop ??

• Write a program that displays integers from 1


to 10 and with those integers check whether
it is even or odd and display it with the
integer in the output using “for” loop ??

• Write a program that take 5 numbers from


user and tell which one is greater ??
MODULUS OPERATOR
• int k , digits ;
cout<<"Enter a number\n";
cin>>k;
cout<<"The digits you entered are \n";
for ( int i =k ; i>=1; i=i/10)
{
digits = i %10;
cout<<digits;
cout<<endl; }
EXERCISE (for)

• Write a program using for loop that inputs an


integer from the user and displays the sum of
its digits. For Example, the program should
display 11 if the user enters 236 ??

• Write a program that inputs a number from


the user and displays the factorial of that
number using for loop ??
EXERCISE (for)

• Write a program to print multiplication table of a number entered by the user using for loop ??

• Write a program that inputs a number from the user , Divide into the digits and check the largest
digit in the number and smallest digit in the number using for loop ??

• Write a program that inputs a number. It then displays the sum of all odd numbers and the sum of
all even numbers separately from 1 to the number entered by the user using for loop ??

• Write a program that inputs an integer from the user and prints if it’s a prime number or composite
number. A number is prime if it has factors 1 and itself, otherwise it’s a composite number using for
loop ??

You might also like