Loops

These are notes for 7 October.

More if/else practice

Let’s start, though, by reviewing Quiz 2 (blank or with solutions), which was problematic for many of you. Those who scored 17 or higher (out of 20) mostly got it and just made small mistakes. Those of you with scores in the 10–13 range definitely need additional practice.

Here are some practice problems in the same style. Solutions are available, but definitely try it first.

A4 solution

#include <iostream>
using namespace std;
int main()
{
    int hour, minute;
    cout << "Enter hour (0-23): ";
    cin >> hour;
    cout << "Enter minute (0-59): ";
    cin >> minute;
    
    if(hour < 10) cout << "0";   // leading zero
    cout << hour << ":";
    if(minute < 10) cout << "0"; // leading zero
    cout << minute << " is ";
    
    int hour12;
    if(hour > 12) hour12 = hour - 12;
    else if(hour == 0) hour12 = 12; // midnight hour
    else hour12 = hour;
    
    cout << hour12 << ":";
    if(minute < 10) cout << "0"; // leading zero
    cout << minute;
    
    // AM/PM
    if(hour >= 12) cout << " PM";
    else cout << " AM";
    cout << "\n";
    
    if(minute >= 8 && minute <= 22) {
        cout << "quarter past ";
    }
    if(minute >= 23 && minute <= 37) {
        cout << "half past ";
    }
    if(minute >= 38 && minute <= 52) {
        cout << "quarter to ";
    }
    if(minute >= 38) {
        hour12 = hour12 + 1; // next hour
        if(hour12 > 12) hour12 = 1; // wrap around
    }
    
    switch(hour12)
    {
    case 12:
        if(hour == 12) cout << "noon";
        else cout << "midnight";
        break;
    case 1: cout << "one"; break;
    case 2: cout << "two"; break;
    case 3: cout << "three"; break;
    case 4: cout << "four"; break;
    case 5: cout << "five"; break;
    case 6: cout << "six"; break;
    case 7: cout << "seven"; break;
    case 8: cout << "eight"; break;
    case 9: cout << "nine"; break;
    case 10: cout << "ten"; break;
    case 11: cout << "eleven"; break;
    }
    
    if(hour12 == 12) { } // do nothing for midnight/noon
    else if(hour < 12) cout << " in the morning";
    else if(hour < 17) cout << " in the afternoon";
    else cout << " in the evening";
    cout << "\n";
    return 0;
    
}

While loop syntax

The syntax is pretty much the same as if: the keyword while, with a Boolean expression in parentheses, no semi-colon but instead a block of statements in curly braces:

while( CONDITION )
{
    STATEMENTS;
}

The interpretation is almost the same as if too, with one critical difference. When the condition is false, the body of the loop is skipped (same as if). When the condition is true, the body of the loop is executed (same as if), but then, we go back to the condition to check it again, and repeat.

So here’s a very simple loop where the body never runs – so this program just prints B:

while( 3 == 4 ) // always false
{
    cout << "A";
}
cout << "B";

And here’s another loop where the body always runs – so this program prints infinitely many As… over and over until your battery runs out or the universe implodes or you kill the program. It never prints B.

while( 3 == 3 ) // always true
{
    cout << "A";
}
cout << "B";

The trick to making a useful loop is to use a condition that might be true at the beginning, but eventually becomes false. Here’s a common pattern for a loop that counts:

int x = 1;
while( x <= 6 )
{
    cout << x << " ";
    x = x + 1;
}

The program should output 1 2 3 4 5 6 and then stop.

Factorial example

#include <iostream>
using namespace std;
int main()
{
    int n;
    int k = 1;
    cout << "Enter integer for factorial: ";
    cin >> n;
    cout << n << "! = ";
    // count down to 1
    while( n > 0 )
    {
        k = k * n;
        n = n - 1;
    }
    cout << k << "\n";
    return 0;
}

Sum example

#include <iostream>
using namespace std;
int main()
{   
    // Compute sum of integers from 1..100
    int sum = 0; // accumulator
    int i = 1; // counter
    while( i <= 100 )
    {
        sum = sum + i;
        i = i + 1;
    }
    cout << sum << "\n";
    return 0;
}

Update operators

On Wednesday we also introduced shortcuts for updating a variable. Inside a loop it’s very common to update a counter or an accumulator variable like this:

    sum = sum * i;
    i = i + 1;

So there is are some short-cut operators for applying an arithmetic operation to a variable, and then putting the result back into the same variable. These two lines do the same thing as those above:

    sum *= i;
    i += 1;

Adding or subtracting one from an integer is common enough that there’s an even shorter operator for that:

    i++;  // Update i by adding 1
    x--;  // Update x by subtracting 1