W1152 While Loop
Prerequisites[edit]
General Loop[edit]
Loops are the general term for executing a defined segment of code zero or more times, where the number of iterations is dependent upon test conditions within the loop.
In the case of a general loop, we can see that the three statements in the figure on the right will be repeated. However, without a test condition this loop would theoretically execute forever and is formally termed an infinite loop. To be useful, a test condition is required in order to inform the CPU when the loop should be exited. Thus, loops generally have two distinct parts:
- A test condition which informs the CPU when the loop should exit
- A body which is the code which is repeated for each iteration of the loop
![]() |
Caution |
Each iteration of the loop must perform some action, albeit slight, to move the loop closer to completion. If this does not occur, the loop would execute an infinite number of times. |
While Loop[edit]
A while loop:
- Tests a condition
- If the condition is true, the statements within the body of the loop are executed, then the condition is re-evaluated
- If the condition is false, execution continues with the statements after the body of the loop
Flowchart[edit]
A flowchart representing the above requirements follows:
Assembly Language[edit]
Let's consider how the above construct is implemented in assembly language:
The condition, the Boolean test, is evaluated. If the test evaluates to false, a jump is executed to the alternative, otherwise execution continues with the consequent. At the end of the consequent, a jump is executed to the Boolean test.
Carefully study the following assembly language example:
1 .global _start
2
3 .text
4 _start:
5 # load the loop control variable
6 mov (loopControlVariable), %r8 # load %r8 from a variable
7
8 test:
9 # we then test to see if the number is greater than 4
10 cmp $4, %r8 # subtract 4 from the contents of register r8
11
12 # evaluate test condition, jump conditionally (less-than or equal)
13 jle alternative
14
15 consequent:
16 # write(1, messageConsequent, 11)
17 mov $1, %rax # system call 1 is write
18 mov $1, %rdi # file handle 1 is stdout
19 mov $messageConsequent, %rsi # address of string to output
20 mov $11, %rdx # number of bytes
21 syscall # invoke operating system to do the write
22
23 # alter loop control variable, one step closer to termination
24 dec %r8
25
26 # jump to test condition at beginning of loop
27 jmp test # jump unconditionally
28
29 alternative:
30 # write(1, messageAlternative, 12)
31 mov $1, %rax # system call 1 is write
32 mov $1, %rdi # file handle 1 is stdout
33 mov $messageAlternative, %rsi # address of string to output
34 mov $12, %rdx # number of bytes
35 syscall # invoke operating system to do the write
36
37 afterAlternative:
38 # exit(0)
39 mov $60, %rax # system call 60 is exit
40 xor %rdi, %rdi # we want return code 0
41 syscall # invoke operating system to exit
42
43 loopControlVariable:
44 .quad 8
45 messageConsequent:
46 .ascii "Consequent\n"
47 messageAlternative:
48 .ascii "Alternative\n"
![]() |
Observe, Ponder, and Journal Section 1 |
|
Swift[edit]
var x = 8
while x > 4 {
print("consequent")
x -= 1
}
print("alternative")
Key Concepts[edit]
![]() |
Key Concepts |
|
Exercises[edit]
![]() |
Exercises |
|