吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → For与While

For与While

来源:网络 | 更新时间:2022-01-14 08:04:46
问题:有没有以下两个循环不能以相同方式工作的示例?
/*Program 1 --> For loop*/
for (<init-stmnt>; <boolean-expr>; <incr-stmnt>) 
{
<body-statements>
}

/*Program 2 --> While loop*/
<init-stmnt>;
while (<boolean-expr>) 
{
<body-statements>
<incr-stmnt>
}
解决方案: 如果 body-statements 包含 continue,那么这两个程序将以不同的方式工作 请参阅以下示例:程序 1 将打印“循环”3 次,但程序 2 将进入无限循环。 程序 1 的示例
int main()
{
int i = 0;
for(i = 0; i < 3; i++)
{
printf("loop ");
continue;
} 
getchar();
return 0;
}
程序 2 的示例
int main()
{
int i = 0;
while(i < 3)
{
printf("loop"); /* printed infinite times */
continue;
i++; /*This statement is never executed*/
} 
getchar();
return 0;
}
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com