Looping Flashcards
for loops
FOR loop. This type of loop requires a variable that counts how many times the computer
repeats the same set of instructions and then stops after the indicated number of loops. These looping
structures look different in different languages, but follow the same principle. In JavaScript, it looks like this:
for (var counter = 0; counter < 11; counter++)
{
…instructions…
}
while loops
This is usually called a WHILE loop. The
instructions inside the loop will be run repeatedly until the condition becomes false, so be sure that the
instructions do not create an infinite loop. These looping structures look different in different languages,
but follow the same principle. In JavaScript, it looks like this:
while (condition)
{
…instructions…
}
do-while loops
do
{
if (input != “Q”
{
}
}
while(input != “Q”
sentinel values
a special value in the context of an algorithm which uses its presence as a condition of termination,
infinite loops
A loop a continues forever.
loop counters
a variable that counts up
Break
The break command causes an immediate exit out of the loop, while the continue
command skips over all other instructions inside the loop structure but iterates the loop counter and
returns to the beginning of the loop again.
while (condition)
{
if (…)
{
break;
}
…instructions…
}