loops Flashcards
loops
allow for some code to be repeated as many times as a programmer desires
initialization
*component needed for a loop
*a value assigned to a variable
comparison
*component needed for a loop
*the value that we want the variable to get to
*the amount of times the loop should occur
update
*component needed for a loop
*the code that changes the value of the variable in order to reach the desired value
for loop syntax
for(initialization; comparison; update)
{
//code to be executed
}
*if the comparison evaluated to true, the code is executed and then the update occurs
*flow: initialization, comparison, loop, update, comparison
*the loop continues until the comparison evaluates as false
nested for loop
a for loop within a for loop
while loop
*can achieve everything a for loop can
*structured differently than a while loop
while loop structure
*initialization occurs outside of the loop
*update occurs within the curly braces
while(comparison)
{
//code to be executed
}
do while loop structure
do
{
//code
//update
} while (comparison);
do while loop initialization
initialization occurs outside the loop