For and Nested Loops
Learn about for loops and nested loops.
We'll cover the following...
For loops
For loops are one of the most common types of loops and can be found in most programming languages. They take the following form:
for (initialization ; condition ; update) {
// do something
}
Three things get set inside the parentheses:
- The initialization code is run before the loop starts and is usually employed to initialize any variables that are used in the loop.
- The condition has to be satisfied for the loop to continue.
- The update code is what to do after each iteration of the loop, and it is typically used to update any values.
This process is illustrated in the following diagram.
Press + to interact
For the Ten Green Bottles song, we would have to initialize the number of bottles to . The condition would be that this number has to be more than zero and the update code would be to reduce the number of bottles by . This is shown in the diagram above.
...