for循环中的小疑问
在以下代码片段中,“x”值将被测试多少次?
int x;
for(x=0;x < 10; x++)
printf("%d",x);
对我来说,答案似乎是11,但我的模块说它是10?!我缺少什么?
How many times 'x' value will be tested in the following code snippet ?
int x;
for(x=0;x < 10; x++)
printf("%d",x);
To me it seems that the answer is 11 but my module says it is 10 ?! what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
十一,因为在调用 printf 之前,在每次循环迭代的开始测试条件:
Eleven, as the condition is tested at the beginning of each loop iteration, before printf is called:
仅当 x < 时,循环才会运行10,所以 x 是 0-9 之间的所有值,而不是 0-10。有 0-9 10 个值,因此循环运行 10 次。
或者,如果您只是谈论比较,那么是的,测试了 11 次。你的模块不正确。
Your loop runs only if x < 10, so x is all values from 0-9, not 0-10. There are 10 values 0-9, so your loop runs 10 times.
Or if you're just talking about comparison, then yes it's test 11 times. Your module is incorrect.
如果您对调试器不熟悉,您可以作弊:
打印
如果您想以正确的方式做事并在此过程中学习调试软件,请首先阅读 这个。
这是包含上面代码的 gdb 会话。您可以计算循环测试线被击中的次数。 11 点了。
If you're not comfortable with debuggers, you can cheat:
which prints
If you want to do things the right way and learn to debug software in the process, start by reading this.
Here's a gdb session with the code above. You can count how many times the loop test line gets hit. It's 11.
如果您的问题是关于表达式
x < 的次数10
被评估,答案是 - 这取决于。这取决于编译器优化。如果编译器生成朴素代码,那么它将对其求值 11 次。如果编译器完全展开循环,则答案将为 0。介于两者之间的任何值也是可能的。If your question is about how many times expression
x < 10
is evaluated, the answer is -- it depends. It depends on compiler optimization. If compiler generates naive code then it will evaluate it 11 times. If compiler completely unrolls your loop, the answer will be 0. Anything in between also possible.0-9有十个值,但是会测试11次,最后一次返回false,退出循环。
There are ten values at 0-9, but it will be tested 11 times, the last time returns false, exiting the loop.
是的。 TEST 将执行 11 次,主体仅执行 10 次。
Yes. The TEST will be executed 11 times, the body only 10 times.
X 从 0 开始,但以 9 结束,因为当 x 小于 10 时,您的代码会循环,因此要解决此问题,这里有一些解决方案:
并且
这些都会导致 11
X begins at zero but it ENDS at 9 because your code loops while x is less than 10 so to fix this here are a few solutions:
and
These would both result in 11
嘿,伙计们,这要容易得多!
for 循环如下所示:
这使得条件得到测试:
因此 10 次迭代给出 11 次测试。简单的!
Hey, folks, that's far easier!
The for loop looks like:
This gives that the condition is tested:
Hence 10 interations gives 11 tests. Simple!
10 次 --
如果执行它,最终会调用循环体 10 次,因为第十一次循环条件变为假,并且身体永远不会被处决。
10 times --
If you go through it, you end up with the loop body being called 10 times, since the eleventh time the loop condition becomes false and the body is never executed.
是的,
x < 10
表达式被计算 11 次。前 10 次为 true,最后一次为 false(因为x==10
)。Yes, the
x < 10
expression is evaluated 11 times. The first 10 times it is true and the final time it is false (becausex==10
).这是一个通常在面试问题中被问到的脑筋急转弯。一个简单的检查方法是将其从 10 更改为 1,即基本情况:
for (x = 0; x < 1; x++) printf("%d ", x);
它被打印 1 次。当 x = 1 时,它会跳出循环,并且不会包含在 for 循环的内容中。因此,它被打印“x”次,而不是“x + 1”次。
另外,您还可以将其视为这样做:
for (x = 1; x < 2; x++)
当 x = 1 时执行循环内容,但不执行 x = 2 或 (2 - 1) 次。
因此,要记住的重要一点是它何时真正跳出循环,以及在循环内容中运行 x 的哪些值。
This is a brain teaser that normally gets asked in interview questions. An easy way to check is to change it from 10 to 1, i.e., the base case:
for (x = 0; x < 1; x++) printf("%d ", x);
It gets printed 1 time. When x = 1 it breaks out of the loop, and doesn't get included in the contents of the for loop. Therefore, it gets printed "x" times, not "x + 1" times.
Also, you can also think about it as doing this:
for (x = 1; x < 2; x++)
It performs the loop contents when x = 1, but not x = 2 or (2 - 1) times.
So the important thing to remember is when it actually breaks out of the loop, and which values for x are run within the loop contents.
你是认真的?这应该是直观的!
x
已测试n+1
次(其中n
为 10),但条件仅满足 n 次,因为您正在使用<
运算符*!看一下效果:
最后一个 print 语句应该输出 10,这表明
x
又被测试了一次(因为我们是从零开始的,所以x
实际上测试了 11 次) 。Are you serious? this should be intuitive!
x
is testedn+1
times (wheren
is 10), but the condition is satisfied n times only because you are using the<
operator*!to see the effect:
the last print statement should output 10 which indicates that
x
is tested one more time (since we are starting from zero,x
is actually tested 11 times).