while语句中的break问题

stringtheory123 2007-06-27 10:20:41
#include <stdio.h>
int main()
{
#define TRUE 1
int month;

while (TRUE)
{
printf("\nEnter a month between 1 and 12: ");
scanf("%d", &month);

if (month > 1 && month < 12)
break;

printf("Error - the month you entered is not valid.\n");
}

printf("The month accepted is %d\n", month);

return 0;
}
=======================
想问一下,这里的break中断以后,不是直接挑出while语句吗,怎么还会继续执行后面的printf提示error语句?如果我把break语句去掉,输入0或1则显示颠倒,而且程序永远不停止了。为什么?

...全文
1008 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
danpianjidezhuren 2011-04-21
  • 打赏
  • 举报
回复
不好意思。写错了,break只跳出if语句,接着往下执行error。
danpianjidezhuren 2011-04-21
  • 打赏
  • 举报
回复
你的break语句是不能结束while(true)循环的,break只能结束本次循环,如果你想结束while(true)循环,可以在break后面加一个2,即break 2;就可以了应该。
可以参考一下下面这个例程::::
while (++$i)
{
switch ($i)
{
case 5:
echo "At 5<br />\n";
break 1; /* 只跳出switch循环,1为参数. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* 跳出while和switch循环,2为参数. */
default:
break;
}
}
?>

输出结果:
At 5
At 10; quitting
stringtheory123 2007-06-28
  • 打赏
  • 举报
回复
我想问的就是:为什么while语句里break没跳出去,而do-while里跳出去了呢
stringtheory123 2007-06-28
  • 打赏
  • 举报
回复
但是那个do-while语句里面,break之后,后面salestax = RATE * price;
printf("The sales tax is $%5.2f", salestax);
不执行啊。
stringtheory123 2007-06-28
  • 打赏
  • 举报
回复
哦,是的,汗!!!
lightnut 2007-06-28
  • 打赏
  • 举报
回复
if (month > 1 && month < 12)
break;
===============================
注意了: 输入1或12, 也不满足上面的条件, 就会打印下面的Error.
LZ是不是把条件搞错了?:)

if (month>=1 && month<=12) break;
lvtaoatscu 2007-06-28
  • 打赏
  • 举报
回复
是这样的嘛?我在VC6.0下测试后没有你所说的那种问题啊,是不是你弄糊涂了哦。
chenyu2202863 2007-06-27
  • 打赏
  • 举报
回复
那时因为你没跳出去
fubusi 2007-06-27
  • 打赏
  • 举报
回复
当你输入在1到12之间是,IF成立,则跳出WHILE循环执行外层;当IF不成立时,则不执行break
执行了printf(error),永远记住IF只执行后面第一个语句;
可以理解吧。嘿嘿
fubusi 2007-06-27
  • 打赏
  • 举报
回复
哦,不好意思我答非所问了,呵呵,稍等
fubusi 2007-06-27
  • 打赏
  • 举报
回复
因为while的条件是true,当输入的值大于12是则不执行IF循环体的,所以跳出了WHILE循环,执行
外层循环
stringtheory123 2007-06-27
  • 打赏
  • 举报
回复
回复人:uzone(曾子曰:三省吾身) ( 二级(初级)) 信誉:100 2007-06-27 22:38:16 得分:0
?
我这边运行的很好,vc 6.0 编译器
=============================
我用gcc也没问题,不是编译,这个程序本身可以运行,我只是好奇
这里的break之后,为什么还能执行
printf("Error - the month you entered is not valid.\n");
这条printf语句.
stringtheory123 2007-06-27
  • 打赏
  • 举报
回复
sorry, 上面这个do-while语句每打好,应该改成

#include <stdio.h>
#define SENTINEL 0.0
#define RATE .06
int main()
{
float price, salestax;

do
{
printf("\nEnter a price or -1 to terminate: ");
scanf("%f", &price);

if (price < SENTINEL)
break;

salestax = RATE * price;
printf("The sales tax is $%5.2f", salestax);

} while (price > SENTINEL);

return 0;

}
~
stringtheory123 2007-06-27
  • 打赏
  • 举报
回复
上面那个break后还会执行error语句,但下面这个
#include <stdio.h>
#define SENTINEL 0.0
#define RATE .06
int main()
{

printf("Enter a price: ");
scanf("%f", &price);

do
{
printf("\nEnter a price or -1 to terminate: ");
scanf("%f", &price);

if (price < SENTINEL)
break;

salestax = RATE * price;
printf("The sales tax is $%5.2f", salestax);
} while (price > SENTINEL)

return 0;

}

====================
上面这个语句break后就不会执行紧接着的printf语句了啊。不是很矛盾吗?
uzone 2007-06-27
  • 打赏
  • 举报
回复
我这边运行的很好,vc 6.0 编译器
stringtheory123 2007-06-27
  • 打赏
  • 举报
回复
你输入不在1-12的数字,它会提示你错误的。
星羽 2007-06-27
  • 打赏
  • 举报
回复
想问一下,这里的break中断以后,不是直接挑出while语句吗,怎么还会继续执行后面的printf提示error语句?

------------

不可能吧

break 跳出 while

33,317

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧