0
if statement
What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") Shouldn't the above code print both 3 and 7, not just 3?
3 Antworten
+ 4
It does print both, you are correct.
+ 6
That would be a good example to show how 'elif' works. If you put it like this:
num = 7
if num > 3:
print("3")
elif num < 5:
print("5")
elif num == 7:
print("7")
only "3" will be printed out. This is because 'elif' is executed *only* when the 'if' statement is False. When its True, 'elif' is omitted.
0
No, because of the middle "if" "if num < 5: print("5")" statement which is false and the code is stopped running here as no "else/elif" statement are present.