while里的while里的continue(嵌套中的continue)

问题

嵌套中的continue会使程序跳到哪里呢?我翻了一下《C Primer Plus》第7章并从中了解到:嵌套中的continue仅仅影响“包含它的最里层的结构”。这里我利用第8章中的一段代码来印证这一特性。

代码如下

//guess.c -- 一个低效且错误的猜数程序 
#include <stdio.h>
int main(void)
{
	int guess = 1;
	printf("Pick an integer from 1 to 100. I will try to ");
	printf("guess it .\nRespond with a y if my guess is ");
	printf("right and with\nan n if it is wrong.\n");
	printf("Uh...is your number %d?\n", guess);
	while (getchar() != 'y'){
		printf("well,then,is it %d?\n", ++guess);
		while (getchar () != '\n')
			continue;
	}
	printf("I knew I could do it!\n");
	return 0;
}

运行结果如下

Pick an integer from 1 to 100. I will try to guess it .
Respond with a y if my guess is right and with
an n if it is wrong.
Uh...is your number 1?
n
well,then,is it 2?
no
well,then,is it 3?
no sir
well,then,is it 4?
yes
I knew I could do it!

总结

不管是输入n,no还是no sir,n后面剩余的部分(包括\n)都会被 while (getchar () != '\n')以及后面的continue给吃掉,这说明嵌套中的continue仅仅影响“包含它的最里层的结构”。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值