-
Notifications
You must be signed in to change notification settings - Fork 1
Control Flow
In this lesson, we will learn about ways to control how a program behave.
Boolean is a kind of datatype. It have only two values: True
and False
.
Now you can think of your true-false questions on your tests as booleans!
Comparison operators are symbols that will give us results in boolean. Let's first introduce them:
This is used to check if 2 variable/values are same. For example:
5 == 5 # This give us True
5 == 0 # This give us False
Just like equals, this checks for exact opposite of equals:
5 != 5 # This give us False
5 != 0 # This give us True
This checks for if a value/variable is greater than another:
5 > 5 # This give us False
5 > 0 # This give us True
This checks for if a value/variable is less than another:
5 < 5 # This give us False
0 < 5 # This give us True
This checks for if a value/variable is greater than or equals another:
5 >= 5 # This give us True
0 >= 5 # This give us False
This checks for if a value/variable is less than or equals another:
5 <= 5 # This give us True
0 <= 5 # This give us True
Conditions are formulas written with boolean values or operators. That means all the previous examples are conditions! All of them used a boolean operator, and they give back a boolean value as result.
Now, we will show some more complicated conditions (boolean statements): Try to calculate their value!
1. True == True
2. True != False
3. 42 == '42'
4. 0 == 0.0
What if we want something more complicated? So far we have only 1 condition. What if we want multiple conditions or at least 1 condition to happen?
Let's introduce them.
Conditions on both sides of this operator must be True
for this to be True
.
That is, both things must happen for this condition to pass.
For example:
5 == 5 and 5 > 0 # True
5 == 5 and 5 > 9 # False
At least 1 condition on both sides of this operator must be True
for this to be True
.
That is, one thing must happen for this condition to pass.
For example:
True == False or 5 > 0 # True
True == False or 5 > 9 # False
Using this keyword, we can flip boolean values. That is, True
to False
and False
to True
not True # False
not False # True
not 0 == 6 # True
not 10 = 10.0 # False
Now, with these operators, we can write more complex conditions. Try to calculate their boolean value.
1. True != False and False != False
2. not 9 > 0 or not 0 > 9
3. False or True and False