在C语言中,可以使用goto或者if..else语句来实现整个函数的跳转。
- int foo( int arg ) {
- int result;
- if ( arg == 0 ) {
- goto END;
- }
- // do something with arg
- result = arg * 2;
- END:
- return result;
- }
- // program label
- good_night:
- printf("Good night!\n");
- // goto statement
- goto good_night;
还可以使用break 和 continue 语句来条件跳转:
- for (i = 0; i < 10; i++) {
- if (i == 5)
- break;
- printf("%d is not 5\n", i);
- }
- for (i = 0; i < 10; i++) {
- if (i == 5)
- continue;
- printf("%d is not 5\n", i);
- }
还有return语句,用来退出当前函数的执行:
- int foo() {
- if (condition)
- return 1;
- else
- return 0;
- }
|