Why output of this code is 6?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14
22nd May 2019, 2:43 PM
Yamin Mansuri
Yamin Mansuri - avatar
8 Answers
+ 14
If you don't use curly brackets, it'll only execute one line of code; it's not like Python int i = 3; if (!i) /*i to boolean is true because its not 0, so it is false*/ i++; /*this wont execute*/ i++;/*this will*/ if (i == 3) /*this is false because i equals 4*/ i += 2; /*this wont execute*/ i += 2; /*this will*/ 3 + 1 + 2 = 6
22nd May 2019, 2:52 PM
Airree
Airree - avatar
+ 11
Airree Thank you very much 😁😁
22nd May 2019, 2:54 PM
Yamin Mansuri
Yamin Mansuri - avatar
+ 6
Because it lacks braces on the ifs.
22nd May 2019, 2:54 PM
Eduardo Petry
Eduardo Petry - avatar
+ 5
Without {} brackets if control only one line.... your both condition is false... If(not true)=false i=3; i++; //then i become 4; Second condition false i+=2; //i become 6; Finally your answer is 6 ;
22nd May 2019, 3:12 PM
Vishal Singh
Vishal Singh - avatar
+ 5
Check out this code in Python - the output is 6. i=3 if(i==False): i+=1 if(i==3): i+=2 i+=1 print("The output is - "+ str(i))
23rd May 2019, 4:27 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 4
it's because you haven't given curly braces. I tried check this out👍 #include <stdio.h> int main() { int i=3; if(!i){ i++; i++; printf("%d",i); } if(i==3){ i+=2; i+=2; printf("%d",i); } return 0; }
22nd May 2019, 3:03 PM
Gowriprasad
Gowriprasad - avatar
+ 2
write code you miss to add curly braces check it out #include <stdio.h> int main() { int i=3; if(!i){ i++; i++; printf("%d",i); } if(i==3){ i+=2; i+=2; printf("%d",i); } return 0; }
23rd May 2019, 1:12 AM
Ameer Wajid Ali
Ameer Wajid Ali - avatar
- 2
Please check it once again .... It's showing 7
22nd May 2019, 3:53 PM
Gowriprasad
Gowriprasad - avatar