Problem Solving With Loops
Problem Solving With Loops
CSC 1106
Introduction to Programming and
Problem Solving | Fundamentals of
Computer Programming
BsCS|BIT
Lecture Outline
Use problem-solving tools when developing a solution using the loop logic
structure.
Use counters and accumulators in a problem solution.
Use nested loop instructions to develop a problem solution.
Distinguish the different uses of three types of loop logic structures.
Walusimbi Hakim |
The Loop Logic Structure
3
A third logic structure for designing decisions is the loop structure, the
loop logic structure is the repeating structure.
Most problems in business involve doing the same task over and over for
The GoTo is an instruction that tells the computer to transfer to another instruction in
the solution instead of processing the next instruction in sequence.
Replacing the GoTo statement in this way increases the readability of the
program.
loop structure.
These include;
statement.
For example,
Counter = Counter + 1 or C = C + 1
When counting by ones, an assignment statement allows the variable on
the left side of the equal sign to be assigned the value of, or to be
replaced by, the resultant of the expression on the right side of the equal
sign.
same variable name is on both sides of the equal sign; the amount the
variable is to be incremented follows the plus sign.
This instruction takes the value of the counter, adds one to it, and then
the loop.
you are accumulating) to the value of another variable, which holds the
sum or total.
In these examples, Sum and TotalSales must be initialized to zero before
For example:
This type of loop tells the computer that while the condition is True, repeat
all instructions between the While and the WhileEnd.
The form of the algorithm is the following:
The symbol used for the While part of the instruction is the diamond, the
symbol for a decision.
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, at which
time the processing will continue with the instruction following the
WhileEnd.
© IUIU – 2021. 10/30/2024
While/WhileEnd
17
We 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.
In those cases, the resultant of the condition is False at the start of the
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; or
When you are calculating test averages for each student in a class.
In the case of calculating test averages, the loop instructions should not be
executed for students who for some reason have no test grades.
To calculate the average you would accumulate and count the test scores
any tests, the computer would try to divide by zero, and an error would
result.
The While/WhileEnd loop would enable the processing to pass over the
Create the algorithm and the flowchart to find the average age of all the
students in a class.
The first instruction initializes the value of the Sum (the total of all ages) to
zero.
The second instruction initializes the value of the counter variable to zero.
Next the first age is entered, an age must be entered before the loop so
valid value for the variable Age in order for the conditions to be true the
first time through the loop.
If there is no primer Read, the value of Age is unknown.
© IUIU – 2021. 10/30/2024
20
The next read is at the end of the loop for two reasons. First, the first
value of Age must be included in the calculations.
Second, the test for the trip value (the value of age that makes the
condition false and signals the end of the list of students) must be just
before the condition, so the trip value will not be calculated as part of
the average age.
The While begins the loop and processes the condition Age When the
resultant is True, the instructions in the loop are processed; when the
resultant is False, the processing continues with the calculation of the
average.
The first loop instruction accumulates the ages by adding each age to the
total, followed by an instruction that counts the students by adding one to
the counter variable.
The final instruction in the loop enters the next age, the processing then
allows the value of Age to control when to stop the looping process and
continue with the rest of the solution.
When the looping process stops, the processing continues at the
calculation of the average age, and finally, the printing of the average
age.
© IUIU – 2021. 10/30/2024
Problem - While/WhileEnd Solution
23
The data for this problem can be entered from the keyboard; the user
enters a 0 when there are no more ages to enter.
Loops are used to iterate repeatedly over a block of code, to change the
way a loop is executed from its usual behavior, we use control statements.
Control statements are used to control the flow of the execution of the
break
and continue.
Based on the given condition, the break statement stops the execution and
brings the control out of the loop.
There can be many cases when a break statement can be used.
certain datatype (int, dict str, etc.) or if it equals a certain value, we can
use the break statement.
The Continue statement is used to skip the current iteration when the
condition is met and allows the loop to continue with the next iteration.
It does not bring the control out of the loop unlike the break statement.