Rule 3: Tests Control Jumps
A JUMP is useful for looping, but what about making decisions? A common thing in programming is to ask questions like:
“If x is greater than 0 then set y to 10.”
If we write this out in simple Python code, it might look like this:
1 if x > 0: 2 y = 10
Once again, this is foreshadowing something you’ll learn later, but this is simple enough to figure out:
Python will test if x is greater than > 0.
If it is, then Python will run the line y = 10
You see how that line is indented under the if x > 0:? That is called a “block” and Python uses indentation to say “this indented code is part of the code above it.”
If x is NOT greater than 0, then Python will JUMP over the y = 10 line to skip it.
To do this with our Python byte code we need a new instruction that implements the testing part. We have the JUMP. We have variables. We just need a way to compare two things and then a JUMP based on that comparison.
Let’s take that code and dis() it to see how Python does this:
1 dis(''' 2 x = 1 3 if x > 0: 4 y = 10 5 ''') 6 7 0 LOAD_CONST 0 (1) # load 1 8 2 STORE_NAME 0 (x) # x = 1 9 10 4 LOAD_NAME 0 (x) # load x 11 6 LOAD_CONST 1 (0) # load 0 12 8 COMPARE_OP 4 (>) # compare x > 0 13 10 POP_JUMP_IF_FALSE 10 (to 20) # jump if false 14 15 12 LOAD_CONST 2 (10) # not false, load 10 16 14 STORE_NAME 1 (y) # y = 10 17 16 LOAD_CONST 3 (None) # done, load None 18 18 RETURN_VALUE # exit 19 20 # jump here if false 21 20 LOAD_CONST 3 (None) # load none 22 22 RETURN_VALUE # exit
The key part of this code is the COMPARE_OP and POP_JUMP_IF_FALSE:
1 4 LOAD_NAME 0 (x) # load x 2 6 LOAD_CONST 1 (0) # load 0 3 8 COMPARE_OP 4 (>) # compare x > 0 4 10 POP_JUMP_IF_FALSE 10 (to 20) # jump if false
Here’s what this code does:
Use LOAD_NAME to load the x variable.
Use LOAD_CONST to load the 0 constant.
Use COMPARE_OP, which does the > comparison and leaves a True or False result for later.
Finally, POP_JUMP_IF_FALSE makes the if x > 0 work. It “pops” the True or False value to get it, and if it reads False, it will JUMP to instruction 20.
Doing that will jump over the code that set y if the comparison is False, but if the comparison is True, then Python just runs the next instruction, which starts the y = 10 sequence.
Take some time walking through this to try to understand it. If you have a printer, try printing it out and set x to different values manually, and then trace through how the code works. What happens when you set x = −1?
What do you mean “pop”?
In the previous code, I’m skipping over exactly how Python “pops” the value to read it, but it’s storing it in something called a “stack.” For now just think of it as a temporary storage place that you “push” values into and then “pop” them off. You really don’t need to go much deeper than that at this stage in your learning. Just understand the effect is to get the result of the last instruction.
Wait, aren’t tests like COMPARE_OP used in loops too?
Yes, and you could probably figure out how that works right now based on what you know. Try to write a while-loop and see if you can get it to work with what you know now. Don’t worry if you can’t though as we’ll be covering this in later exercises.