If Statements

In this week’s post, we will be learning how to properly use something called an if statement.  An if statement is used to test a condition, and then execute some code based on what that condition was.  Here is an example of an if statement that shows what a computer does when an if statement is present:

In the example, a is less than b, therefore the first bit of code is executed, and the second one was not.  However, if both of these variables were equal to the same number, neither of the two would execute, because if the numbers are both the same, neither of them is greater.

Like in math, you can also test to see if a number is greater than or equal to/less than or equal to another number (usually denoted by  and  in math, but not in programming).  To test for this, you must use either <= (less than or equal to) or >= (greater than or equal to).  These symbols must be typed out in that exact order, NOT => or =<.  It would be used like this:

example if

To test if a variable is equal to another variable, the symbol == would be used.  To test if a variable is not equal to another, you would use !=.  It is very easy to forget to use two equal signs, but it is necessary or else your program will not execute.  When declaring a variable, use =, but when testing a condition, you must use ==.

Indentation

To make it easier to explain, I have taken information from programarcadegames.com to explain how to properly indent with if statements:

Indentation matters. Each line under the if statement that is indented will only be executed if the statement is true:

1
2
3
4
5
ifa ==1:
    print("If a is one, this will print.")
    print("So will this.")
    print("And this.")
print("This will always print because it is not indented.")

Indentation must be the same. This code doesn’t work.

1
2
3
4
ifa ==1:
  print("Indented two spaces.")
    print("Indented four. This will generate an error.")
   print("The computer will want you to make up your mind.")

Once an if statement has been finished, it is not possible to re-indent to go back to it. The test has to be performed again.

1
2
3
4
5
ifa ==1:
    print("If a is one, this will print.")
    print("So will this.")
print("This will always print because it is not indented.")
    print("This will generate an error. Why it is indented?")

And/Or

If statements can also test more than one condition, by using the and and or operators.  These can be used like this:

example and or

You must repeat your variable every time you test it against a new variable.  For example, if you wanted to test if x is less than y and z, you would have to say:

if x < y and x < z:

and NOT:

if x < y and < z:

The computer does not know to check if you do not directly tell it to, so you must repeat yourself when it comes to variables.

 

Booleans

Booleans are not always an easy concept to grasp, so once again, I will explain using the words of programarcadegames.com:

Boolean variables can store either a True or a value of False. Boolean algebra was developed by George Boole back in 1854. If only he knew how important his work would become as the basis for modern computer logic!

An if statement needs an expression to evaluate to True or False. What may seem odd is that it does not actually need to do any comparisons if a variable already evaluates to True or False.

If statements and Boolean data types
1
2
3
4
# Boolean data type. This is legal!
a True
if a:
    print("a is true")

Back when I was in school it was popular to say some false statement. Wait three seconds, then shout “NOT!” Well, even your computer thinks that is lame. If you are going to do that, you have to start with the not operator. The following code uses the not to flip the value of a between true and false.

The not operator example 1
1
2
3
# How to use the not function
if not(a):
    print("a is false")

Because not is an operator and not a function, the parentheses aren’t necessary. This is also legal:

The not operator example 2
1
2
3
# How to use the not function
if not a:
    print("a is false")

It is also possible to use Boolean variables with and and or operators.

Using “and” with Boolean variables
1
2
3
4
a True
b False
if a and b:
    print("a and b are both true")

It is also possible to assign a variable to the result of a comparison. In the code below, the variables a and b are compared. If they are equal, c will be True, otherwise c will be False.

Assigning values to Boolean data types
1
2
3
4
5
6
7
8
a 3
b 3
# This next line is strange-looking, but legal.
# c will be true or false, depending if
# a and b are equal.
c a =b
# Prints value of c, in this case True
print(c)
Zero means False. Everything else is True.

It is possible to create an if statement with a condition that does not evaluate to true or false. This is not usually desired, but it is important to understand how the computer handles these values when searching for problems. The statement below is legal and will cause the text to be printed out because the values in the if statement are non-zero:

1
2
3
4
if 1:
    print("1")
if "A":
    print("A")

The code below will not print out anything because the value in the if statement is zero which is treated as False. Any value other than zero is considered true.

1
2
if 0:
    print("Zero")

In the code below, the first if statement appears to work. The problem is that it will always trigger as true even if the variable a is not equal to b. This is because b by itself is considered true.

1
2
3
4
5
6
a "c"
if a ="B" or "b":
    print("a is equal to b. Maybe.")
# This is a better way to do the if statement.
if a ="B" or a ="b":
    print("a is equal to b.")

Now that you know the basics of if statements, you can experiment and try out your own programs.  Next week, we will be going over more advanced comparisons using else and elif functions, as well as comparing text variables!

Leave a comment