W1152 While Loop

From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Infinite loop BSOD

Prerequisites[edit]

General Loop[edit]

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 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


CautionWarnIcon.png

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:

   While Loop Flowchart.png

Assembly Language[edit]

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

   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:

CoderMerlin™ Code Explorer: W0000 (1) 🟢


ObserveObserveIcon.png
Observe, Ponder, and Journal: Section 1
  1. Why is the test executed before the loop?
  2. Compare this diagram to that of the conditional. What are the similarities? What are the differences?
  3. What is the purpose of the Jump instruction after the consequent?

Swift[edit]

CoderMerlin™ Code Explorer: W0000 (3) 🟢


Key Concepts[edit]

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

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

References[edit]