You are on page 1of 17

LOOPING STRUCTURES (II)

Muhammad Kamran
Lecturer Civil Engineering Department
Sarhad University of Information and Technology
“while” LOOP
Simplest loop of C++ Language.

“while” loop executes one or more statement while the


given condition remains true.

It is useful when number of iterations is not known in


advance.
while (loop-continuation- int count = 0;
condition) while (count < 100)
{ // loop-body; {
cout << "Welcome to C++\n";
Statement(s); count++;
} }

count = 0;

Loop
false false
Continuation (count < 100)?
Condition?

true true
Statement(s) cout << "Welcome to C++!\n";
(loop body) count++;

(a) (b)
Declare count
Trace for Loop And Variable initialized
count is now 0

int count =0;


while (count < 2)
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop
(count < 2) is true
int count =0;
while (count < 2)
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop

Print Welcome
int count =0; to C++
while (count < 2)
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop

int count =0; Increase count by


while (count < 2) 1
count is 1 now
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop
(count < 2) is still
true since
int count =0; count is 1
while (count < 2)
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop

Print Welcome
int count =0; to C++
while (count < 2)
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop

int count =0; Increase count by


while (count < 2) 1
count is 2 now
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop
(count < 2) is false
since
int count =0; count is 2 now
while (count < 2)
{
cout << "Welcome to C++\n";
count++;
}
Trace for Loop

The loop exits.


Execute the next
int count =0; statement after the
while (count < 2) loop.
{
cout << "Welcome to C++\n";
count++;
}
Exercise (while)
Write a program that display first five numbers and their sum
using while loop ??

Write a program that inputs a number from the user and


displays the factorial of that number using while loop ??

Write a program using while 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 ??
Exercise ( while )
Write a program to swap two numbers ??
Exercise (while)
 Two numbers are entered through the keyboard. Write a program to find the
value of one number raised to the power of another using while loop ??
 Write a program to reveres any given integer number e.g. if user enters 6754 it
gives us 4576 using while loop ??
 Write a program using while loop to print Fibonacci series of n terms where n is
input by user ??
Fibonacci Series: 0 1 1 2 3 5 8 13 24 ....
ITERATION Do’s
Make sure there is a statement that will eventually terminate
the iteration criterion.
The loop must stop!
Make sure that initialization of loop counters or iterators is
properly performed.
Have a clear purpose for the loop.
Document the purpose of the loop.
Document how the body of the loop advances the purpose
of the loop.

You might also like