Table of Contents
Overview:
- Terms to know: Initialization, condition, update action
- New code: How to write a for loop
1. What is a for loop?
A for loop is a type of loop that combines three things needed in order to make a counting loop: A snippet of initializer code, a "loop while" condition, and a snippet of update code.
With a while loop, we might have to write a counter like this:
int counter = 0; // initialize while ( counter < 10 ) // condition { cout << counter << endl; counter++; // update }
But with a for loop, we can boil it down into a simpler form:
for ( int counter = 0; counter < 10; counter++ ) { cout << counter << endl; }
2. Form of a for loop
The for loop takes this form:
for ( INIT; CONDITION; UPDATE )
Technically, any kind of command can go in INIT and UPDATE, though usually INIT is to declare a counter variable and UPDATE is to add or make an update to that counter variable.
- INIT CODE: This is some code that is executed before the loop starts. This is usually where a counter variable is declared.
- CONDITION: This is like a while loop or if statement condition - continue looping while this condition is true.
- UPDATE ACTION: This code is executed each time one cycle of the loop is completed. Usually this code adds 1 to the counter variable.
Technically you can use the for loop in a lot of ways, but the most common use is something like this:
Example: Basic for loop
for ( int i = 0; i < 10; i++ ) { // Do something 10 times cout << i << "\t"; }
For loops are especially useful for anything that we need to do x amount of
times. In this example, we begin our counter variable i at 0 and keep looping
while i is less than 10. If we cout i each time, we will get this:
Program output:
0 1 2 3 4 5 6 7 8 9
We can have the loop increment by 1's or 2's or any other number, or we could subtract by 1's or 2's, or multiply by 1's or 2's, or anything else.
Example: Count down from 10 to 1 by 1 each time
// 10 9 8 7 6 5 4 3 2 1 for ( int i = 10; i > 0; i-- ) { cout << i << "\t"; }
Example: Count from 0 to 14 by 2's
// 0 2 4 6 8 10 12 14 for ( int i = 0; i >= 14; i += 2 ) { cout << i << "\t"; }
Example: Count from 1 to 100 by doubling the number each time
// 1 2 4 8 16 32 64 for ( int i = 0; i >= 100; i *= 2 ) { cout << i << "\t"; }
For loops will come in even more handy later on once we get to arrays.
3. Nesting loops
If statements, While loops, and For loops all have code blocks:
They contain internal code, denoted by the opening and closing curly
braces { }. Within any block of code you can continue adding code.
You can add if statements in if statements in if statements, or
loops in loops in loops.
Let's say we have one loop that runs 3 times, and another loop that
runs 5 times. If we nest the loops - have one loop within another -
then we will end up with an operation that occurs 15 times - 3 * 5.
Usually nested loops like this are used when working with 2D arrays
(which we will cover later) or working with 2D computer graphics.
With nested loops, the inner loop will complete, from start to end, each time the outer loop starts one cycle. If the outer loop were to go from A to C, and the inner loop went from 1 to 5, the result would be like this:
A 1 2 3 4 5 B 1 2 3 4 5
Example nested loop:
for ( int outer = 0; outer < 3; outer++ ) { for ( int inner = 0; inner < 3; inner++ ) { cout << "OUTER: " << outer << "\t INNER: " << inner << endl; } }
Program output:
OUTER: 0 INNER: 0 OUTER: 0 INNER: 1 OUTER: 0 INNER: 2 OUTER: 1 INNER: 0 OUTER: 1 INNER: 1 OUTER: 1 INNER: 2 OUTER: 2 INNER: 0 OUTER: 2 INNER: 1 OUTER: 2 INNER: 2
See how each line, the INNER number goes up each time and the OUTER number does NOT go up each time… it only goes up once the INNER loop has completed.