Statements: Conditionals

Introduction

Often we need to respond to situations or scenarios that occur. In some instances, a certain block of statements should execute, whereas in other situations, another block should execute. In such situations, we use a conditional statement to decide which block of statements to run.


Conditions

A conditional is made up of a structure known as "IF THEN ELSE". Within this structure, we present a condition which is made up of a binary arithmetic expression. If this expression resolves to true (1), then the first block of code is executed. If the expression resolves to false (0), then the next block of code is executed.

Let us see an example of the binary arithmetic statements we might encounter.

if (num % 2 == 0):
    print "This number is even."

if (animal_type == "sheep" and gender == "female" and age > 2):
    print "This animal is an ewe."

if (mark > 80 and mark < 90):
    print "Well done, you got an A- ."

In each of these cases, the if keyword is followed by a bracketed expression. This expression is computed by the computer, and if found to be true, the print statements (in these cases) are executed.


Python

Here is the syntax for an IF THEN ELSE in Python.

if (expression):
    execute
    these
    statements
elif (another_expression):
    then
    execute
    this
else:
    run
    these
    statements

Notice the elif keyword. This is an abbreviation of "else if", meaning that if the first expression is false, then test this next expression. If the next expression is also false, then finally process the else statement.

Also note that the elif and else components of the IF THEN ELSE are optional. So, you can have an if and an else without the elif, and you can have an if and elif without an else, or you can simply have an if on its own. Also note that you can have as many elif's as you want.

Here is an example to further demonstrate the syntax:

function is_even(num):
    if (num % 2 == 0):
        return true
    else:
        return false

function grade(score):
    if (score > 90):
        return "A+"
    elif (score > 80):
        return "A-"
    elif (score > 70):
        return "B"
    elif (score > 60):
        return "C"
    elif (score > 50):
        return "D"
    elif (score > 40):
        return "E"
    else:
        return "F"

IF Shorthand

Sometimes, you want to assign one value to a variable if a condition is true, or a second value to a variable if false. In this case, a useful syntax to make use of is the IF shorthand. This has the following syntax:

variable = value1 if condition else value2

So, if the condition resolves to true, then the variable is assigned value1, otherwise, if the condition resolves to false, then the variable is assigned value2.

Here is an example to further demonstrate the IF shorthand:

is_even = true if (x % 2 == 0) b else 11

Here, if x % 2 == 0 resolves to true, then we know that x is an even number, and so the variable is_even is assigned the boolean value "true". If there is a remainder to the division of x by 2, then the condition will result in false, meaning that x is an odd number, and thus the variable is_even will be assigned the boolean value "false".


Exercises

  1. Write a function which takes your age as an input, and returns "Child" if your age is below 13, "Adolescent" if your age is below 20, "Adult" if the age is below 65, otherwise return "Pensioner".
  2. Now, rewrite that function with the if/elif conditions reversed.
  3. Write a function within two lines which returns true if the number passed to it is greater than 100, or false if this is not the case.

Continue to next section


Comments

comments powered by Disqus