+ 2

num=8 if num>3: print "3" if num<5: print "5" if num==7: print "7" output of this program is s 3.. why is it so????

2nd Nov 2016, 1:36 AM
Navaneetha Babu
Navaneetha Babu - avatar
11 Answers
+ 14
We have num = 8 ..... first If block condition is true, 8 is greater than so 3 will be printed On second If block condition is false so nothing will be printed .... Third If block is within second IF block (nested). As condition of main If block is false so all the remaining code within it will not be checked so that's why 3 is the right answer
2nd Nov 2016, 1:52 AM
Waqas Asghar Bhalli
Waqas Asghar Bhalli - avatar
+ 6
if num=8 > greater than 3, print 3. truly 8 is greater than 3 that means the statement is true 3 will be printed. second if num< Less than 5 is num i.e. 8 less than 5...? no that means 3 will still be printed. lastly if num i.e. 8 == 7. no 8 isn't 7 therefore 3 will still be printed..
6th Dec 2016, 2:45 PM
Xperience
Xperience - avatar
+ 5
these are a nested if loops. if first condition is true I.e if num > 3 is true then only the second condition is executed and the second condition is if num < 5 which is false so it comes out of the loop and hence only 3 is printed for output. upvote if you like the answer
2nd Nov 2016, 4:07 AM
Sri Vathsa
Sri Vathsa - avatar
0
thankzz..... guyzzz
2nd Nov 2016, 5:20 AM
Navaneetha Babu
Navaneetha Babu - avatar
0
you are welcome navaneeta babu
2nd Nov 2016, 5:23 AM
Sri Vathsa
Sri Vathsa - avatar
0
nothing gets executed after the second if statement as num=8 is not less than 5 So the output is only 3
7th Nov 2016, 2:44 PM
Nitin Rao
Nitin Rao - avatar
0
In simpler terms, since you had num=8 if number is greater than 3(true), print 3. Since 8 is greater than 3, it wrote 3.
15th Nov 2016, 12:20 AM
Miles Bradley
0
The first statement only true except that ask are false that's why it prints the first condition
18th Dec 2016, 2:56 PM
Bairavan vvl
Bairavan vvl - avatar
0
>>> x = 4 >>> x = 5 >>> print
10th Dec 2019, 10:29 AM
rafi
0
num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") why 3 is the only output it should be 37 right because the last if statement is true so it should print 37? can u help me?
12th May 2020, 6:23 AM
Jexter Perez
Jexter Perez - avatar
0
thanks
26th Aug 2020, 4:58 PM
Godsgift Akintola
Godsgift Akintola - avatar