【C语言】continue、break、do-while的用法

资料来源:C循环|菜鸟教程

break:

1、当break语句出现在一个内循环时,循环会立即终止,程序接着执行循环语句的下一句语句;也就是跳出当前的循环

2、用于终止switch语句中的一个case;

example:

#include <stdio.h>
 
int main ()
{
   /* 局部变量定义 */
   int a = 10;

   /* while 循环执行 */
   while( a < 20 )
   {
      printf("a 的值: %d\n", a);
      a++;
      if( a > 15)
      {
         /* 使用 break 语句终止循环 */
          break;
      }
   }
 
   return 0;
}

执行结果:

 

continue:

continue的作用是终止本次循环,跳过当前循环中的代码,强制开始下一次循环,而break的作用是终止整个循环!

continue之后,仍会执行循环条件。

example:

#include <stdio.h>
 
int main ()
{
	int m = 1;


	do
	{
		if (m == 5)
			continue;

		printf("%d\n", m);

	} while (m++<10);


	printf("\nHello, World! \n");

	system("pause");

	return 0;
}

执行结果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值