Chapter 3: If statements

If statement

face Josiah Wang

So far, I have focussed on the first “building block” - simple statements.

Let us now move on to the second building block - the selection block.

The selection block

This is a type of compound statement, where you can have multiple simple statements inside the statement.

In our guessing game, I introduced the if statement.

if user_guess == 42:
    print("Correct")

I also introduced the two-way if-else statements.

if user_guess == 42:
    print("Correct")
else:
    print("Incorrect")

if accepts any boolean expression as a condition (e.g. x == 5)

if user_guess > 42:
    print("Hot")

So, technically, you can even do this. But why would you even want to do it?

if True:
    print("I am always True.")
else:
    print("I don't think anyone will ever see me.")