+ 4
how the answer will be 5 can anyone explain?
#include <stdio.h> int main() { int x=1; while(x++<=1) while(x++<=2); printf("%d",x); return 0; }
3 Respostas
+ 5
Why semicolon 👉🏻 while(x++<=2); ?? Try it without semicolon you will get output 3
+ 5
https://code.sololearn.com/cmxq29mPRD0W/?ref=app
/*
line7: x=1
line8: while(x++<=1) //true as 1<=1
function of x++(post increment operator)
first use then increase
so value of x=1 will be used in this loop then increment will take place
now x=2
line9: while(x++<=2); //true as 2<=2
again post increment will work
so now x=3
now it will again check
while(x++<=2); //false as 3<=2 is false
but again post increment will work
so now x=4
so now compiler will jump to line8 as statement in line9 failed
line8: while(x++<=1) //false as 4<=1 is false
again post increment will work
so x=5
now compiler will jump to line11
line11: printf("&d",x); // 5
so 5 is your output
hope you understood...
comment back for any other query
*/
+ 2
When we enter the 1st loop the x will become 2 after the test and it will test on the second loop 2==2 so it's true and it's gonna increment then gonna check again the second loop but this time it's gonna be false but even though it's gonna increment now x=4 test the wrapping loop and increment x=5 and done, the problem was in the way this increment works inside loops even if it's not true it is going to increment whether the test true or false