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

Unit 5 - Looping

Uploaded by

Augustin
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)
19 views19 pages

Unit 5 - Looping

Uploaded by

Augustin
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

Looping

Learning Outcomes
• The advantages of looping
• Using a loop control variable
• Nested loops
• Using a for loop
Advantages of Looping
Although making decisions is what makes
computers seem intelligent, looping makes
computer programming both efficient and
worthwhile. When you use a loop, one set of
instructions can operate on multiple, separate sets
of data. Using fewer instructions results in less
time required for design and coding, fewer errors,
and shorter compile time.
Loop Control Variable
• You can use a while loop to execute a body of statements
continuously as long as some condition continues to be true. The
body of a loop might contain any number of statements, including
function calls, decisions, and other loops.
• To make a while loop end correctly, you should declare a loop control
variable to manage the number of repetitions a loop performs.
• Three separate actions should occur:
– The loop control variable is initialized before entering the loop.
– The loop control variable is tested, and if the result is true, the loop body is
entered.
– The loop control variable is altered within the body of the loop so that the
while expression eventually evaluates as false.
Cont…
Within a loop’s body, you can change the value of the loop
control variable in a number of ways. For example:
• You might simply assign a new value to the loop control
variable.
• You might retrieve a new value from an input device.
• You might increment, or increase, the loop control
variable.
• You might reduce, or decrement, the loop control
variable.
Recall Example2: While loop

Start
While True
Output statement “ I am stuck inside this loop”
Endwhile

NB: Predict the output of above loop


Recall : Example3:Nested Loops

Start
largest_number = -999999999
Input first number OR -1 to Stop
While number != -1
If number > largest_number then
largest_number = number
Input next number OR -1 to stop
EndWhile
Output largest_number
Stop
NB: Predict output if test data for number is 4, 3,20,10
Cont…
Exercise
Use the While Loop: write a pseudocode for a program which
takes Name and Surname as input from 10 users. It should
also output the Name and Surname for those 10 users.
Nested Loops
• Program logic gets more complicated when you
must use loops within loops, or nested loops.
When one loop appears inside another, the loop
that contains the other loop is called the outer
loop, and the loop that is contained is called the
inner loop.
Nested Loops: Example
Declare Integer seconds , minutes
For minutes = 1 to 60
For seconds = 1 to 60
output seconds
Endfor
Output minutes
Endfor
for Loop
The for statement, or for loop, with definite
loops—those that will loop a specific number of
times—when you know exactly how many times
the loop will repeat. In a for statement, a loop
control variable is:
• Initialized
• Evaluated
• Altered
Predict the output
Predict Output for the following Pseudocode

Input a Number
For i = 1 to 10 step 1
result = Number * i
Output result
endfor
Exercise
Use the For Loop: write a pseudocode for a
program which takes Name and Surname as input
from 10 users. It should also output the Name
and Surname for those 10 users.
Exercise
1. Design the logic for a program that outputs every
number from 1 through 20.
2. Design the logic for a program that outputs every
number from 1 through 20 along with its value
doubled and tripled.
3. Design the logic for a program that outputs every
even number from 2 through 100.
4. Design the logic for a program that outputs
numbers in reverse order from 25 down to 0.
References
Required reading:
• Programming Logic and Design, Comprehensive,
Cengage Learning, 2018 by Joyce Farrell
Reference Materials:
• Starting Out with Programming Logic and
Design, Pearson Education, 2015 by Tony Gaddis
THANK YOU
For your time!

FET

You might also like