Difference between revisions of "W1152 While Loop"

From Coder Merlin
Line 5: Line 5:
== General Loop ==
== General Loop ==
[[File:General Loop - No Test.png|thumb|right|link=|General Loop]]
[[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 upon test conditions within 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 in order to inform the CPU when the loop should be exited.  Thus, loops generally have two distinct parts:   
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 in order to inform the CPU when the loop should be exited.  Thus, loops generally have two distinct parts:   
* A '''test condition''' which informs the CPU when the loop should exit
* A '''test condition''' which informs the CPU when the loop should exit

Revision as of 20:26, 5 January 2020

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 upon test conditions within 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 in order to inform the CPU when the loop should be exited. Thus, loops generally have two distinct parts:

  • A test condition which informs the CPU when the loop should exit
  • A body which is the code which 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.

Introduction[edit]

Topic Headers[edit]

Key Concepts[edit]

Exercises[edit]

References[edit]