Difference between revisions of "W1152 While Loop"

From Coder Merlin
Line 3: Line 3:
* [[W1151 Conditional and Flow Chart]]
* [[W1151 Conditional and Flow Chart]]


== Background ==
== General Loop ==
(Use when required for background reading)
[[File:General Loop - No Test.png|thumb|right|link=|General 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
<br clear='all'/>
 
{{Caution|
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 ==
== Introduction ==
== Topic Headers ==
== Topic Headers ==

Revision as of 20:25, 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

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]