top of page

If Operations

Checkpoint #1

Whitespace

Elif

Checkpoint #2

Nested If

Summary

If Operations

While Booleans maybe didn't seem that important before, they can be really useful in constructs called If Statements. If Statements allow you to establish instructions throughout your code; they create decision points so that a block of code is only executed if a certain condition is true.

If Statements.png

This is a flowchart of what happens in your code that contains an If Statement. Notice how it can execute two different paths, depending on whether a condition is true or false.

If Statements usually contain a conditional statement that can be evaluated as either True or False (Booleans!). There are six main operators used in the conditional parts of if statements:

>

<

>=

<=

!=

==

Greater than

Less than

Less than or equal to

Greater than or equal to

Not equal to

Equal to

Let's try a simple example:

1 age = 16

2 if age >= 18: # Here ">=" means greater than or equal to

3     print("You are eligible to vote in the United States")

4 if age < 18: # Here "<" means less than

5     print("Sorry you cannot vote (yet)!")

Can you predict the output of the above code? Type it below to check your answer!

bottom of page