为什么不打印 While 循环?

Why isn't the While loop printing?

本文关键字:循环 While 打印 为什么不      更新时间:2023-10-16
#include <iostream>
int i = 0;
int main()
{
    for (i = 0; i < 5; i++)
    {
        std::cout << "Hello Worldn";
    }
    while(i < 5)
    {
        std::cout << "Inside while Loop" << std::endl;
    }
}

在上面的循环中, i的值已经增加到5,因为

for(i=0;i<5;i++)
{
    /* at the end of for loop i=5 */
}

现在您的i值为5,条件失败。尝试重置i的值,它将起作用。

您在第一个循环中递增i,但切勿将其重置。因此,当它退出for循环时,我仍然5岁,因此while循环永远不会执行它。设置我在段循环之前等于零,并且while循环将运行。如果执行此操作,则循环将永远运行,除非您在循环内更新i

当for循环停止执行时,我将变为5,因此在循环时不符合条件。将我设置为for循环后的0。

i=0;
while(i<5)
 ..// your code here
 ..