Why it doent print 7 also? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why it doent print 7 also?

num==7 so that statement should give output

1st Mar 2017, 6:06 AM
Shaunak Pai Kane
Shaunak Pai Kane - avatar
3 Answers
+ 5
@uvaish We put checks/expressions that can be evaluated to True or False in If statements. It is supposed to be a condition and the condition is being satisfied here. @Shaunak But it is a different case here. You need to remember and understand that, if you have "nested" if statements, then the first one needs to be "True" for the second one to run at all, doesn't matter if they inside nested condition is True or False. In the code: if num < 5: print("5") if num ==7: print("7") "if num < 5:" evaluates to False. So, the program flow would never reach "if num ==7:" It will skip all the statements inside "if num<5" statement.
1st Mar 2017, 9:49 AM
Akshay Kumar
Akshay Kumar - avatar
+ 4
thanks a lot my doubt is cleared
1st Mar 2017, 10:10 AM
Shaunak Pai Kane
Shaunak Pai Kane - avatar
0
Hello! here is my explanation: num = 7 <---This is the given value *if num > 3: <----This is the 1st condition, if it´s not satisfied then the program goes to condition #2 print("3") *if num < 5: <---- This is the second condition,if not satisfied goes to condition #3 print("5") *if num ==7: <----This is the third and final condition. print("7") As we can note, the number given is 7 and 7==7 right? Mathematically it is true, but the "If" condition in the program evaluates the input in a logical way as I explained above: Value given, if condition #1 is true the program ends, if not then evaluates condition #2, and so on. Because the first condition is TRUE (7 is greater than 3) then the program ends and the result is 3 because that is the instruction( print("3")). *P.D. In fact we will never get to the final condition (7==7) because the program would end immediately in the first condition. *P.D. Sorry if I do not explain myself clearly.
19th May 2017, 4:33 AM
Pedro Andreu
Pedro Andreu - avatar