KEMBAR78
Chapter 7 Problem Solving With Loops | PDF | Algorithms | Control Flow
0% found this document useful (0 votes)
167 views18 pages

Chapter 7 Problem Solving With Loops

The document discusses different types of loop structures used in programming including while/while-end loops, repeat/until loops, automatic counter loops, and nested loops. It also covers topics like incrementing and accumulating values in loops, flowcharts to represent loop logic, and using indicators to control loop processing. Recursion is introduced as another type of loop structure where a function calls itself.

Uploaded by

latishy
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)
167 views18 pages

Chapter 7 Problem Solving With Loops

The document discusses different types of loop structures used in programming including while/while-end loops, repeat/until loops, automatic counter loops, and nested loops. It also covers topics like incrementing and accumulating values in loops, flowcharts to represent loop logic, and using indicators to control loop processing. Recursion is introduced as another type of loop structure where a function calls itself.

Uploaded by

latishy
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/ 18

Data Structures and Algorithms

Problem Solving
with Loops

The Loop Logic Structure


A third logic structure for designing decisions is the
loop structure. The loop logic structure is the repeating
structure.

Types of Loop Structures

@ WHILE / WHILE-END
@ REPEAT / UNTIL
@ AUTOMATIC COUNTER LOOP

Incrementing
The task of incrementing, or counting, is done by
adding a constant, such as 1 or 2, to the value of a
variable. To write the instruction to increment a
variable, you use an assignment statement. For
example:

COUNTER = COUNTER + 1 or C = C+ 1
Problem Solving with Loops *Property of STI
Page 1 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Accumulating
Another task that a program must often perform is
accumulating, or summing a group of numbers. The
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


PRODUCT = PRODUCT * NUMBER

Problem Solving with Loops *Property of STI


Page 2 of 18
Data Structures and Algorithms

Problem Solving
with Loops

WHILE / WHILE - END


The first of the three types of loop structures is the
WHILE/WHILE-END structure. This type of loop tells
the computer that while the condition is TRUE, repeat
all instructions between the WHILE and the WHILE-
END. The form of the algorithm is the following:

WHILE <CONDITION(S)>
INSTRUCTION
INSTRUCTION
.
.
.
WHILE-END

Problem Solving with Loops *Property of STI


Page 3 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Flowchart Diagram of WHILE/WHILE-END

Problem Solving with Loops *Property of STI


Page 4 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Decision Equivalent to WHILE/WHILE-END

Problem Solving with Loops *Property of STI


Page 5 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Problem : Create the algorithm and the flowchart to


find the average age of all the students in a class.

Problem Solving with Loops *Property of STI


Page 6 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Repeat/Until
The second type of loop structure is the REPEAT/UNTIL
structure. This type of loop tells the computer to
repeat the set of instructions between the REPEAT and
the until, until a condition is TRUE. The format of the
REPEAT/UNTIL algorithm is the following:

Repeat
Instruction
Instruction
.
.
.
Until <condition(s)>

Problem Solving with Loops *Property of STI


Page 7 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Flowchart Diagram of REPEAT/UNTIL

Problem Solving with Loops *Property of STI


Page 8 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Decision Equivalent to REPEAT/UNTIL

Problem Solving with Loops *Property of STI


Page 9 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Problem : Create the algorithm and the flowchart to


find the average age of all the students in a class.

Problem Solving with Loops *Property of STI


Page 10 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Automatic – Counter Loop


The third type of loop structure is the automatic-
counter loop. This type of loop increments or
decrements a variable each time the loop is repeated.
The form of the algorithm for the automatic counter-
loop is the following :

LOOP:COUNTER = BEGIN TO END STEP S


INSTRUCTION
INSTRUCTION
.
.
.
LOOP-END: COUNTER

Problem Solving with Loops *Property of STI


Page 11 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Decision Equivalent to Automatic-Counter Loop

Problem Solving with Loops *Property of STI


Page 12 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Problem : Create the algorithm and the flowchart to


find the average age of all the students in a class.

Problem Solving with Loops *Property of STI


Page 13 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Nested Loops
Loops can be nested like decisions can. Each loop must
be nested inside the loop just outside it. The general
rules regarding loops, such as where the condition is
processed and how indentation and brackets are used,
hold true for nested loops as well as single loops.

The inner loops do not have to be the same types of


loop structures as the outer loops; that is, a WHILE/
WHILE-END may be nested inside a REPEAT/UNTIL loop,
or vice versa.

Problem Solving with Loops *Property of STI


Page 14 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Nested Loops

Problem Solving with Loops *Property of STI


Page 15 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Problem Solving with Loops *Property of STI


Page 16 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Indicators
Indicators – are logical variables that a programmer
sets within a program to change the processing path
or to control when the processing of a loop should end.

They are sometimes called flags, switches, or trip


values.

Recursion
Another type of loop structure is recursion. Recursion
occurs when a module or a function calls itself.

Problem Solving with Loops *Property of STI


Page 17 of 18
Data Structures and Algorithms

Problem Solving
with Loops

Problem Solving with Loops *Property of STI


Page 18 of 18

You might also like