Output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Output

Int I=3; If(!I) I++; I++; If(I==3) I+=2; I+=2; Printf("%d", I); The output is 6 but how?

6th Dec 2020, 4:44 PM
Achintya Raj
Achintya Raj - avatar
7 Answers
+ 4
Lets format your code to understand more easily: int l = 3; if (!l) { // False. Doesn't execute. l++; } l++; // l = 4 if (l==3) { //Doesnt execute either cause l=4 now. l += 2; } l += 2; // 4 + 2 printf( %d", l); See? When you doesn't use brackets for an if, else, for, while etc..., Only the [ONE] instruction immediately after it gets executed. First you assign 3 to l. Then you check if l doesn't has a Truthy value, which is true because e is a true value, and the ! operator inverts the result which returns false to the if test. Then you increment by one and checke if l == 3, which is false cause at that point l is equal to 4, so it skips the if block and executes the last statement: l += 2. 4 + 2 = 6.
6th Dec 2020, 4:59 PM
Erick Ruh Cardozo
Erick Ruh Cardozo - avatar
+ 2
Achintya Raj Here i is declared so !i will be false and if condition will not work only one i++ will work. So here i will be 4 Now again i == 3 will be false so Only one i += 2 will work and i will be 6
6th Dec 2020, 4:53 PM
A͢J
A͢J - avatar
+ 1
Achintya Raj First i is 3 then it is increment & you add 2 then it becomes 6
6th Dec 2020, 4:46 PM
Sâñtôsh
Sâñtôsh - avatar
+ 1
But what is the reason
6th Dec 2020, 4:47 PM
Achintya Raj
Achintya Raj - avatar
+ 1
Thanks
6th Dec 2020, 5:01 PM
Achintya Raj
Achintya Raj - avatar
0
Such an easy question I didn't recognise
6th Dec 2020, 5:01 PM
Achintya Raj
Achintya Raj - avatar
0
Thanks for the ans
6th Dec 2020, 5:02 PM
Achintya Raj
Achintya Raj - avatar