AI智能
改变未来

几句话说清楚goto,break与continue

先来看看goto

#include<stdio.h>void main(){int i,j, count =0;for (i = 1; i <= 10; i++){for (j = 1; j <= 3; j++){if (i == 6)goto stop;/*跳出外循环*/count++;}}stop:printf(\"i=%d\", i);printf(\"循环次数%d\", count);}

输出i=5 循环次数15
再来看看break

#include<stdio.h>void main(){int i,j, count =0;for (i = 1; i <= 10; i++){for (j = 1; j <= 3; j++){if (i == 6)break;/*跳出内循环*/count++;}}printf(\"i=%d\", i);printf(\"循环次数%d\", count);}

输出i=11,循环次数27
最后来看看continue

#include<stdio.h>void main(){int i,j, count =0;for (i = 1; i <= 10; i++){for (j = 1; j <= 3; j++){if (j == 2)continue;/*挖去j=2所有情况*/count++;}}printf(\"i=%d\", i);printf(\"循环次数%d\", count);}

输出i=11 循环次数20

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 几句话说清楚goto,break与continue