C++ 循环中的小疑问

C++ 循环中的小疑问,c++,c,loops,C++,C,Loops,在下面的代码段中,将测试多少次“x”值 int x; for(x=0;x < 10; x++) printf("%d",x); intx; 对于(x=0;x

在下面的代码段中,将测试多少次“x”值

int x;
for(x=0;x < 10; x++)
   printf("%d",x);
intx;
对于(x=0;x<10;x++)
printf(“%d”,x);

对我来说,答案似乎是11,但我的模块说答案是10?!我遗漏了什么?

您的循环仅在x<10时运行,因此x是0-9的所有值,而不是0-10。有10个值0-9,因此循环运行10次


或者,如果你只是在谈论比较,那么是的,这是测试11次。您的模块不正确。

您的循环仅在x<10时运行,因此x是0-9的所有值,而不是0-10。有10个值0-9,因此循环运行10次


或者,如果你只是在谈论比较,那么是的,这是测试11次。您的模块不正确。

11,因为在调用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)
0<10==true
1<10==真
2<10==正确
3<10==真
4<10==真
5<10==正确
6<10==正确
7<10==正确
8<10==真
9<10==正确
10<10==false//退出循环(未执行printf)

11,在调用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)
0<10==true
1<10==真
2<10==正确
3<10==真
4<10==真
5<10==正确
6<10==正确
7<10==正确
8<10==真
9<10==正确
10<10==false//退出循环(未执行printf)
10次--

  • 将0分配给x
  • 检查x<0是否为真,如果不是,则转到6
  • 执行循环体
  • 增量x
  • 去2号
  • 循环后编码
  • 如果执行该操作,循环体将被调用10次,因为第11次循环条件变为false,并且循环体将永远不会执行。

    10次--

  • 将0分配给x
  • 检查x<0是否为真,如果不是,则转到6
  • 执行循环体
  • 增量x
  • 去2号
  • 循环后编码

  • 如果执行该操作,循环体将被调用10次,因为第11次循环条件变为false,并且该体将永远不会执行。

    是的,
    x<10
    表达式将被计算11次。前10次为真,最后一次为假(因为
    x==10
    )。

    是的,
    x<10
    表达式计算11次。前10次为真,最后一次为假(因为
    x==10
    )。

    在0-9有10个值,但将测试11次,最后一次返回假,退出循环。

    在0-9有10个值,但将测试11次,最后一次返回假,退出循环。

    是。测试将执行11次,主体仅执行10次。

    是。测试将执行11次,主体仅执行10次。

    (x=0;x<10;x++)
    
    for(x=0;x < 10; x++) 
    
    X从0开始,但在9结束,因为当X小于10时,代码循环,所以要解决这个问题,这里有几个解决方案:

    for(x=0;x <= 10; x++) 
    
    for(x=0;x
    for(x=0;x<10;x++)
    
    X从0开始,但在9结束,因为当X小于10时,代码循环,所以要解决这个问题,这里有几个解决方案:

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

    for(x=0;x如果你对调试器不满意,你可以作弊:

    int main() {
        int x;
        for(x=0;(printf("testing %d\n", x) || 1) && (x < 10); x++)
            printf("%d\n",x);
        return 0;
    }
    
    如果您想以正确的方式做事,并在这个过程中学会调试软件,请从阅读开始

    这是一个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       }
    
    $gdb循环
    GNU gdb(gdb)7.0.1-debian
    版权所有(C)2009免费软件基金会。
    许可证GPLv3+:GNU GPL版本3或更高版本
    这是自由软件:您可以自由更改和重新发布它。
    在法律允许的范围内,不提供任何担保。键入“显示复制”
    和“显示保修”了解详细信息。
    此GDB配置为“x86_64-linux-gnu”。
    有关错误报告说明,请参阅:
    ...
    从/home/nathan/c/loop读取符号…完成。
    (gdb)中断6
    断点1位于0x4004ec:file loop.c,第6行。
    (gdb)运行
    启动程序:/home/nathan/c/loop
    断点1,loop.c处的main():6
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)n
    测试0
    7 printf(“%d\n”,x);
    (gdb)
    0
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试1
    7 printf(“%d\n”,x);
    (gdb)
    1.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试2
    7 printf(“%d\n”,x);
    (gdb)
    2.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试3
    7 printf(“%d\n”,x);
    (gdb)
    3.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试4
    7 printf(“%d\n”,x);
    (gdb)
    4.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试5
    7 printf(“%d\n”,x);
    (gdb)
    5.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试6
    7 printf(“%d\n”,x);
    (gdb)
    6.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试7
    7 printf(“%d\n”,x);
    (gdb)
    7.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试8
    7 printf(“%d\n”,x);
    (gdb)
    8.
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试9
    7 printf(“%d\n”,x);
    (gdb)
    9
    6表示(x=0;(printf(“测试%d\n”,x)| | 1)和&(x<10);x++)
    (gdb)
    测试10
    8返回0;
    (gdb)
    9       }
    
    如果你对调试器不满意,你可以作弊:

    int main() {
        int x;
        for(x=0;(printf("testing %d\n", x) || 1) && (x < 10); x++)
            printf("%d\n",x);
        return 0;
    }
    
    如果您想以正确的方式做事,并在这个过程中学会调试软件,请从阅读开始

    这是一个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       }
    
    $gdb循环
    GNU gdb(gdb)7.0.1-debian
    版权所有(C)2009年发现的自由软件