资料来源: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;
}
执行结果: