gpt4 book ai didi

c - C 语言的 for 循环中的 continue 语句是如何工作的?

转载 作者:行者123 更新时间:2023-11-30 21:41:51 25 4
gpt4 key购买 nike

我在 C 中有以下代码:

#include <stdio.h>

int main()
{
int i = 1;
while(i)
{
int index = 0;
printf("while begin\n");

for(index = 0; index < 10; index++)
{
if(index == 2)
{
continue;
}
printf("within for\n");
}
printf("while end\n");
}

printf("returned from while");
}

continue 是否直接影响 for 循环while 循环?看起来一旦调用了 continue ,for循环就会从头开始运行,while循环不应该从头开始运行吗?

最佳答案

continue 将导致封闭的 forwhile 循环体的剩余部分被跳过。在您的情况下, for 是 continue 的“封闭”循环,而 while 循环是 for 循环的“封闭”范围。

根据(非官方)documentation

The continue statement causes a jump, as if by goto to the end of the loop body (it may only appear within the loop body of for, range-for, while, and do-while loops).

关于c - C 语言的 for 循环中的 continue 语句是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com