10/12/2017
Loops
Programming Fundamentals
Loops provide a way to repeat one or more statement
s as many times as our application requires until a
Understanding Loops particular condition is met
The statements inside a loop are called iteration
statements
2 Programming Fundamentals- Loops
Loops Loops
A loop condition can take a number of different forms,
Two essential elements of a loop: to provide different ways of controlling the loop
Execute the loop for a given number of times
1. Statement or block of statements that forms the
Execute the loop until a given value exceeds another
body of the loop, to be executed repeatedly
value
2. A loop condition of some kind that determines Execute the loop until a particular character is entered
when to stop repeating the loop from the keyboard
We can set the loop condition to suit the
circumstances
4 Programming Fundamentals- Loops
3 Programming Fundamentals- Loops
Types of Loops while loop
There are three different kinds of loop in C++.
Condition No
while(condition) is true?
The while loop {
Yes
The do-while loop // iteration statement(s)
} //end of while loop Loop
The for loop Statement(s)
Next Statement
5 Programming Fundamentals- Loops 6 Programming Fundamentals- Loops
1
10/12/2017
Exercise – While loop while loop
1. Write a program that calculates and displays If the condition controlling the loop
the sum of numbers between -5 and 15
produces an integer, the loop will continue
2. Write a program that calculates and displays
as long as the value is non-zero
the sum of numbers between -20 and 0 with
a limitation to set i = 0; Any non-zero integer is converted to type
bool as true , and only 0 converts to false
7 Programming Fundamentals- Loops 8 Programming Fundamentals- Loops
The do-while Loop Exercise – Do-While loop
3. Write a program (using do-while loop)
do that calculates and displays the sum of
{ Loop numbers between 1 and 10
Statement(s)
// iteration statement (s) 4. Write a program that reads marks of a
} while (condition); student in n quizzes and displays the
Condition Yes average quiz marks
is true?
No
This semicolon is
absolutely compulsory
Next Statement
9 Programming Fundamentals- Loops 10 Programming Fundamentals- Loops
This expression is The for loop
More Complex while Loop conditions Executed once, at the
beginning of the loop.
This expression is evaluated
at the end of each loop iteration.
It is typically used to It is typically used to modify
initialize one Or more the values of Variables initialized
loop variables in the First expression
while (choice ==‘y’ || choice ==‘Y’)
for( initializing_expression; condition; iteration_expression )
• while (tolower(choice) ==‘y’) {
The semi-colons must be present.
//iteration statement (s) The expression need not be
} //end of for loop
This expression is evaluated
at the beginning of each loop
iteration. If it is true the loop
continues, and if it is false
execution with the statements
after the loop
11 Programming Fundamentals- Loops
12 Programming Fundamentals- Loops
2
10/12/2017
Evaluate The for loop Exercise – For loop
Initialization
expression Flow chart 5. Write a program that iterates thorough the odd num
bers less than 30, and display the odd numbers.
Condition No
is true?
Initialize i =1
Yes
6. Write a program that iterates thorough the numbers
less than 30 (1 to 30), and display only those
Loop numbers that are divisible by 3.
Statement(s)
7. Write a program that iterates thorough the numbers
less than 30, and display only those numbers that
Evaluate
Iteration
are divisible by 3 and divisible 5.
expression 8. Write a program that iterates thorough the odd
numbers less than count(entered by user), and
Next Statement outputs the square of each number.
13 Programming Fundamentals- Loops 14 Programming Fundamentals- Loops
Loops and Variable Scope Nested Loops
for loop, like while and do-while loops, defines a We can place a loop inside another loop
scope
We can ‘nest’ loops to whatever depth we require for s
The loop statement or block, as well as any
expressions used to control the loop, all fall within the olving a problem
scope of a loop Nested loops can be of any kind:
This includes all three expressions used to control for
loop
A for loop, inside a while loop inside a do-while loop,
Any automatic variables declared within the scope of they can be mixed in any way we want
a loop do not exist outside the loop
15 Programming Fundamentals- Loops 16 Programming Fundamentals- Loops
Skipping loop Iteration Breaking Out of a Loop
If we want to end the loop prematurely,
If we want to skip one iteration of the loop and
The break statement will be used
jump onto the next iteration we make use of c
break;
ontinue statement.
Its effect is same as we saw in the switch statement
continue;
17 Programming Fundamentals- Loops 18 Programming Fundamentals- Loops
3
10/12/2017
Indefinite Loops Bonus
An indefinite loop can potentially run forever Write a program to count the number of
characters (not including whitespace) entered
If we leave out the test conditions in for loop, by the user. The count should end when it first
it runs forever encounters a # character in the input.
Indefinite loop is useful when we don’t know
advance how many iterations are required
19 Programming Fundamentals- Loops 20 Programming Fundamentals- Loops
Summary
A loop is a mechanism for repeating a block of statements
There are three kinds of loop that we can use: while, do-while a
nd for
The while loop repeats for as long as a specified condition is
true
The do-while loop always performs at least one iteration, and
continues for as long as a specified condition is true
The for loop is typically used to repeat a given number of times,
and has three expressions
Any kind of loop may be nested within any other kind
Executing a continue statement within a loop will skip the
remainder of the current iteration and go straight to the next itera
tion
Executing a break statement within a loop will cause an
immediate exit from the loop
A loop defines a scope, so that variables declared within loop
are not accessible outside the loop. Variables declared in the
initialization expression of for loop are not accessible outside
21 Programming Fundamentals- Loops