Skip to content

Iterative Program Flow

Controlling the flow of a program from within the program is a big part of Turing initial General Purpose computer. It allows a program to execute in various orders, based on internal changes and external data

Key Concepts

Iterative FlowRepeating a section of code more than once, often with different data each time
Repeating code blocksDuring execution a program can execute the same code block repeatedly
Knowing when to stop iteratingIn order to know when to stop, a control variable is used to track iterations and is checked each cycle. If the variable reaches a preset value, the loop terminates
Terms
TermMeaning
Code BlockSet of instructions together in a section of a program. Code blocks are instructions that together accomplish a function of the program.
ConditionBoolean value set by data or user input. Can be multiple values that are all logically compared into a single True/False result.
InstructionSingle executable line of code in a program. Includes OpCodes and Operands.

Introduction

Beyond executing sequential and conditional instructions, computers reliably execute the same set.

The Condition

Conditional and Iterative constructs use a condition to decide to change program flow. The condition is set prior to the conditional or iterative code. It may be set by data from previous instructions, user inputs, or data form other programs or computers.

The Condition is the result of comparing data with another data items, or to a range of values. The condition typically a Boolean result of a true/false.

Example Comparisons
  • data is the same as a single value x = 17
  • data is larger that a value x > 16
  • data within a range of values x > 16 && x < 87

In each case, the comparison is either true or false

With the Condition evaluated, the program will execute or skip certain code if the condition is true. Otherwise, the program will continue executing in a sequential flow

Iterative Flow

A program can execute a section of code multiple times. This is common in arithmetic function and database processing.

The number of iteration (number of times the section runs) can be a built-in value or based on user or external inputs.

Iterative Flow Diagram

Iterative loops must have some control criteria to determine when to stop looping. This is typically a data element that tracks the loop cycles and reaches a value that indicates the loop should terminate.

For Loop

Iteration using a For Loop is doing the work a set number of times. The work can use the number of times the loop has been run as a reference. An example is using the count as an array index

For Loop Example

java
int x = 0;
for(int i = 0; i < 10; i++) {
  x += i; //Add current value if i to x
}
asm
;Updated 11/3/2022
.ORIG x3000
  AND R0, R0, #0  ;Load x with 0
  AND R1, R1, #0  ;Load i with 0

  ; for(i = 0; i < 10; i++)
  ForLoop 
          ADD R0, R0, #1    ;Work: x += 1
          ADD R1, R0, #-10  ;Test to loop again...Update i with x - 10
          BRn ForLoop       ;Loop again if i is negative
                            ;Fall through to Done if i is 0 or positive
  Done HALT
.END
Breakdown of code

i is the loop control variable that hold the number of times the loop and executed

The for loop:

  1. initializes loop control variable to 0 (int i = 0)
  2. Established the exit criteria (i < 10)
  3. Increments loop control each time through the loop

i is initially set to zero (0) i is tested against 10. As long as it is less than 10 the loop will continue i is incremented by one (1) each time the loop completes

Each time the loop executes, the value in i is added to x

The first time through i is zero. The second time i is 1, then 2, 3, ... The loop runs 10 times, while i < 10

So x will contain 45 when the loop exits

While Loop

Rather than looping a set number of times, a While Loop will loop as long as a condition is true.

Iterating until a condition is met (true) causes a While Loop to execute zero (0) or more times. The number of iterations depends on the condition and when it is finally met.

A while loop much update the condition variable inside the loop, so that when tested at the start of the next iteration it may make the condition true, and exit the loop.

While Loop Example

java
//Condition: Keep looping until i is 10 or more
//Action: Starting at 0, add 2 to i each loop

int i = 0; //data to update in the loop
while(i <= 10){
  i += 2; //Add 2 to the data
}
asm
;Updated 11/5/2022
.ORIG x3000
  ADD R1, R1, #0 ; int i = 0
  ADD R2, R2, #0 ; temp to use for comparing i with exit condition

;Condition: Keep looping until R1 is 10 or more
;Action: Starting at 0, add 2 to R1 each loop
WhileLoop   
  ADD R2, R1, #-10 ; Test R1 <= 10. Store in R2 so we don't affect the data
  BRp Done        ; R1 + (-10) is positive, jump out of loop
  ADD R1, R1, #2   ; Work: Add 2 to the data
  BRnzp WhileLoop  ; Go back and possible loop again

  Done HALT
.END
Breakdown of code

i is both the data to be acted on and the condition variable used to control the loop

It is possible to use a different variable for controlling the while loop, however, this makes it function for like a For Loop

The While Loop starts by checking the control variable against a logical operations. The result will be true or false. If true, the loop in entered. If false the loop is bypassed.

While loops will execute zero (0) or more times. If the condition is true at the start, it will not be entered

In the loop, the condition variable is modified. Other things can also occur within the loop, however updating the condition variable must occur each time

The loop iterated back to the start, where the condition is re-evaluated and the decision to loop again or bypass the loop is made

The cycle continues until the condition is false

Do-While Loop

Unlike the While loop, a Do-While will complete the loop one (1) time before checking the condition. The condition test is at the bottom of the loop.

This is useful when the work performed in the loop must be, such as in processing a user action. Also useful when the work performed in the loop changes the condition test.

Do-While Loop Example

java
//Condition: Keep looping until i is 10 or more
//Action: Starting at 0, add 2 to i each loop

int i = 0; //data to update in the loop
do {
  //Code to do 'work'
  i += 2; //Add 2 to the data
} while(i <= 1)
Breakdown of code

i is both the data to be acted on and the condition variable used to control the loop

It is possible to use a different variable for controlling the do-while loop, however, this makes it function for like a For Loop

The Do-While Loop starts by doing the work for the first cycle. It then checks the control variable against a logical operations. The result will be true or false. If true, the loop executes again. If false the loop is ended.

Do-While loops will execute one (1) or more times. If the condition is true at the start, it will not be entered

This is their primary different between While and Do-While loops

In the loop, the condition variable is modified. Other things can also occur within the loop, however updating the condition variable must occur each time

The loop iterated back to the start, where the work is preformed again before updating the loop counter and re-evaluating decision to loop again or bypass the loop is made

The cycle continues until the condition is false

asm
.ORIG x3000
  ;Updated 11/5/2022
  ADD R1, R1, #0; int i = 0
  ADD R2, R2, #0; temp to use for comparing i with exit condition

;Condition: Keep looping until R1 is 1 or more
;Action: Starting at 0, add 2 to R1 each loop
myLoop   
  ADD R1, R1, #2   ; Work: Add 2 to the data
  ADD R2, R1, #-2  ; Test R1 <= 1. Store in R2 so data is not affected
  BRnz Done        ; R1 + (-2) is zero or negative, jump out of loop
  BRnzp myLoop     ; Go back and loop again

  Done HALT
.END

Conclusion

One or more instructions are executed more that one time. Each time the instructions are executed, data can be updated to new values.

Iterative constructs use a Control value to mange the looping and is used to decide when to exit the loop

The contents of this E-Text were developed under an Open Textbooks Pilot grant from the Fund for the Improvement of Postsecondary Education (FIPSE), U.S. Department of Education. However, those contents do not necessarily represent the policy of the Department of Education, and you should not assume endorsement by the Federal Government.
Released under Creative Commons BY NC 4.0 International License