Pinball Game Machine

Iffing and Elsing

Code operation typically varies according to both the incoming data and the decision flow that responds to it. So let’s dig into conditional statements. These define blocks of code which will run only if a particular condition is met.

PHP Code Example:

In this example, we set up four possible conditions. First of all we generate a number between 0 and 100 and assign the result to a variable. A conditional clause is run according to tests that determine whether the integer is divisible by 4, 3, or 2. Finally an else clause activates if no other conditions are matched.

$num = rand(0, 100);

if ($num % 4 == 0) {
    print "$num is divisible by 4\n";
} else if ($num % 3 == 0) {
    print "$num is divisible by 3\n";
} else if ($num % 2 == 0) {
    print "$num is divisible by 2\n";
} else {
    print "$num is not divisible by 4, 3, or 2\n";
}

The output, of course, will vary according to the value of $num but it will look something like this:

7 is not divisible by 4, 3, or 2

Python Code Example:

Here is the equivalent code in Python.

import random

num = random.randint(0,100)

if num % 4 == 0:
    print(f"{num} is divisible by 4")
elif num % 3 == 0: 
    print(f"{num} is divisible by 3")
elif num % 2 == 0:
    print(f"{num} is divisible by 2")
else:
    print(f"{num} is not divisible by 4, 3, or 2")

Barring typos in the source, the output should be identical.

Discussion

As you can see, the language differences are minimal at core. Of course the mechanism for generating a random number differs. We will cover that in a future article.

Note that the logic in both cases short-circuits so that once a condition is met, subsequent conditions will not be tested. So, if our integer is divisible by 4, that statement will be run and the remaining else if or elif clauses will not be evaluated, whether or not the condition would have evaluated truthily.

The principle differences here lie in syntax. Python, as you’d expect, uses indentation rather than braces to define its blocks. Python’s conditional statements do not require parentheses around their test expressions. Note the colon after the test expression and that the Python equivalent to PHP’s else if (or less common but equally valid elseif) is elif.

Python’s strict indentation rules apply here so you must align align elif and else with your initial if statement. You can’t get away with a kludged up structure like this.

if ($num == 0) { print "lowest\n"; } else if ($num == 100) { print "highest\n"; }

For the sake of readability, this is arguably a Very Good Thing. On the other hand, you can place the statement code on the same line as the test expression like this:

if num == 0: print("lowest")
elif num == 100: print("highest")

However, again for the sake of readibility, you might think carefully before doing it.

See also

Further Reading:


Stay up to date with the Python for PHP Programmers project


Yes! Please sign me up to this newsletter


Photo by Sigmund on Unsplash