Why use for
loops?:
- more expressive, succinct, and compact when expressing loops that execute a known number of iterations
for
loops convey more intent thanwhile
loops that execute a known number of iterations
How to use for
loops?:
- Things to note:
- any variable used in
expression1
,expression2
, orexpression3
will need to be previously declared expression1
is used to initialize some parameter, typically called an indexexpression2
is a condition that must betrue
for the loop to continue executionexpression3
is is used to alter the value of the parameter initially assigned byexpression1