Difference between revisions of "W1152 While Loop"
(13 intermediate revisions by 3 users not shown) | |||
Line 4: | Line 4: | ||
== General Loop == | == General Loop == | ||
[[File:General Loop - No Test.png|thumb|right|link=|General | [[File:General Loop - No Test.png|thumb|right|link=|General loop]] | ||
Loops are the general term for ''executing a defined segment of code zero or more times'', where the number of iterations is dependent | Loops are the general term for ''executing a defined segment of code zero or more times'', where the number of iterations is dependent on test conditions in 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. | 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 to inform the CPU when the loop should be exited. Thus, loops generally have two distinct parts: | ||
* A '''test condition''' | * A '''test condition''' that informs the CPU when the loop should exit | ||
* A '''body''' which is the code | * A '''body''' which is the code that is repeated for each iteration of the loop | ||
<br clear='all'/> | <br clear='all'/> | ||
{{Caution| | {{Caution| | ||
Each iteration of the loop '''must''' perform some action, albeit slight, to move the loop closer to completion. | 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 == | |||
A '''while''' loop: | |||
* Tests a condition | |||
* If the condition is true, the statements within the body of the loop are executed, and then the condition is re-evaluated | |||
* If the condition is false, execution continues with the statements ''after'' the body of the loop | |||
== Flowchart == | |||
A flowchart representing the above requirements follows: | |||
{{ResponsiveImage|[[File:While Loop Flowchart.png]]}} | |||
== Assembly Language == | |||
Let's consider how the above construct is implemented in assembly language: | |||
{{ResponsiveImage|[[File:While Loop Assembly.png]]}} | |||
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: | |||
{{CodeExplorer | |||
|exerciseID=1 | |||
|height=800 | |||
|language=assembly | |||
|initialCode= | |||
.global _start | |||
.text | |||
_start: | |||
# load the loop control variable | |||
mov (loopControlVariable), %r8 # load %r8 from a variable | |||
test: | |||
# we then test to see if the number is greater than 4 | |||
cmp $4, %r8 # subtract 4 from the contents of register r8 | |||
# evaluate test condition, jump conditionally (less-than or equal) | |||
jle alternative | |||
consequent: | |||
# write(1, messageConsequent, 11) | |||
mov $1, %rax # system call 1 is write | |||
mov $1, %rdi # file handle 1 is stdout | |||
mov $messageConsequent, %rsi # address of string to output | |||
mov $11, %rdx # number of bytes | |||
syscall # invoke operating system to do the write | |||
# alter loop control variable, one step closer to termination | |||
dec %r8 | |||
# jump to test condition at beginning of loop | |||
jmp test # jump unconditionally | |||
alternative: | |||
# write(1, messageAlternative, 12) | |||
mov $1, %rax # system call 1 is write | |||
mov $1, %rdi # file handle 1 is stdout | |||
mov $messageAlternative, %rsi # address of string to output | |||
mov $12, %rdx # number of bytes | |||
syscall # invoke operating system to do the write | |||
afterAlternative: | |||
# exit(0) | |||
mov $60, %rax # system call 60 is exit | |||
xor %rdi, %rdi # we want return code 0 | |||
syscall # invoke operating system to exit | |||
loopControlVariable: | |||
.quad 8 | |||
messageConsequent: | |||
.ascii "Consequent\n" | |||
messageAlternative: | |||
.ascii "Alternative\n" | |||
}} | |||
{{Observe|Section 1| | |||
# Why is the test executed ''before'' the loop? | |||
# Compare this diagram to that of the conditional. What are the similarities? What are the differences? | |||
# What is the purpose of the ''Jump'' instruction after the ''consequent''? | |||
}} | |||
== Swift == | |||
{{CodeExplorer | |||
|exerciseID=3 | |||
|height=100 | |||
|language=swift | |||
|initialCode= | |||
var x = 8 | |||
while x > 4 { | |||
print("consequent") | |||
x -= 1 | |||
} | |||
print("alternative") | |||
}} | }} | ||
== Key Concepts == | == Key Concepts == | ||
{{KeyConcepts| | |||
* '''Loops''' are the general term for executing a defined segment of code zero or more times. | |||
** A loop generally requires a '''test condition''' that informs the CPU when the loop should exit. | |||
** A '''body''' which is the code that is repeated for each '''iteration''' (repetition) of the loop. | |||
** Each iteration of the loop ''must'' perform some action to move the loop closer to completion. | |||
* An '''infinite loop''' would theoretically execute forever, often because it lacks a test condition or the condition will never evaluate to the value required for the loop to exit. | |||
* In a '''while loop''': | |||
** 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. | |||
}} | |||
== Exercises == | == Exercises == | ||
{{Exercises| | |||
# {{Assignment|J1152}} Create a journal and answer all questions in this experience. Be sure to include all sections of the journal, properly formatted. | |||
# {{MMMAssignment|M1152-10}} | |||
}} | |||
== References == | == References == |
Latest revision as of 21:14, 8 February 2023
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 on test conditions in 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 to inform the CPU when the loop should be exited. Thus, loops generally have two distinct parts:
- A test condition that informs the CPU when the loop should exit
- A body which is the code that is repeated for each iteration of the loop
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, and 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:
- Why is the test executed before the loop?
- Compare this diagram to that of the conditional. What are the similarities? What are the differences?
- What is the purpose of the Jump instruction after the consequent?
Swift[edit]
Key Concepts[edit]
- Loops are the general term for executing a defined segment of code zero or more times.
- A loop generally requires a test condition that informs the CPU when the loop should exit.
- A body which is the code that is repeated for each iteration (repetition) of the loop.
- Each iteration of the loop must perform some action to move the loop closer to completion.
- An infinite loop would theoretically execute forever, often because it lacks a test condition or the condition will never evaluate to the value required for the loop to exit.
- In a while loop:
- 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.
Exercises[edit]
- J1152 Create a journal and answer all questions in this experience. Be sure to include all sections of the journal, properly formatted.
- M1152-10 Complete Merlin Mission Manager Mission M1152-10.