0

# >>>a=10 >>>while a<=25: a+=2 print(a) >>>12,......26 Why this code print up to 26 when it is less than 25? >>>a = 10 >>>while a <= 25: print(a) a+=2 >>>12,.......24 What is the difference between these two code & why they give different result?

1st Jul 2016, 3:38 AM
Romario Kisku + 4
If you observe your code, the variable is a<=25. I. E <= is less than or EQUAL to. So the variable will start with a=10, when it is a+=2, it gives output as 12. The last value of variable a is 25 bcz of the 'less than or equal' to clause. So a+2 should give 27 as the last value
1st Jul 2016, 5:34 AM
meghna datta + 2
Initial value of a is 10, and after every iteration, +2 is added. so the code will display only even terms. and according to your constraints, last valid value of 'a' will be 24, when it enters the while loop, 'a+2' will be executed. And then the print statement will provide 26 as output. If you want that your output value should be less than 25, then your first statement in while loop should be "print('a')" statement, then the a+=2 statement. In the first code, you are first doing a+=2 so it first adds and then prints the value While in second code, you first print the value of a, which is less than 25, so it gives 24 and the addition of +2 is done after printing the value of a. Hence, the last value printed is 24. I hope I cleared your doubt. Thank you.
1st Jul 2016, 5:07 PM
Ashwin Umale + 1
a is 26 in both cases. The second code however just prints the code before 2 is added to it. That's why the second one outputs 24 while the first outputs 26.
2nd Jul 2016, 11:05 AM
Gershon Fosu 