Module 6: Loops Flashcards
for loop
A typical looping construct designed to make it easy to repeat a section of code using a counter variable. The for loop combines the creation of a variable, a Boolean looping condition, and an update to the variable (increment or decrement) in one statement.
while loop
programming construct used to repeat a set of commands (loop) as long as (while) a boolean condition is true. Will check if the condition is true before entering the body.
do-while loop
repeatedly executes a block of statements until a specified Boolean expression evaluates to false. Will always execute the code at least once before checking if the condition is true.
enhanced for loop (for-each loop)
a for loop that iterates through each element in an array or Collection
for loop syntax
for ( int - variable you want to create for loop; condition; updates variable being used in condition ++ or - -)
{
statement(s);
}
while loop syntax
while (condition)
{
statement(s);
}
enhanced for loop (for-each loop) syntax
for(data_type variable : array | collection){
//body of for-each loop
}
do while loop syntax
do
{
statement(s);
} while (condition);
What is needed for a nested loop?
You can use one statement inside another statement.
for ( initialization; condition; increment) {
for ( initialization; condition; increment) {
// statement of inside loop
}
// statement of outside loop
}