W1153 Repeat-While Loop

From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
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
ObserveObserveIcon.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:

        .global _start

        .text
_start:
        # load the loop control variable
        mov     (loopControlVariable), %r8  # load %r8 from a variable


statements:
        # write(1, messageStatement, 10)
        mov     $1, %rax                   # system call 1 is write
        mov     $1, %rdi                   # file handle 1 is stdout
        mov     $messageStatement, %rsi    # address of string to output
        mov     $10, %rdx                  # number of bytes
        syscall                            # invoke operating system to do the write

        # alter loop control variable, one step closer to termination
        dec     %r8

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:
        # jump to statements at beginning of loop
        jmp     statements                 # 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
messageStatement:
        .ascii  "Statement\n"
messageAlternative:
        .ascii  "Alternative\n"

Swift[edit]

var x = 8
repeat  {
    print("consequent")
    x -= 1 
} while (x > 4)
print("alternative")
ObserveObserveIcon.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]

Key ConceptsKeyConceptsIcon.png
  • 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]

ExercisesExercisesIcon.png
  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]