【问题标题】:Triple Nested For loop (C++) [closed]三重嵌套 For 循环(C++)[关闭]
【发布时间】:2015-12-07 14:55:57
【问题描述】:

在这个“三重”嵌套 for 循环程序中,ctr 变量没有递增。这是为什么呢?

// Example program
#include <iostream>
#include <string>

for (int ctr = 0; ctr < 256; ctr++)
{
    for (i; i < 512; i += 32)
    {
        for (int j = 0; j < 512; j += 32)
        {
            std::cout << "Count: " << ctr << i << "," << j << std::endl;
        }
    }
}

return 0

【问题讨论】:

    标签: c++ .net loops for-loop nested


    【解决方案1】:

    i 在读取之前未初始化。那是未定义的行为。你的编译器没有警告你吗?

    简单修复:使用for (int i = 0; /*etc*/;

    【讨论】:

      【解决方案2】:

      也许只有你思考,因为ctr &gt; 0 时不会打印ctr,因为i 没有在for 循环中初始化。

      尝试将for (i; i &lt; 512; i += 32) 更改为for (int i = 0; i &lt; 512; i += 32)

      【讨论】:

        猜你喜欢
        • 2020-08-09
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        相关资源
        最近更新 更多