The following code shows the idea of branching.
The lines following the "if" that are spaced in, are only run when the if line above them is really true. Spaces are important in Python. The lines that have more space at the beginning than the "if" are called "indented". The "if" makes a decision about whether to run the following lines that are indented or not.
The key '>' means "greater than" or for positive numbers, means larger than.
Anything on a line after a '#' is a comment and is not run.
This diagram shows the path in the code when boxA is set equal to 3. The check for "boxA > 2" is true because boxA is 3.
boxA = 3 # Not indented, so it is always run
print('Before if boxA > 2') # Not indented, so it is always run
if boxA > 2: # Not indented, so it is always run
print('boxA is > 2') # Indented line that is run if boxA > 2
print('This also runs when boxA is > 2') # Indented line that is run if boxA > 2
print('After indented lines for boxA > 2') # Not indented, so it is always run
if boxA > 5: # Not indented, so it is always run
print('boxA is greater than 5') # Indented line that is run if boxA > 5
print('This is always displayed') # Not indented, so it is always run
Before if boxA > 2 boxA is > 2 This also runs when boxA is > 2 After indented lines for boxA > 2 This is always displayed
When the value of boxA is set to 10, then the path taken is shown here.
boxA = 10 # Not indented, so it is always run
print('Before if boxA > 2') # Not indented, so it is always run
if boxA > 2: # Not indented, so it is always run
print('boxA is > 2') # Indented line that is run if boxA > 2
print('This also runs when boxA is > 2') # Indented line that is run if boxA > 2
print('After indented lines for boxA > 2') # Not indented, so it is always run
if boxA > 5: # Not indented, so it is always run
print('boxA is greater than 5') # Indented line that is run if boxA > 5
print('This is always displayed') # Not indented, so it is always run
Before if boxA > 2 boxA is > 2 This also runs when boxA is > 2 After indented lines for boxA > 2 boxA is greater than 5 This is always displayed
What happens when boxA is set to 1?
boxA = 5
if boxA == 5:
print('boxA is equal to 5')
if boxA == 3:
print('boxA is equal to 3') # This does not display if boxA is set to 5.
if boxA != 6:
print('boxA is not equal to 6')
boxA is equal to 5 boxA is not equal to 6
Here is a table of the checks.
Key | Meaning |
---|---|
> | is greater than |
< | is less than |
= | is equal |
!= | is not equal |
> | is greater or equal |
< | is less or equal |
boxA = 2
if boxA == 5:
print('boxA is equal to 5')
else:
print('boxA is not equal to 5')
boxA is not equal to 5
boxA = 2
if boxA == 5:
print('boxA is equal to 5')
elif boxA == 6:
print('boxA is equal to 6')
else:
print('boxA is not equal to 5 or 6')
boxA is not equal to 5 or 6
boxA = 2
boxB = 5
if boxA == 2:
print('boxA is equal to 2')
if boxB == 5:
print('boxA is equal to 2 and boxB is equal to 5')
print('done')
boxA is equal to 2 boxA is equal to 2 and boxB is equal to 5 done
Notice that if boxA is not two, then the check for boxB cannot happen because it was skipped since it is indented compared to the check for boxA.
boxA = 1
boxB = 5
if boxA == 2:
print('boxA is equal to 2')
if boxB == 5:
print('boxA is equal to 2 and boxB is equal to 5')
print('done')
done
boxA = 2
boxB = 5
if boxA == 2 and boxB == 5:
print('boxA is equal to 2 and boxB is equal to 5')
print('done')
boxA is equal to 2 and boxB is equal to 5 done
There is a way to check two things at once using the word "or".
boxA = 1
boxB = 5
if boxA == 2 or boxB == 5:
print('Either boxA is equal to 2 or boxB is equal to 5')
print('done')
Either boxA is equal to 2 or boxB is equal to 5 done