W1153 Repeat-While Loop

From Coder Merlin
Jump to navigation Jump to search
Infinite Loop 1

Prerequisites[edit]

Repeat-While Loop[edit]

A repeat-while loop (which is also sometimes called a do-while loop):

  • Executes the statements within the body of the loop
  • Tests a condition
    • If the condition is true, the statements within the body of the loop are again executed
    • If the condition is false, execution continues with the statements after the body of the loop
ObserveIcon.png
Observe, Ponder, and Journal Section 1
  1. Compare this behavior to that of the while loop. What are the similarities? What are the differences?
  2. What are the minimum number of times that this loop will execute?
  3. What is the purpose of the Jump instruction after the consequent?

Flowchart[edit]

   Repeat-While Loop Flowchart.png

Assembly Language[edit]

Let's consider how the above construct is implemented in assembly language:

   Repeat-While Loop Assembly.png

The statements in the body of the loop are executed. Then, 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. The consequent executes an unconditional jump back to the body of the loop.

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
 9statements:
10        # write(1, messageStatement, 10)
11        mov     $1, %rax                   # system call 1 is write
12        mov     $1, %rdi                   # file handle 1 is stdout
13        mov     $messageStatement, %rsi    # address of string to output
14        mov     $10, %rdx                  # number of bytes
15        syscall                            # invoke operating system to do the write
16
17        # alter loop control variable, one step closer to termination
18        dec     %r8
19
20test:   
21        # we then test to see if the number is greater than 4
22        cmp     $4, %r8                    # subtract 4 from the contents of register r8
23
24        # evaluate test condition, jump conditionally (less-than or equal)
25        jle      alternative
26
27consequent:
28        # jump to statements at beginning of loop
29        jmp     statements                 # jump unconditionally
30
31alternative:
32        # write(1, messageAlternative, 12)
33        mov     $1, %rax                   # system call 1 is write
34        mov     $1, %rdi                   # file handle 1 is stdout
35        mov     $messageAlternative, %rsi  # address of string to output
36        mov     $12, %rdx                  # number of bytes
37        syscall                            # invoke operating system to do the write
38
39afterAlternative:
40        # exit(0)
41        mov     $60, %rax                  # system call 60 is exit
42        xor     %rdi, %rdi                 # we want return code 0
43        syscall                            # invoke operating system to exit
44
45loopControlVariable:
46        .quad    8
47messageStatement:
48        .ascii  "Statement\n"
49messageAlternative:
50        .ascii  "Alternative\n"

Swift[edit]

var x = 8
repeat  {
    print("consequent")
    x -= 1 
} while (x > 4)
print("alternative")
ObserveIcon.png
Observe, Ponder, and Journal Section 2
  1. Are you able to implement the same functionality found in a while loop using a repeat-while loop, without changing the condition or adding a conditional? Explain and provide an example.
  2. Are you able to implement the same functionality found in a repeat-while loop using a while loop, without changing the condition or adding a conditional? Explain and provide an example.

Key Concepts[edit]

KeyConceptsIcon.png
Key Concepts
  • In a repeat-while loop:
    • The statements in the body of the loop are executed
    • The condition is tested
    • If the condition is true, the statements within the body of the loop are again executed
    • If the condition is false, execution continues with the statements after the body of the loop

Exercises[edit]

ExercisesIcon.png
Exercises
  1.  J1153  Create a journal and answer all questions in this experience. Be sure to include all sections of the journal, properly formatted.
  2.  M1153-10  Complete  Merlin Mission Manager  Mission M1153-10

References[edit]


CoderMerlin™ proudly recommends:
HintIcon.png

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!


Designed with pride in Silicon Valley, CA, USA