Loops
Loops are a way of making code appear "cleaner" and more efficient when repeated code is used.
Parts of a Loop
Kind of loop (e.g.
while
,for
,foreach
)Initialization of a control variable (e.g.
int count = 1;
int i = 0;
etc.) A control variable is a temporary variable used to iterate (increase or decrease) to limit the number of times a loop code runs.Test conditions (What needs to be true?)
Update the control variable (e.g.
i++; i -= 5;
etc.)Action to take (What is in the code block)
Types of Loops
Nested Loops
A loop INSIDE a loop? ? Heck, yeah!
The loops work the same inside other loops, BUT a different control variable will need to be used.
Generally, since i
is often used, nested loops continue the alphabet: j
, then k
, then l
...
Example:
Prints:
Fluffy Fluffy Fluffy Nugget Nugget Nugget Pumpkin Pumpkin Pumpkin
Last updated