为什么这个输出是12? [英] Why this output is 12 ?

查看:117
本文介绍了为什么这个输出是12?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  

int main()
{
int x = 10 ;

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

printf( %d,x);
}

解决方案

Quote:

为什么这个输出是12?

它是12,因为它是源代码中请求的内容。

如果你还有其他的东西,我想你不明白你的代码。 br />


我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]


< pre lang =C#> int x = 10 ;

do {
x ++; // 后期增量,x将为11.
}
while (x ++ > 12 ); // 后期增量,因此检查(11> 12)条件失败。然后递增。

printf( %d, X); // 输出为12





首先我们得x = 10;

正如我们在do-while循环中所知道的那样,'do'中的第一次代码将在不检查条件的情况下运行。所以x ++;将使x = 11.



现在它会检查条件,即while(x ++> 12)

这里x ++是后增量所以x将保持为11并且它将检查条件(11> 12)并且条件在此失败。所以它将退出do-while循环。

但由于后期增量x将增加1.现在x的值为12.



所以最后 printf 语句将打印12作为输出。


对于您之前的问题,您正式接受了错误的答案。你应该接受这个:为什么输出是9和19 [ ^ ] 。



它的好处是:它也回答了你现在的问题。如果您不明白为什么,请阅读评论并重新阅读答案本身,这一次更彻底。



请帮我们一个忙:停止询问这类问题。考虑答案是通用的。提出不同类型的问题。



-SA


#include<stdio.h>

int main()
{
    int x=10;

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

    printf("%d",x);
}

解决方案

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[^]


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.


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


这篇关于为什么这个输出是12?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆