Mod and short-circuit logic

These are notes from 30 September.

Modulus operator

This operator, represented by the % symbol in C++, produces the remainder when dividing. The following video explains it with some examples.

Short-circuit

The relational operators (for comparing two values) are ==, !=, <, >, <=, and >=. The logical operators (which operate on other Boolean expressions) are &&, ||, and !.

The AND && and OR || operators use short-circuit evaluation, which means that if the result can be predicted from the left expression, then the right is just skipped.

For example, consider x == 0 || 13/x == 2:

  • When x is 5, the left side is false. That means we must evaluate the right side. 13/x is 13/5 which is 2 (using integer division). So the right side is true. That makes the whole expression true.

  • What about when x is 0? The left side is true. That’s enough – we can stop evaluating there, because whether the right value was true or false, the result will still be true. Short-circuit evaluation thus avoids division by zero in this case! Evaluating 13/0 would crash the program.

a3sol.cpp

// My solution to A3.
#include <iostream>
using namespace std;
int main()
{
    int month, day, days_in_month;
    cout << "Enter month (1-12): ";
    cin >> month;
    if(month < 1 || month > 12)
    {
        cout << "Invalid month.\n";
        // Could use the following to avoid having to
        // put the rest of the program inside 'else':
        //return 0; // exit program
    }
    else
    {
        switch(month)
        {
            case 2: // 29 days
                days_in_month = 29;
                break;
            case 4: case 9: case 11: case 6: // 30 days
                days_in_month = 30;
                break; 
            default: // 31 days
                days_in_month = 31;
        }
        cout << "Enter day (1-" << days_in_month << "): ";
        cin >> day;
        if(day <= 0 || day > days_in_month)
        {
            cout << "invalid day\n";
            //return 0; // exit program (instead of else block)
        }
        else
        {
            switch(month)
            {
            case 1: cout << "January"; break;
            case 2: cout << "February"; break;
            case 3: cout << "March"; break;
            case 4: cout << "April"; break;
                // etc.
            }
            cout << " " << day;
            switch(day)
            {
                case 1: case 21: case 31: cout << "st"; break;
                case 2: case 22: cout << "nd"; break;
                case 3: case 23: cout << "rd"; break;
                default: cout << "th";
            }          
            cout << "\n";
        } // valid day
    } // valid month    
    return 0;
}

divzero.cpp

#include <iostream>
using namespace std;
int main()
{
    int y;
    cout << "Enter divisor: ";
    cin >> y;
    cout << "13/" << y << " is:\n";
    int x = 13/y; // Fails here when y is zero.
    cout << x;
    cout << "\n";
    return 0;
}