Difference between revisions of "W1154 For Loop"

From Coder Merlin
Line 7: Line 7:
== Introduction ==
== Introduction ==
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.  The '''for loop''' is a construct which enables us to execute a segment of code a ''defined number'' of times.  It is thus the most appropriate choice when we know in advance how to calculate the number of iterations prior to beginning the loop.  While this construct is not strictly necessary, it provides [[Glossary#Syntactic_Sugar|syntactic sugar]] by clarifying intent.
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.  The '''for loop''' is a construct which enables us to execute a segment of code a ''defined number'' of times.  It is thus the most appropriate choice when we know in advance how to calculate the number of iterations prior to beginning the loop.  While this construct is not strictly necessary, it provides [[Glossary#Syntactic_Sugar|syntactic sugar]] by clarifying intent.
== General Loop ==
[[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'/>


== For Loops ==
== For Loops ==
Line 19: Line 12:




{{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.
}}


== Key Concepts ==
== Key Concepts ==
== Exercises ==
== Exercises ==
== References ==
== References ==

Revision as of 20:25, 5 January 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Roller Coaster Loop

Prerequisites[edit]

Introduction[edit]

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. The for loop is a construct which enables us to execute a segment of code a defined number of times. It is thus the most appropriate choice when we know in advance how to calculate the number of iterations prior to beginning the loop. While this construct is not strictly necessary, it provides syntactic sugar by clarifying intent.

For Loops[edit]

for loops execute for a defined count of iterations, as such, the test condition is implied within the definition. Through each iteration, a loop control variable is adjusted toward its final goal. When that goal is reached, the loop exits.



Key Concepts[edit]

Exercises[edit]

References[edit]