CH7-Problem Solving With Loops
CH7-Problem Solving With Loops
Solving with
Loops
SW 100: Principles of Programming and Problem Solving
Dr.Hanin 1
Abdulrahman
Overvie
w
• The Loop Logic Structure
• Incrementing
• Accumulating
• While/WhileEnd
• Repeat/Until
• Automatic-Counter Loop
• Nested Loops
• Indicators
• Algorithm Instructions and Flowchart
Symbols
• Recursion
Dr.Hanin 2
Abdulrahman
The Loop Logic
•Structure
A third logic structure for designing decisions is
the loop structure.
Average()
1. Enter Grade1, Grade2, Grade3, Grade4, Grade5, Grade6 What about 100
2.Total = Grade1+ Grade2+Grade3+Grade4+Grade5+ students?
Grade6
3. Average = Total / 6 We need to use a
4. Print Average loop logic structure
End
Dr.Hanin 4
Abdulrahman
Types of Loop
Structure
•While/WhileEnd loop which repeats instructions while
a
condition is True and stops repeating when a
condition arises that is not True.
Dr.Hanin 7
Abdulrahman
Incrementing or
Decrementing
• The task of incrementing, or counting, is done by
adding a constant, such as 1 or 2, to the value of
a variable.
• Exampl
C must be initialized to zero before
e:
C=C+ starting the loop
1
• This instruction enables the programmer to count the
number of items, people, temperatures, and so on, as
part of the solution to a problem.
• Decrement C=C-
example: Dr.Hanin
1 8
Abdulrahman
Accumulati
ng
• Summing a group of numbers is a process of
accumulating is similar to incrementing, except a
variable instead of a constant is added to another
variable, which holds the value of the sum or total.
• The instruction for accumulating is the following:
Sum = Sum + Variable or S = S + V
• For example, the instruction to find total sales would
be
TotalSales = TotalSales + Sales
Sum and TotalSales must be
initialized
Dr.Hanin
Abdulrahman to zero before starting the 9
Accumulati
•ng
Calculating the product of a series of numbers is
similar to finding the sum of a series of numbers
with two exceptions.
• For example:
Product=1
Dr.Hanin 10
Abdulrahman
Types of Loop
Structure
1. While/WhileEnd loop
2. Repeat/Until loop
3. Automatic-counter
loop
Dr.Hanin 11
Abdulrahman
While/
WhileEnd
This type of loop tells the computer that
while the condition is True, repeat all
instructions between the While and the
WhileEnd.
Dr.Hanin 12
Abdulrahman
While/
WhileEnd
The symbol used for the
While part of the
instruction is the
diamond, the symbol for a
decision.
How many
While/WhileEnd
students??
Dr.Hanin 16
Abdulrahman
Average Age
of a Class
Dr.Hanin 17
Abdulrahman
While/
WhileEnd
• An age must be entered before the loop so that the
condition has data to use to get a resultant.
->This is called a primer Read because it gives the
While/WhileEnd loop a valid value for the variable Age
in order for the conditions to be true the first time
through the loop.
Dr.Hanin 19
Abdulrahman
Repeat/
Until
This type of loop tells the computer to repeat
the set of instructions between the Repeat and
the Until, until a condition is True.
Algorithm Format
Dr.Hanin 20
Abdulrahman
Flowchart Diagram of
Repeat/Until
Dr.Hanin 21
Abdulrahman
While/WhileEnd vs
Repeat/Until
While/WhileEnd Repeat/Until
the program continues to loop as long the program stops the loop process when
as the resultant of the condition is the resultant of the condition is True
True
the condition is processed at the The condition is processed at the end.
beginning The instructions in the loop are processed
entirely at least once.
you must initialize the data so that the you can set the operands of the conditions
resultant of the condition is True the anywhere within the loop since the
first time through the loop. Otherwise, condition is processed at the end.
the loop instructions will never be
processed.
Dr.Hanin 22
Abdulrahman
Repeat/
Until
• Use the Repeat/Until structure
when you do not know the number of
times the instructions will be executed,
Dr.Hanin 24
Abdulrahman
Perfect Square number problem
Dr.Hanin 26
Abdulrahman
Automatic-Counter
Loop
• This type of loop increments or decrements a variable each
time the loop is repeated
• Use it when you know from the start the number of times
the loop will be executed .
Dr.Hanin 27
Abdulrahman
Automatic-Counter
Loop
The form of the algorithm for the automatic-counter
loop is the following:
Dr.Hanin 28
Abdulrahman
Flowchart of
Automatic- Counter
•Loop
Counter is the variable
used for the counter,
• Begin is the beginning
value,
• End is the ending value
• StepValue is the value by
which the counter is to be
incremented
• To decrement, StepValue
needs to be a negative
number and Begin must be
greater than End. Dr.Hanin 29
Abdulrahman
Automatic-Counter
Loop
When incrementing the counter, remember the
following rules:
1. When the computer executes the Loop
instruction, it sets the counter equal to the
beginning number.
Dr.Hanin 30
Abdulrahman
Automatic-Counter
Loop
3. When the counter is less than or equal
to the ending number
the processing continues at the instruction that
follows the Loop instruction.
Dr.Hanin 31
Abdulrahman
Automatic-Counter
Loop
When decrementing the counter , remember the
following rules:
1. The counter is decremented at the end of the
loop.
Dr.Hanin 33
Abdulrahman
Dr.Hanin 34
Abdulrahman
Automatic-Counter Loop
•Example
The counter begins at 1 and is incremented by 1
until the counter is greater than 12.
• When the increment value is left out, the computer
assumes the value to be 1.
• When the counter increases to a value greater than
12, the average age is calculated and printed.
• Also notice the absence of the primer Read. The primer
Read is not necessary because there is no trip value
needed since the number of students is known.
Dr.Hanin 36
Abdulrahman
While/WhileEnd Loop Equivalent
of the Automatic-Counter Loop
Dr.Hanin 37
Abdulrahman
While/WhileEnd Loop Equivalent of the
Automatic-Counter Loop (Av of age 12 student
example)
Repeat/Until Loop Equivalent
of the Automatic-Counter Loop
Dr.Hanin 39
Abdulrahman
Repeat/Until Loop Equivalent of the
Automatic-Counter Loop (Av of age 12 student
example)
The equivalent flowcharts of the
While/WhileEnd and Repeat/Until loops
• Note that both these loops require a counter
instruction and initialization of the counter
Dr.Hanin 41
Abdulrahman
The equivalent flowcharts of the
While/WhileEnd and Repeat/Until loops
• When Begin is greater than End at the beginning of
the loop, the While/WhileEnd will not execute any
internal instructions, whereas the Repeat/Until will
execute the instructions once.
Dr.Hanin 42
Abdulrahman
Decision Equivalents to Automatic-
Counter Loop
Dr.Hanin 43
Abdulrahman
Nested
Loops
Dr.Hanin 44
Abdulrahman
Nested
loops
Examples
Outer loop
(outer loop
Inner loop
(Inner loop
counter) counter)
1 1
1 2
1 3
2 1
2 2
2 3
Dr.Hanin 45
Abdulrahman
Nested loops
Examples
Outer loop Inner loop
(outer loop (Inner loop
counter) counter)
1 1
1 2
1 3
2 1
2 2
2 3
Dr.Hanin 46
Abdulrahman
Example
Draw flow chart for this problem: print all prime numbers between 2 and the number has been entered from
user
Recursio
n
• Another type of loop structure is recursion.
• Recursion occurs when a module or a function calls
itself.
• The condition that ends the loop must be within the
module.
• Factorial(N)
• An example of a recursive function that calculates a
factorial number such as 5! (5! = 5 * 4 * 3 * 2 * 1)
• Notice that Factorial(N) continues to call itself until
N=1. There must be a way Dr.Hanin
to stop the recursion. 49
Abdulrahman
Recursion
example
Dr.Hanin 50
Abdulrahman
Recursion
example
Dr.Hanin 51
Abdulrahman
Execution of a Recursion
Problem
Dr.Hanin 52
Abdulrahman