0% found this document useful (0 votes)
3 views52 pages

CH7-Problem Solving With Loops

Chapter 7 discusses problem-solving using loops in programming, covering various loop structures such as While/WhileEnd, Repeat/Until, and Automatic-Counter loops. It explains the concepts of incrementing, accumulating, and recursion, providing examples and flowcharts for clarity. The chapter emphasizes the importance of initializing variables and the conditions under which each loop type should be used.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views52 pages

CH7-Problem Solving With Loops

Chapter 7 discusses problem-solving using loops in programming, covering various loop structures such as While/WhileEnd, Repeat/Until, and Automatic-Counter loops. It explains the concepts of incrementing, accumulating, and recursion, providing examples and flowcharts for clarity. The chapter emphasizes the importance of initializing variables and the conditions under which each loop type should be used.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Chapter 7: Problem

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.

• The loop logic structure is the repeating


structure.

• So, it is a computer task, that is used for Repeating a


series of instructions many times.

• Example: The Process of calculating the Total


Payment for more than Dr.Hanin
one employee. 3
Abdulrahman
Exampl
e
Write an Algorithm that calculate the
average of 6 students grade.

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.

•Repeat/Until loop, which repeats instructions


while a condition is False or until a condition is
True.

•Automatic-counter loop in which a variable is


set equal to
a given number and increases in equal given
increments until it is greater than an ending
Dr.Hanin
Abdulrahman
5
Types of Loop
Structure
• The algorithm and the flowchart differ with each
type of loop structure.

•Several standard types of tasks are


accomplished through the use of the loop
structure.
•Two of these types are:
• counting (also called incrementing and
decrementing)
• accumulating (also called calculating a
Dr.Hanin 6
sum or a total) Abdulrahman
Types of Loop
•Structure
In both tasks (counting, accumulating) a
number is added or subtracted from a
variable and the result is stored back into
the same variable.

•In each case the resultant variable is assigned


the value of zero before starting the loop 
called initializing the variable.

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.

 First, the plus sign is replaced by the


multiplication sign (*).
Second, the product variable must be initialized to 1
instead of 0.

• 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.

This flowchart shows only


part of an algorithm, on-
page connectors are used.
Dr.Hanin 13
Abdulrahman
While/
WhileEnd
• At the beginning of the While/WhileEnd loop, the
program processes the condition in order to decide
whether to execute the loop instructions.

• When the resultant of the condition is True, the


complete set of instructions will be executed, and then
the computer will go back to the beginning of the loop
and process the condition again.

• The loop will repeat until the resultant of the condition


is False.
Dr.Hanin 14
Abdulrahman
While/
WhileEnd
• Use the While/WhileEnd structure when:
you do not know the number of times the
instructions will be repeated,
or if there are cases when the instructions in the loop
should not be processed.

• Examples of such cases are:


when you are entering information on clients and
you don’t know the number of clients;
when you are finding the total amount of sales for
the day and you don’t know the number of sales.
Dr.Hanin 15
Abdulrahman
While/WhileEnd
Example
Problem: Create the algorithm and the
flowchart to find the average age of all the
students in a class.

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.

• The last age should be zero. This zero is called a trip


value, or a flag.
->It allows the value of Age to control when to stop
the looping process and continue with the rest of the
solution. Dr.Hanin 18
Abdulrahman
Types of Loop
Structure
1. While/WhileEnd loop
2. Repeat/Until loop
3. Automatic-counter
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,

when you know they will be executed at


least once,

or when you do not know the condition for


repeating until after the instructions in the
loop are processed.
Dr.Hanin 23
Abdulrahman
Repeat/Until
loop
Average Age
of a Class

Dr.Hanin 24
Abdulrahman
Perfect Square number problem

How to check if the number is perfect square or not???


If the square root of the number is integer number
e.g) 4 , 9 , 16, 25 , 36 ,……. So on
Types of Loop
Structure
1. While/WhileEnd loop
2. Repeat/Until loop
3. Automatic-counter
loop

Dr.Hanin 26
Abdulrahman
Automatic-Counter
Loop
• This type of loop increments or decrements a variable each
time the loop is repeated

• The programmer uses a variable as a counter that starts


counting at a specified number and increments the variable
each time the loop is processed.

• The beginning value, the ending value, and the increment


value may be constants, variables, or expressions
(calculations).

• 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.

2. When the computer executes the Loop-


End, it increments the counter.

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.

4. When the counter is greater than the


ending number
 Stop

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.

2. When the counter is greater than or equal to


the ending value, the loop continues.

3. Step value needs to be a negative number


and begin must be greater than end
Dr.Hanin
Abdulrahman
32
Automatic-Counter Loop
Example
Create the algorithm and the flowchart to
find the average age of the 12 students in
a class.

How many Automatic-


students?? 12 Counter 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

• The decision for both loops involves the use of the


ending value The automatic-counter loop takes care of
these instructions automatically.

• The While/WhileEnd loop executes the test at the


beginning of the loop, and the Repeat/Until executes
it at the end.

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.

• When you know the starting and ending values


of the counter and you need a counter to run
through a group of instructions multiple times, the
use of the automatic counter loop is preferred.

Dr.Hanin 42
Abdulrahman
Decision Equivalents to Automatic-
Counter Loop

Dr.Hanin 43
Abdulrahman
Nested
Loops

Loops can be nested like decisions can.

Each loop must be nested inside the loop just


outside it.

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

You might also like