Conditional Loops Compared

Conditional Loops Compared

Scratch provides two loop constructs that are dependent on a condition for execution. They are similar, yet differ in one crucial aspect—how the condition relates to execution:

  • forever with an immediate enclosed if will only execute the blocks in the loop if the condition is true. Think of it as an if block that keeps occurring. However, if the condition ever fails, then the body of the loop is not executed. Interestingly, the loop still continues to execute, constantly checking the condition; if it ever becomes true again, the body of the loop will execute. Because it never stops, you are unable to attach blocks beneath it. They would never execute.
  • repeat until will default to executing the blocks in the loop repeatedly. The condition is what the loop requires in order to exit—to not repeat the loop. Accordingly, because the loop may terminate, you are able to attach blocks beneath the loop, as they are able to be executed in some conditions.

Put another way, if you were instructed to “Hop on one foot until the clock chimes,” you would continue to hop, stopping only when the condition you are looking for (a clock chime) occurs. However, if you were instructed to “Hop on one foot if the clock chimes, forever,” you would wait until you heard a clock chime before you hopped. Each time the clock chimed, you would hop on one foot. Your instruction is to do that forever, so as long as you are following the instructions, you will continually stop to hop on one foot each time a clock chimes.