Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<stdio.h>

int main()
{
    int x=10;

    do {
        x++;
    } while(x++ > 12);

    printf("%d",x);
}
Posted
Updated 26-Feb-16 0:31am
v2
Comments
[no name] 27-Jan-16 3:18am    
">" or "<" is the Thing you should think about.
KarstenK 26-Feb-16 7:04am    
This is what your source code will execute. Use a debugger to visit each step of your code.

Quote:
Why this output is 12 ?
It is 12 because it is what is requested in the source code.
If you expected something else, I guess you don't understand your code.

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jan-16 3:28am    
5ed.
—SA
Patrice T 27-Jan-16 3:36am    
Thank you.
I hope the OP will not post a question for every syntax trick :)
Sergey Alexandrovich Kryukov 27-Jan-16 3:51am    
It comes to that, it looks like. :-(
—SA
Arthur V. Ratz 20-Mar-16 11:37am    
5.
C#
int x=10;

do{
    x++;    //post increment, x will be 11.
}
while(x++ > 12); //post increment so check (11 > 12) condition failed. Then Increment.

printf("%d",x); // output will be 12


first we are having x=10;
As we know in do-while loop, first time code inside 'do' will run without checking while condition. so x++; will make x=11.

Now it will check while condition i.e while (x++ > 12)
Here x++ is post increment so x will remain 11 and it will check condition (11 > 12) and condition fails here. so it will exit do-while loop.
but because of post increment x will be increment by 1. now value of x is 12.

So at last printf statement will print 12 as output.
 
Share this answer
 
v2
For your previous question, you formally accepted wrong answer. You should have accepted this one: Why the output is 9 and 19[^].

Its benefit is: it also answers to your present question. If you don't understand why, read the comments and re-read the answer itself, this time more thoroughly.

And please do us a big favor: stop asking this type of question. Consider the answer was generic. Ask questions of different types.

—SA
 
Share this answer
 
Comments
Patrice T 27-Jan-16 3:39am    
+5
Sergey Alexandrovich Kryukov 27-Jan-16 3:51am    
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900