Masthead

Boolean Logic and If Statements

1. Introduction

Boolean values can be either “true” or “false”.  In computer programming we will also refer to true as “1”.  We also refer to false as “0”.  This is the fundamental number system of digital computers and also an area of philosophy, logic.  The computer includes logical functions in addition to arithmetic functions.  All the operators are shown in the table below.  We will use logic heavily with “if” statements. 

Historical Note
Boolean values are named after George Boole, a British mathematician and philosopher who invented Boolean algebra in the 1800s. 

2. Comparisons

The most common use of Boolean values is in comparisons with are used heavily with “if” statements.  The following table shows the primary comparison operators.  Each of these operators will return a Boolean value (true or false().  Note that some of these operators will work with strings and numbers while others only work with numbers.

== (2 equal signs)

Equal

Strings and numbers

<> 

Not equal

Strings and numbers

!=

Not equal

Strings and numbers

Less than

Numbers only

Greater than

Numbers only

<=

Less then or equal

Numbers only

>=

Greater than or equal

Numbers only

 

Note: Take care with the "Equal" comparison and remember that is has 2 equal signs. One equals will "assign" a value to a variable. Confusing these two is the most common programming error.

Note: There are two ways of checking if two values are "not equal" in Python. I use "!=" because this is used in other similar programming languages. Some languages use "<>" and so do SQL databases. It's up to you which to use but I recommend picking one and staying with it.

Below is a table with some example comparisons and their results.

Equation

Value of x

x = 1 == 1

true

x = 1 == 2

false

x = 1 <> 1

false

x = 1 <> 2

true

x = 1 < 0

false

x = 1 > 0

true

x = 1 <= 0

false

x = 1 >= 0

true

x = “hi” == “hi”

true

x = “hi” == “bye”

false

x = “hi” <> “hi”

false

x = “hi” <> “bye”

true

 

3. “If” statements

"If" statements allow us to execute one block of code if a "comparison" is true. As a simple examples let's take our random number example from before and then print out when the number is greater than 50. This is done by using the word "if" followed by a condition in parenthesis. We also have to add a colon after the parenthesis.

import random
x=0
while (x<10):
 y=random.randint(1,100) 
 print(y)
 if (y>50): print("Number is over 50")
 x+=1

If we want to have more than one line of code "inside" the "if" statement we just go to the next line and add another space:

import random

x=0
while (x<10):
 y=random.randint(1,100) 
 print(y)
 if (y>50): 
  Message="Number is over 50"
  print(Message)
 x+=1

Sometimes you'll want to do one thing when the "if" statement is true and another when it is false:

import random

x=0
while (x<10):
 y=random.randint(1,100) 
 print(y)
 if (y>50): 
  Message="Number is over 50"
 else:
  Message="Number is equal to or less than 50"
 print(Message)
 x+=1

4. Select Statements

Most languages have a "select" or "case" statement. These statements allow you to extend what we just did with "if" and "else" to any number of conditions. Python uses "if" with a special "else if" or "elif" statement to achieve the same results.

Using the same example, let's say we want to print out a different message when the value is over 75, between 50 and 75, between 25 and 50, and 25 or below.

import random

x=0
while (x<10):
 y=random.randint(1,100) 
 print(y)
 if (y>75): 
  Message="Number is over 75"
 elif (y>50):
  Message="Number is over 50 and less than or equal to 75"
 elif (y>25):
  Message="Number is over 25 and less than or equal to 50"
 else:
  Message="Number of less than or equal to 25"
 print(Message)
 x+=1

Notice that we only have to check if the number is over 50. This is because if the number is greater than 75, it will execute the code in the first "if" statement and then move to the "print()" function. Similarly, if the number is greater than 50, it will never reach the comparison for "y>25". If this is unclear, now is a perfect time to use the Wingware IDE to "walk through" the code and see what is happening.

5. Logical Operators

Logical operators operate on two Boolean values and return a Boolean value.  “And” and “Or” are the most commonly used operators and are used heavily with “if” statements.
 


Logical

 

not

Logical negation

and

Logical And

or

Logical Or

 

The tables below show the values that result from two operands A and B using a Boolean operator.  The values for A and B are either 0 or 1 as shown.  The result of the combination of A and B using the specified Boolean operators are in the cells matching the column for a value of A and the row for a value of B.  An example would be “Anding” A=0 with B=0 would result in 0.

not

A=0

A=1

 

1

0

 

and

A=0

A=1

B=0

0

0

B=1

0

1

 

or

A=0

A=1

B=0

0

1

B=1

1

1

 

6. Complex Conditions

You can combine multiple boolean expressions together with the logical operates mentioned above. Let's just print out a message when a random value is greater than 25 "and" less than 75.

import random
x=0
while (x<10):
 y=random.randint(1,100) 
 print(y)
 if (y>25) and (y<75):
 	Message="Number is between 25 and 75"
 print(Message)
	x+=1 

You can combine conditional statements with boolean expressions to make very complex decisions in your programs. We recommend you use parenthesis to keep the logic clear.

Additional Resources

Python Documentation: Conditional Expressions

 

© Copyright 2018 HSU - All rights reserved.