break:cause the innermost enclosing loop or switch to be exited immediately.
continue: cause the next iteration of the enclosing for, while, or do loop to begin. The continue statement applies
only to loops, not to switch. A continue inside a switch inside a loop causes the next loop iteration.
break:导致最内层的封闭循环或者switch立刻退出;
continue:导致for,while,或者do循环的下一个循环开始执行。continue只用在循环中,不用在switch中。一个循环中switch中的continue会导致下一个循环。
#include <stdio.h>
/* test the break and continue */
int main()
{
int i;
for (i = 0; i < 10; i++)
{
switch(i)
{
case 5:
break;
}
printf("%d", i);
}
return 0;
}
/* test the break and continue */
int main()
{
int i;
for (i = 0; i < 10; i++)
{
switch(i)
{
case 5:
break;
}
printf("%d", i);
}
return 0;
}
输出:0 1 2 3 4 5 6 7 8 9
break只是退出了switch,执行了后面的printf
#include <stdio.h>
/* test the break and continue */
int main()
{
int i;
for (i = 0; i < 10; i++)
{
switch(i)
{
case 5:
continue;
}
printf("%d", i);
}
return 0;
}
/* test the break and continue */
int main()
{
int i;
for (i = 0; i < 10; i++)
{
switch(i)
{
case 5:
continue;
}
printf("%d", i);
}
return 0;
}
输出:0 1 2 3 4 6 7 8 9
continue直接执行下一个for循环,没有执行printf