for循环中的小疑问

发布于 2024-10-09 11:30:53 字数 195 浏览 0 评论 0原文

在以下代码片段中,“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

陌上青苔 2024-10-16 11:30:53

十一,因为在调用 printf 之前,在每次循环迭代的开始测试条件:

0 < 10 == true
1 < 10 == true
2 < 10 == true
3 < 10 == true
4 < 10 == true
5 < 10 == true
6 < 10 == true
7 < 10 == true
8 < 10 == true
9 < 10 == true
10 < 10 == false    // exit from loop (printf not executed)

Eleven, as the condition is tested at the beginning of each loop iteration, before printf is called:

0 < 10 == true
1 < 10 == true
2 < 10 == true
3 < 10 == true
4 < 10 == true
5 < 10 == true
6 < 10 == true
7 < 10 == true
8 < 10 == true
9 < 10 == true
10 < 10 == false    // exit from loop (printf not executed)
维持三分热 2024-10-16 11:30:53

仅当 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.

护你周全 2024-10-16 11:30:53

如果您对调试器不熟悉,您可以作弊:

int main() {
    int x;
    for(x=0;(printf("testing %d\n", x) || 1) && (x < 10); x++)
        printf("%d\n",x);
    return 0;
}

打印

testing 0
0
testing 1
1
testing 2
2
testing 3
3
testing 4
4
testing 5
5
testing 6
6
testing 7
7
testing 8
8
testing 9
9
testing 10

如果您想以正确的方式做事并在此过程中学习调试软件,请首先阅读 这个

这是包含上面代码的 gdb 会话。您可以计算循环测试线被击中的次数。 11 点了。

$ gdb loop
GNU gdb (GDB) 7.0.1-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/nathan/c/loop...done.
(gdb) break 6
Breakpoint 1 at 0x4004ec: file loop.c, line 6.
(gdb) run
Starting program: /home/nathan/c/loop 

Breakpoint 1, main () at loop.c:6
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) n
testing 0
7               printf("%d\n",x);
(gdb) 
0
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 1
7               printf("%d\n",x);
(gdb) 
1
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 2
7                   printf("%d\n",x);
(gdb) 
2
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 3
7               printf("%d\n",x);
(gdb) 
3
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 4
7               printf("%d\n",x);
(gdb) 
4
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 5
7               printf("%d\n",x);
(gdb) 
5
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 6
7               printf("%d\n",x);
(gdb) 
6
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 7
7               printf("%d\n",x);
(gdb) 
7
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 8
7               printf("%d\n",x);
(gdb) 
8
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 9
7               printf("%d\n",x);
(gdb) 
9
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 10
8           return 0;
(gdb) 
9       }

If you're not comfortable with debuggers, you can cheat:

int main() {
    int x;
    for(x=0;(printf("testing %d\n", x) || 1) && (x < 10); x++)
        printf("%d\n",x);
    return 0;
}

which prints

testing 0
0
testing 1
1
testing 2
2
testing 3
3
testing 4
4
testing 5
5
testing 6
6
testing 7
7
testing 8
8
testing 9
9
testing 10

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.

$ gdb loop
GNU gdb (GDB) 7.0.1-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/nathan/c/loop...done.
(gdb) break 6
Breakpoint 1 at 0x4004ec: file loop.c, line 6.
(gdb) run
Starting program: /home/nathan/c/loop 

Breakpoint 1, main () at loop.c:6
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) n
testing 0
7               printf("%d\n",x);
(gdb) 
0
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 1
7               printf("%d\n",x);
(gdb) 
1
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 2
7                   printf("%d\n",x);
(gdb) 
2
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 3
7               printf("%d\n",x);
(gdb) 
3
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 4
7               printf("%d\n",x);
(gdb) 
4
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 5
7               printf("%d\n",x);
(gdb) 
5
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 6
7               printf("%d\n",x);
(gdb) 
6
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 7
7               printf("%d\n",x);
(gdb) 
7
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 8
7               printf("%d\n",x);
(gdb) 
8
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 9
7               printf("%d\n",x);
(gdb) 
9
6           for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) 
testing 10
8           return 0;
(gdb) 
9       }
放低过去 2024-10-16 11:30:53

如果您的问题是关于表达式 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.

找个人就嫁了吧 2024-10-16 11:30:53

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.

等数载,海棠开 2024-10-16 11:30:53

是的。 TEST 将执行 11 次,主体仅执行 10 次。

Yes. The TEST will be executed 11 times, the body only 10 times.

岁月静好 2024-10-16 11:30:53
for(x=0;x < 10; x++) 

X 从 0 开始,但以 9 结束,因为当 x 小于 10 时,您的代码会循环,因此要解决此问题,这里有一些解决方案:

for(x=0;x <= 10; x++) 

并且

for(x=0;x < 11; x++) 

这些都会导致 11

for(x=0;x < 10; x++) 

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:

for(x=0;x <= 10; x++) 

and

for(x=0;x < 11; x++) 

These would both result in 11

╄→承喏 2024-10-16 11:30:53

嘿,伙计们,这要容易得多!

for 循环如下所示:

loop

这使得条件得到测试:

  • 第一次迭代之前,
  • 每次迭代之后。

因此 10 次迭代给出 11 次测试。简单的!

Hey, folks, that's far easier!

The for loop looks like:

loop

This gives that the condition is tested:

  • before the first iteration,
  • after each iteration.

Hence 10 interations gives 11 tests. Simple!

兮颜 2024-10-16 11:30:53

10 次 --

  1. 将 0 赋给 x
  2. 检查 x 是否 < 0 为真,如果不是,则转到 6
  3. 执行循环体
  4. 增量 x
  5. 转到 2。
  6. 循环后的代码

如果执行它,最终会调用循环体 10 次,因为第十一次循环条件变为假,并且身体永远不会被处决。

10 times --

  1. Assign 0 to x
  2. Check if x < 0 is true, if not go to 6
  3. Execute loop body
  4. Increment x
  5. Go to 2.
  6. Code after the loop

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.

久夏青 2024-10-16 11:30:53

是的,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 (because x==10).

负佳期 2024-10-16 11:30:53

这是一个通常在面试问题中被问到的脑筋急转弯。一个简单的检查方法是将其从 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.

怼怹恏 2024-10-16 11:30:53

你是认真的?这应该是直观的!

x 已测试 n+1 次(其中 n 为 10),但条件仅满足 n 次,因为您正在使用< 运算符*

看一下效果:

int x;
for(x=0;x < 10; x++)
   printf("%d",x);
printf("%d",x);

最后一个 print 语句应该输出 10,这表明 x 又被测试了一次(因为我们是从零开始的,所以 x 实际上测试了 11 次) 。

Are you serious? this should be intuitive!

x is tested n+1 times (where n is 10), but the condition is satisfied n times only because you are using the < operator*!

to see the effect:

int x;
for(x=0;x < 10; x++)
   printf("%d",x);
printf("%d",x);

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文