THE ‘IF’ STATEMENT

Up to this point we have learned how to collect basic data from the user, but wouldn’t it be useful if we could do different things depending on what the user typed in? Well, this happens to be a very core concept of computer programming, and we can do exactly as previously described with these things called ‘if’ statements. These are basic statements which allow us to do certain things only in certain conditions.

The first thing we’re going to learn about is the ‘if‘ itself. Just write the if keyword and then in some brackets, the condition. To specify the condition you simply write one value (either a variable or constant), then the comparison operator you want to compare them with (for example – equal to, which is ==) and then the second value (either a variable or constant). We then put some curly brackets, and anything inside the curly brackets is what will be executed if the condition is true. You might have gotten a bit lost in that explanation. Let’s take a look at an example.

The following would compare 1 to 1 (which is a bit silly, but gives an example which is obviously always true):

[python]
if (1 == 1):
#Stuff to do if the condition is true (which in this case, it always is! 1 is always 1!)
print("The condition is true")
[/python]

Note that the value that you are comparing the second thing to must match the type of the first thing – for example, if comparing a string you must either compare to a string variable, or to a string constant (and remember that string constants are always shown in double or single quotes).

Another thing to note is that the code you want to execute must be indented AFTER the condition and you must end the condition with a colon (:). A basic if statement would look like the following:

[python]
if (condition):
#statements to execute if true
[/python]

In our first example, however, one always equals one, so it’s not much of a condition — we can use variables to actually create a somewhat useful condition:

[python]
age=int(input("Please enter your age: ")) #must convert the value to an integer for later comparison
if (age==16):
print("Wow, I’m 16 too!")
[/python]

In this case the program would output “Wow, I’m 16 too!” if the user entered the value 16, but would not output anything if the user inputted any other number. We can also compare any two variables using the same method:

[python]
age=int(input("Please enter your age: "))
height=int(input("Please enter your height: "))

if (age == height):
print("Your age and height are the same!")
[/python]

The issue we have at the moment, is that in most programs we aren’t always going to want to just check if something is equal to something else. What if we wanted to check if something was less than, or greater than, or not equal to something else? Well luckily there are other comparison operators we can use instead of just being restricted to the “is equal to” operator (==). “Less Than” (<) and “Greater Than” (>) are relatively simple – they are simply their usual symbols, and so we could check if the user’s height is greater than their age like this:

[python]
age=int(input("Please enter your age: "))
height=int(input("Please enter your height: "))

if (height > age):
print("Your height is greater than your age!")
[/python]

We can also do “Greater Than or Equal To” and “Less Than or Equal To” by simply adding a single equals sign after the appropriate symbol. For example, we could check if the user’s height was less than or equal to their age like this:

[python]
age=int(input("Please enter your age: "))
height=int(input("Please enter your height: "))

if (height <= age):
print("Your height is less than or equal to your age!")
[/python]

There are also the simple “equal to” and “not equal to” conditional operators. We already know “equal to” as ==, and “not equal to” is an exclamation mark followed by an equals sign: !=. So we could check if the user’s height doesn’t equal their age like so:

[python]
age=int(input("Please enter your age: ")
height=int(input("Please enter your height: ")

if (height != age)
print("Your height doesn’t equal your age!")
[/python]

You Try

Copy the code above and execute it. Does it create the result you expected? Try changing it up to see what output is created.

Check Your Understanding

Before moving to the next section, let’s see how well you know the current material.

  1. What is the difference between the following code segments:
    [python]
    if (age=16) ….
    if (age==16) ….
    if (age&gt;16) ….
    if (age&lt;16) ….
    if (age&gt;=16) ….
    if (age&lt;=16) ….
    if (age!=16) ….
    [/python]
  2. Determine the output of the following code segment:
    [python]
    birthMonth = 11
    if (birthMonth == 10):
    print("You were born in October")
    [/python]
  3. Determine the output of the following code segment:
    [python]
    birthMonth = 11
    if (birthMonth == 10):
    print("You were born in October")
    [/python]
    Why does the output differ from #2?