我对这个程序中的break语句有疑问。
从技术上讲,break语句终止它所在的循环,但是在这个程序中,break在if语句中。
所以,这里的break应该只终止if语句,对吗?但它也会终止do-while语句。
抱歉,如果我问错了什么。我是编程新手
#include <stdio.h>
int main()
{
int count;
char response;
for (count = 1; count <= 100; count++)
{
printf("count = %d\n", count);
printf("enter y to continue or any other key to quit");
scanf(" %c", &response);
if (response != 'y')
break;
}
printf("thank you!\n");
return 0;
}发布于 2021-02-18 00:02:11
根据C标准(6.8.6.3 break语句)
2 break语句终止最小的封闭switch或迭代语句的执行。
此if语句中的break语句
if (response !='y')
break;终止封闭的for语句的执行。
你可以这样想象它的动作:
for (count=1;count<=100;count++){
//...
if (response !='y')
goto L1;
}
L1:
printf("thankyou!");如果if语句没有包含在迭代语句或switch语句中,则不能在if语句中使用break语句。
break语句是一个跳转语句,它将控制传递到最小的封闭switch或迭代语句之外。
发布于 2021-02-18 00:04:57
您可以在两种状态下使用"break“语句。
循环中的
https://stackoverflow.com/questions/66245572
复制相似问题