0
0
Pythonprogramming~5 mins

If statement execution flow in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does an if statement do in Python?
It checks a condition and runs a block of code only if that condition is true.
Click to reveal answer
beginner
What happens if the if condition is false and there is no else block?
The program skips the if block and continues with the next code after it.
Click to reveal answer
intermediate
How does an elif statement work in an if-elif-else chain?
It checks its condition only if all previous if and elif conditions were false.
Click to reveal answer
beginner
What is the role of the else block in an if statement?
It runs a block of code when all previous if and elif conditions are false.
Click to reveal answer
beginner
Consider this code:<br>
if x > 10:
  print('Big')
elif x > 5:
  print('Medium')
else:
  print('Small')
<br>What will print if x = 7?
It will print Medium because x > 10 is false but x > 5 is true.
Click to reveal answer
What happens when the if condition is true?
AThe code inside the <code>if</code> block runs.
BThe code inside the <code>else</code> block runs.
CThe program stops.
DThe code inside the <code>elif</code> block runs.
If all if and elif conditions are false, which block runs?
A<code>if</code> block
BNo block runs
C<code>elif</code> block
D<code>else</code> block
Can an if statement have multiple else blocks?
AYes, as many as you want.
BNo, only one <code>else</code> block is allowed.
COnly if you use <code>elif</code>.
DOnly if the conditions are true.
What does the elif keyword mean?
AIt ends the if statement.
BElse, it runs if all conditions are true.
CElse if, it checks another condition if previous ones are false.
DIt repeats the if block.
What will this code print if score = 85?<br>
if score >= 90:
  print('A')
elif score >= 80:
  print('B')
else:
  print('C')
AB
BA
CC
DNothing
Explain how Python decides which block of code to run in an if-elif-else statement.
Think about the order Python checks conditions.
You got /4 concepts.
    Describe what happens when an if condition is false and there is no else block.
    What does the program do next?
    You got /3 concepts.