For loops

Table of Contents


* For loops

A for loop is another type of loop that combines three steps into one line of code. A for loop looks like this:

for ( INIT_CODE ; CONDITION ; UPDATE_ACTION )
{
}

Technically you can use the for loop in a lot of ways, but the most common use is something like this:

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:

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.


1. Nesting code blocks

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.

1.1. Nesting if statements

Nesting an if statement within another if statement basically gives you a boolean expression with an AND.

Example:

if ( wantsBeer )
{
  if ( age >= 21 )
  {
    GiveBeer();
  }
}

Equivalent logic:

if ( wantsBeer && age >= 21 )
{
  GiveBeer();
}

Whether you implement some logic with nested if statements, or with if / else if statements using AND operations is a matter of design preference- in some cases, one might be cleaner than the other, but not always.

If you have a statement like this:

if ( conditionA )
{
  if ( conditionB )
  {
    Operation1();
  }
  else
  {
    Operation2();
  }
}

It could be equivalently described like this:

if ( conditionA && conditionB )
{
  Operation1();
}
else if ( conditionA && !conditionB )
{
  Operation2();
}

1.2. Nesting 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 \times 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
C	1	2	3	4	5

Example:

for ( int outer = 0; outer < 3; outer++ )
{
  for ( int inner = 0; inner < 5; inner++ )
  {
    cout << "OUTER: " << outer
      << "\t INNER: " << inner << endl;
  }
}

Output:

OUTER: 0	 INNER: 0
OUTER: 0	 INNER: 1
OUTER: 0	 INNER: 2
OUTER: 0	 INNER: 3
OUTER: 0	 INNER: 4
OUTER: 1	 INNER: 0
OUTER: 1	 INNER: 1
OUTER: 1	 INNER: 2
OUTER: 1	 INNER: 3
OUTER: 1	 INNER: 4
OUTER: 2	 INNER: 0
OUTER: 2	 INNER: 1
OUTER: 2	 INNER: 2
OUTER: 2	 INNER: 3
OUTER: 2	 INNER: 4

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.

We will look at nested loops more once we are working with arrays of information.


Author: Rachel Wil Sha Singh

Created: 2023-10-17 Tue 16:02

Validate