Why didnt the code stop running? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why didnt the code stop running?

num = 12 if num > 5: print("Bigger than 5") if num >=47: print("123456")*should the code not have stopped here and ignored the next part? if num <=47: print("Between 5 lol")

13th Oct 2020, 7:45 PM
John Moore
John Moore - avatar
3 Answers
+ 1
Conditional statements and loops are different. This being a conditional statement, if a given condition is false then it is simply ignored. So the program will not terminate. Since if num <= 47 is still inside the outer if statement, it will execute because the condition returns true.
13th Oct 2020, 7:54 PM
Avinesh
Avinesh - avatar
0
I think I got it now, thanks guys! #NOTES on control structures and "if" statements. num = 12 if num > 5: print("bigger than 5") if num >= 47: print("lol") #will stop running here due to the code being "False" and the next statement being "nested" if num == 12: print("hioh") #however if we do the same thing again, only this time releasing the nest, the second condition is ran as normal. num = 12 if num > 5: print("bigger than 5") if num >= 47: print("lol") #we will release this now if num == 12: print("hioh")#which now will print "hioh"
13th Oct 2020, 7:55 PM
John Moore
John Moore - avatar
0
if one "if" statement is false doesn't means program stops ,the another "if" and other if's will be checked as well given they all have same indentation level like in your case
13th Oct 2020, 7:56 PM
Abhay
Abhay - avatar