如何在嵌套的 for 循环中使用“继续一段时间”循环
How to use continue for a while loop in a nested for loop
但是,在满足 if 语句的条件后,我正在尝试继续 while 循环,但是,if 语句在 for 循环中,而继续语句只是继续 for 循环而不是 while 循环。我的代码如下所示:
while (valid_input == false) {
printf("Enter a date (yyyy/mm/dd): ");
fflush(stdout);
fgets(date, 20, stdin);
for (int i = 0; i <= 3; i++) {
if (!isdigit(date[i])) {
printf("Error: You didn't enter a date in the format (yyyy/mm/dd)n");
continue;
}
}
我如何对此进行编码,以便在满足条件(!isdigit(date[i]((后继续在while循环的开头?
您可以
简单地使用另一个布尔变量来指示您要continue
外部循环并break
执行内部循环:
while (valid_input == false) {
printf("Enter a date (yyyy/mm/dd): ");
fflush(stdout);
fgets(date, 20, stdin);
bool continue_while = false; // <<<
for (int i = 0; i <= 3; i++) {
if (!isdigit(date[i])) {
printf("Error: You didn't enter a date in the format (yyyy/mm/dd)n");
continue_while = true; // <<<
break; // <<< Stop the for loop
}
}
if(continue_while) {
continue; // continue the while loop and skip the following code
}
// Some more code in the while loop that should be skipped ...
}
如果没有更多需要跳过的代码,也许在 for()
循环中只break;
就足够了。
使用
continue
是不可能的,您需要使用goto
或条件。很难,在你的特定情况下,break
会达到相同的结果。
顺便说一句。我在这里不考虑处理日期验证的设计决策。只需回答如何进行下一次while
迭代。
相关文章:
- 循环后如何继续阅读
- 循环不继续无法弄清楚为什么
- 如何创建一个 while 循环,继续循环,直到在C++中输入'using streams'键?
- while 循环 C++ 结束,但不继续到其余语句
- While循环在fork和execvp调用后没有继续
- C++,程序输出无效输入,但继续通过循环
- 程序终止,而不是继续整数输入验证循环
- C /QT 5 Gstreamer继续循环
- 我如何摆脱循环并继续循环
- 在 fork() 之后,如何在 for() 循环中继续运行 execve()
- 如何在嵌套的 for 循环中使用“继续一段时间”循环
- C++ for 循环继续循环,即使它不应该循环
- 如何在无效的数据类型输入后继续循环
- 使用ifstreamwhile循环,如何显示输入错误并在下一行继续
- 在基于范围的 for 循环中继续不会重新触发断点?
- 有人可以帮助我做一个时循环,以便在每次计算后我都可以继续使用CIN值
- C++ ? 运算符,在 for 循环中继续运算符
- C++循环在 EOF 之后继续运行时这样做
- 在C++中添加while循环以继续或退出控制台
- 循环浏览文件夹中的所有文件以继续执行脚本