Chapter 6: Conditionals

Say we had a simple program that displayed your age:

age = 25
print "You are " + age + " years old."

If you ran this program it would display:

You are 25 years old.

Next year you could change the first line to “age = 26” and the program’s output would update appropriately. In fact you could give this program to anyone and they could modify the first line so that the second line would print the right age. Once you turned 30 you might want to modify it like this:

age = 30
print "You are " + age + " years old."
print "Your warranty has expired!"

That would work fine, except that if you now gave it to someone who was 25 they would not only have to modify the first line but remember to remove the third line. But why force a human to remove the third line? The computer should be able to automatically display or not display the third line depending on the value of age. We can use an “if” statement to tell the computer to do just that:

age = 30
print "You are " + age + " years old."
if age > 29:
    print "Your warranty has expired!"

Again let’s “play computer”: when you get to the third line, you look at the word age, realize it’s a variable, and get its value (30) from memory (it was stored there on line 1):

if 30 > 29:

This line says, “If 30 is greater than 29, perform the statements that are indented. If not, skip the indented statements.” Since 30 is greater than 29, the indented statements are performed and the joke is displayed on the screen. If a friend changes the first line to “age = 25”, the third line will compare 25 to 29, and since 25 is not greater than 29, the fourth line will be skipped.

This is a conditional. Whether the fourth line is run is conditional on the math formula on the third line. That formula could be more complex, such as:

if age >= 40 and age < 50:
    print "You are in your forties."

The >= symbol means “greater than or equal to” (normally written as “≥” in math). Here the “print” line won’t get run unless the age is between 40 and 49. You can also put more than one indented statement:

if age >= 50 and age < 60:
    print "You are in your fifties."
    print "Mid-life crisis yet?"

If the person’s age is between 50 and 59, both lines will be displayed; otherwise neither will. Now notice that if the age of the user is 1, the second line of our original program will display a grammatical error:

You are 1 years old.

That should be “year”, not “years”. We can use a conditional to fix this bug. (A bug is an error in the program.)

age = 25
if age == 1:
    print "You are " + age + " year old."
if age != 1:
    print "You are " + age + " years old."

The == means “is equal to”. There are two equal signs in order to differentiate it from an assignment statement, which uses a single equal sign (as in the first line of our program). This is related to what we wrote earlier about computer languages always wanting to be perfectly clear about what’s intended. The != means “not equal to”. Think of the math symbol of the equal sign with a slash through it, “≠”, then imagine the exclamation mark being that slash. Now if you set age to 1 you’ll get this:

You are 1 year old.

Which is correct. If the age is not 1, then the first “print” will be skipped and the second will run, displaying the version of the text with a plural “years”. It’s fairly common to want to do one thing if a condition is true (such as “age == 1”) and another if that condition is not true (“age != 1”). So computer languages have a shortcut to save you from having to retype a whole “if” statement with the inverse condition. You simply write this:

age = 25
if age == 1:
    print "You are " + age + " year old."
else:
    print "You are " + age + " years old."

That means, “If age is 1, run the first print statement. If not (else), run the second print statement.” Again you can have multiple statements in either the first section or the second:

age = 25
if age == 1:
    print "You are " + age + " year old."
    print "You're an infant!"
else:
    print "You are " + age + " years old."
    print "You're not an infant."

Now you can give this program to someone else and they only need to change the first line to get a few lines of accurate text displayed about them. You can imagine that this can get arbitrarily complex, with very sophisticated conditions being tested. Consider this:

age = 25
if age == 1:
    print "You are " + age + " year old."
    print "You're an infant!"
else:
    print "You are " + age + " years old."

    if age < 25:
        print "You're young."
    else:
        print "You're mature!"

This is called a “nested if”. The second “if” (the one that compares age with 25) is nested (within) the first “if”. Since it’s indented, the second “if” will itself only be run if the first condition (“age == 1”) is not true. If age is in fact 1, the first “print” will be run and the entire second section, including the second “if”, will be skipped. So the program will display only one of “You’re an infant!” or “You’re young.” or “You’re mature!”. You should “play computer” through this program with various values of age, such as 1, 5, 25, and 30.

Conditionals are extremely useful. In our example, it would have been possible (though inconvenient) for each person to add or remove “print” statements as they modified the age in the first line, but in reality the value for age would have come from elsewhere, such as a form or a database, and the program would have to run as-is, unmodified, and display the correct text for any value of age that it might get.

We can look at conditionals in C, again to show that basic concepts are available in different programming languages with only superficial differences:

if (age > 29)
    puts("Your warranty has expired!");

Note these differences: there are parenthese around the condition; “print” was replaced with “puts”, as before; the text to display is surrounded by parentheses; and there’s a terminating semicolon at the end of the “puts” statement. If you wanted to display more than one line, you’d have to write it like this:

if (age >= 50 && age < 60) {
    puts("You are in your fifties.");
    puts("Mid-life crisis yet?");
}

Note these two additional differences: the “and” has been replaced with “&&” in the condition; and there are braces around the pair of “puts” statements. In C (and languages derived from C, such as C++, Java, and C#), indentation is ignored, so the “if” statement will assume that only the very next statement will be conditionally run. If you want more than one statement, you have to group them with braces to tell the language that all of these are conditionally run. In our Python-like language above, indentation is used to tell which statements are under the control of the “if” and “else” lines.