summary : Most of our everyday life is non-linear. It never goes in a straight line. Same is the case with programs. The flow of any program is also non-linear in nature. In this section, we will explore the basic flow control syntax available in Python
Say, we are programming the elevator. Depending on the elevator’s current floor, and depending on the user’s floor, the elevator will have to move to that floor.
First, we have to learn a couple of things about our environment.
- How many floors in the building ?
- What is the current floor the elevator is on ?
- Which floor is the user on ?
Where would you store these ? Variables of course. Let’s declare these variables.
floors = 10 current_floor = 0 # This is just a starting point user_floor = 0 # This is just a starting point
current_floor is just a starting point and will change as the elevator keeps moving. Same with user_floor . Now, let’s say the user has pressed the elevator button on floor number 5. How should the elevator move ?
user_floor = 6 difference = user_floor - current_floor if difference < 0 : current_floor = user_floor print ( " Move down ") if difference > 0 : current_floor = user_floor print ( " Move up ")
Try changing the user_floor variable and see if you are getting the right result or not.
the syntax for if statement is quite simple.
For example in this case, the expression is
difference < 0
which can evaluate to a boolean variable – either True or False. And the block of code that is executed can be as big as you want, as long as there is that indentation.
Nested if statement
The if statements can be nested. For example, the elevator should move up or down only when the emergency button is NOT pressed. If it is pressed, stop moving. Again, we store the value of the emergency button in another variable – a True or False boolean variable. If it is True it means that the emergency button is pressed – don’t do anything. If it is False, keep continuing with the operation the user requested.
emergency_button = False # By default we set it to false. user_floor = 7 difference = user_floor - current_floor if difference < 0 : current_floor = user_floor if emergency_button == False : print ( " Move down ") if difference > 0 : current_floor = user_floor if emergency_button == False : print ( " Move up ")
Once we get to operators & expressions, we will see examples of how to combine these multiple if statements using an and syntax.
Let’s rewrite the same program using another flow control syntax in Python called elif
user_floor = 3 difference = user_floor - current_floor if difference < 0 : current_floor = user_floor print ( " Move down ") elif difference > 0 : current_floor = user_floor print ( " Move up ")
elif is a short form for else if . Essentially, both these programs achieve the same functionality. But why use the elif syntax ?
It can be hard to visualize this. So, let’s do some debugging to understand this in action. Debugging is specific to the IDE you are using. In our case, we will use Visual Studio Code as our IDE. If you are using Spyder or PyCharm, the specific steps will be different.
Open the program debugging.py in Visual Studio Code, and click on the debug button on the left.
Set a breakpoint in the gutter ( to the left of line number ) of the first line,
Start debugging, by clicking on the play button in debug window.
In debug mode, the program execution is stopped at the first breakpoint. In our case, we have set a breakpoint at the first line and the execution should stop there. Let’s execute the program line by line. In order to do that, let’s execute the first line by clicking on the step over button.
As you can see, the program execution has moved on to the next line. The yellow highlight shows the current line of execution.
At this point, you should see that the floors variable is available for inspection.
keep going until you hit the if statement.
At this point, difference is 6 and hence it is > 0. But still the first if statement is executed to check if the difference is < 0. Since the difference is > 0, the next if statement is executed and :Move up” gets printed.
If you continue to debug the next set of if-elif statements, since difference = -3 , only the first if statement is executed and the elif statement is NOT executed.
This is just to make you understand the program flow and difference between an if statement and an elif statement.
What is the user is on the same floor as the elevator ? The elevator just needs to open the door, right ? Let’s capture that as the default case using the else statement.
user_floor = 5 difference = user_floor - current_floor if difference < 0 : current_floor = user_floor print ( " Move down ") elif difference > 0 : current_floor = user_floor print ( " Move up ") else : print ( " Open door ")
number = 10 if number > 0 , print ( " positive number ")
age = 10 if ___________ : print ( " can vote ")
age = 23 if age >= 18 : print ( " can vote ") elif age >= 21 : print ( " can drink ")
age = 23 if age >= 18 : print ( " can vote ") if age >= 21 : print ( " can drink ")
age = 9 if age >= 18 : print ( " can vote ") else : if age >= 10 : print ( " go get a girlfriend/boyfriend") else : print ( " go watch cartoons ")
age = 19 if age >= 18 : print ( " can vote ") elif age >=21 : print ( " go drink ") else : if age >= 10 : print ( " go get a girlfriend/boyfriend") else : print ( " go watch cartoons ")
Challenge – Find out if a number is positive, negative or zero. Print it out on the screencode
number = 21 if number > 0 : print ( " positive ") elif number < 0 : print ( " negative ") else : print ( " zero ")
Challenge – Say you have the ages of 3 students – all different ages, find out which of the students is the oldestcode
student_1 = 21 student_2 = 19 student_3 = 29 if student_1 > student_2 : if student_1 > student_3 : print ( "student_1 is the oldest") else : print ( "student_3 is the oldest") elif student_2 > student_3 : print ( " student_2 is the oldest") else : print ( "student_3 is the oldest")