You are on page 1of 33

Programming

The Repetition Structure(Loops/Iteration)


Objectives
• Learn what are loops
• Learn how loops are executed in Flowcharting
• Use looping mechanisms in PHP
Overview
• Programmers use the repetition structure, referred to more simply as a loop, when
they need the computer to repeatedly process one or more program instructions.
• The loop contains a condition that controls whether the instructions are repeated.
• It can either specify the requirement for repeating the instructions or specify the
requirement for not repeating them. The requirement for repeating the
instructions is referred to as the looping condition because it indicates when the
computer should continue “looping” through the instructions.
• The requirement for not repeating the instructions is referred to as the loop exit
condition because it tells the computer when to exit (or stop) the loop.
• Every looping condition has an opposing loop exit condition; one is the opposite of
the other.
Loops
• indicates that one or more instructions need to be repeated until
some condition is met
• Loops through a code block a specified number of time
Examples
Illustration
1.repeat (3 times)
walk forward one complete step
end repeat
2. turn left 90 degrees
3. repeat (2 times)
walk forward one complete step
end repeat
4. turn right 90 degrees.
5. repeat (2 times)
walk forward one complete step
end repeat
6. turn right 90 degrees
7. walk forward one complete step
8. turn right 90 degrees
9. repeat (4 times)
walk forward one complete step
end repeat
10. turn right 90 degrees
11. sit down
1. Which control structures
are used in the algorithm?
2. What will the algorithm
print when the price of the TV
is $2,100?
3. How would you modify the
algorithm so that it can be
used for only the first 10
customers buying a TV?
4. How would you modify the
algorithm so that it allows the
user to also enter the discount
rate and then uses that rate to
calculate the discount
Using a Pretest Loop to Solve a Real-World
Problem
Using Counters and Accumulators
• Some algorithms require you to calculate a subtotal, a total, or an
average. You make these calculations using a repetition structure that
includes a counter, an accumulator, or both.
• A counter is a numeric variable used for counting something, such as
the number of employees paid in a week.
• An accumulator is a numeric variable used for accumulating (adding
together) something, such as the total dollar amount of a week’s
payroll.
Two tasks are associated with counters and
accumulators
• Initializing means to assign a beginning value to the counter or
accumulator. Typically, counters and accumulators are initialized to
the number 0. However, they can be initialized to any number,
depending on the value required by the algorithm. The initialization
task is performed before the loop is processed because it needs to be
performed only once.
• $ctr=0;
Two tasks are associated with counters and
accumulators
• Updating refers to the process of either adding a number to (called
incrementing) or subtracting a number from (called decrementing) the
value stored in the counter or accumulator. The number can be either
positive or negative, integer or noninteger. A counter is always updated
by a constant value—typically the number 1. An accumulator, on the
other hand, is usually updated by a value that varies. Accumulators are
normally updated by incrementing rather than by decrementing. The
assignment statement that updates a counter or an accumulator is
placed in the body of a loop because the update task must be
performed each time the loop body instructions are processed.
• $ctr=$ctr + 1;
More Examples
Loops in PHP
There are three things needed in order for the loop to work.

1. Initialization – this is where a variable is declared to start


the loop counter.
- This is $x = 1 in our example.
- This mean that the $x is the initializer and it is set to start at
1
2. Condition – This is the second part of the loop where we
check if the loop counter still matches a condition. When it
returns false, the loop stops execution of the code block.
-Our condition in the example below is $x <= 5
3. Increment – When the loop ends, this part of the code is
executed once and then
the loop repeats.
-This is where we can do some operations to the loop
counter.
-In our case we increase the value of $x by one with $x++
Which is the initialization, the condition
and the increment?
While loop
Summary
• The for… loop is used to execute a block of a specified
number of times
• While… loop is used to execute a block of code as long as the
set condition is made to be false
• The do… while loop is used to execute the block of code at
least once then the rest of the execution is dependent on the
evaluation of the set condition
Hands-on Activity
• PHP Loops @ https://www.w3schools.com/php/php_looping.asp
PHP while Loop @
https://www.w3schools.com/php/php_looping_while.asp
PHP do while Loop @
https://www.w3schools.com/php/php_looping_do_while.asp
PHP for Loop @
https://www.w3schools.com/php/php_looping_for.asp
PHP Break and Continue @
https://www.w3schools.com/php/php_looping_break.asp
Flowcharting
Create a flowchart that displays the weekly gross pay for any number of
employees. The user will input the number of hours the employee
worked and the employee’s hourly rate. Employees working more than
40 hours receive time and one-half for the hours worked over 40.

Note:
Save work as SurnameFnameFLab_5.8.1 and turn in to Google
Classroom
Programming
• Create a program that displays a multiplication table similar to the one
shown below.

Note:

Save work as SurnameFnameFLab_5.8.2 and


turn in to Google Classroom
References
• Zak, Diane. An Introduction to Programming
• PHP Loops retrieved from
https://www.w3schools.com/php/php_looping.asp
• PHP Loop: For, ForEach, While, Do While [Example] retrieved from
https://www.guru99.com/php-loop.html

You might also like