0% found this document useful (0 votes)
45 views23 pages

Understanding the While Loop

This document discusses different types of loops in programming including while, do-while, for, and nested loops. It provides examples of each loop type and how to use them, as well as how to exit loops using break and continue statements. Key loop concepts covered include pretest and posttest loops, loop control variables, and accumulating totals within loops.

Uploaded by

wazmanzambia
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)
45 views23 pages

Understanding the While Loop

This document discusses different types of loops in programming including while, do-while, for, and nested loops. It provides examples of each loop type and how to use them, as well as how to exit loops using break and continue statements. Key loop concepts covered include pretest and posttest loops, loop control variables, and accumulating totals within loops.

Uploaded by

wazmanzambia
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/ 23

Structured

Programming
Loops
Introduction to Loops:

•Loop: a part of a program that


may execute > 1 time (i.e., it
repeats)
•Number of times to repeat is
known
•Number of times to repeat is
not Known
The while Loop
•while loop format:
while (condition)
{ statement(s);
}
•The {} can be omitted if there
is only one statement in the
body of the loop
while Loop Example
int number = 1;
while (number <= 5)
{ cout << number << " ";
number++;+ 1;
}
• produces output:
12345
• number is called a loop control
variable
Exiting the Loop
•The loop must contain code to
allow the condition to
eventually become false so the
loop can exit
•Otherwise, you have an infinite
loop (i.e., a loop that does not
stop)
Exiting the Loop
•Example infinite loop:
x = 5;
while (x > 0)// infinite loop because
cout << x; // x is always > 0

555555555555555555555555555555555555
55555555555555555555555555555555555
55555555555555555555555555555555555
55555555555555555555555555555555555
55555555555555555555555555555555555
555555555555555555555555555555555
Input Validation Loop Example
cout << "Enter a mark (1-100) ";
cin >> mark;
while (mark < 1 || mark > 100)
{ cout << “Mark must be between 1 and 100."
<< " Re-enter your mark. ";
cin >> mark;
}
// Code to use the valid number goes here.
User Controls the Loop Example
int num=1, limit;
cout << “How many numbers do you
want to print\n";
cin >> limit;
while (num <= limit)
{ cout <<num<<endl;
num++;
}
Keeping a Running Total
• running total: an accumulated sum of numbers from
each iteration of loop
• accumulator: a variable that holds running total
int sum = 0, num = 1, usernumber=0; // sum is the
while (num <= 3) // accumulator
{
cout<<"Enter a number \n";
cin>>usernumber;
sum += usernumber;
num++;
}
cout << "Sum of numbers is “ << sum << endl;
The do-while Loop
•do-while: a post test loop
(condition is evaluated after
the loop executes)
The do-while Loop
•Syntax:
do
{
Statement(s);
} while (condition);

Note the
required ;
The do-while Loop Example
• int val=1;
do
{
cout << val << " ";
val = val + 1;
} while (val <=5);
• produces output:
1 2 3 4 5
Menu-Driven Program Example
do {
// code to display menu
// and perform actions
cout << "Another choice? (Y/N) ";
} while (choice =='Y'||choice =='y');

The condition could be written as


(toupper(choice) == 'Y');
or as
(tolower(choice) == 'y');
The for Loop
• It is a pretest loop that executes zero or
more times
• It is useful for a counter-controlled loop

Required ;
• Syntax:
for( initialization; test; update
)
{statements;
}
for Loop
for Loop Example
float average=0
int sum = 0, num;
for (num = 1; num <= 5; num++)
sum += num;
cout << "Sum of numbers 1 – 5 is "
<< sum << endl;
Deciding Which Loop to Use
• while: pretest loop (loop body may
not be executed at all)
• do-while: post test loop (loop body
will always be executed at least once)
• for: pretest loop (loop body may
not be executed at all); has
initialization and update code; is
useful with counters or if precise
number of iterations is known
Nested Loops
• A nested loop is a loop that is inside the body of
another loop
• bool alive =true, hungry =true;
• While (alive)
• { //wake up;
• While (hungry)
•{
• //eat some more;
• //still hungry?;
• //cin>>hungry;
•}
• //sleep;
• //cin>>alive;
•}
Nested Loops

•Example: outer loop

for (row = 1; row <= 4; row++)


inner loop
{
for (col = 1; col <= 3; col++)
{
cout <<row*col<<“ “;
}
cout<<“\n”;
}
Breaking Out of a Loop
• break can be used to terminate
the execution of a loop iteration
•Avoid using break ( it makes code
harder to understand)
•When used in an inner loop,
break terminates that loop only
and returns to the outer loop
break Example
•for(int x=1;x<=10;x++)
•{
•if(x==5)
•break;
•cout<<x<<“ “;
•}
•1 2 3 4
The continue Statement
• You can use continue to go to the
end of the loop and prepare for next
iteration
• while and do-while loops go to
the test expression and repeat the
loop if test is true
• for loop goes to the update step,
then tests, and repeats the loop if
test condition is true
• Avoid using continue (like break,
it can make program logic hard to
understand)
continue Example;
•for(int x=1;x<=10;x++)
•{
•if(x==5)
•continue;
•cout<<x<<“ “;
•}
•1 2 3 4 6 7 8 9 10

You might also like