C语言中的break和continue

        一般在的,while, for 这样含有循环体的语句中,某些情况下我们会需要立即跳出当前循环。这时使用break语句就可以实现直接从当前循环体直接跳出开始执行while, for语句的下一条语句。

        另外,break 这种可以从一段复合语句中跳出的功能也被switch 广泛借用,来跳过switch 复合语句,也就是被花括号框住的代码块中剩余的部分,转去执行switch 语句的下一条语句。因此在switch 中,break 常被用来控制控制流跳出,使得switch 在运用上更加灵活。

        而Continue 略不同于break 。虽然同样在while, for 语句中体现出“跳过”的效果。但continue是跳过循环体的剩余部分,而后重新执行循环体的句头而非下一条语句。

        另continue使用范围被限制在循环体中,不可用于switch。

 

我们可以通过一些简单的代码来验证他们的效果:

break 语句在while…do 句型中的效果:

#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
      //break in while...do
      //初始化 initialize
      int tmp = 0;
      puts ( "while criculation\n" );
      //while循环
      while ( tmp <= 5 ){//while循环5次
            printf_s ( "%d\t", tmp );
            printf_s ( "%d\n", tmp );
            tmp++;
      }//end while
      //初始化 initialize
      tmp = 0;
      puts ( "\nwhile circulation, execute the break when tmp = 3\n" );
      //while循环
      while ( tmp <= 5 ){//while循环5次
            printf_s ( "%d\t", tmp );
            if ( 3 == tmp ){//当tmp为3时,执行break
                  puts ( "break...");
                  break;
            }//end if
            printf_s ( "%d\n", tmp );
            tmp++;
      }//end while
}

相似的,在do…while 语句中的效果:

#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
      //break in do...while
      //初始化 initialize
      int tmp = 0;
      puts ( "while criculation\n" );
      //while循环
      do{//while循环5次
            printf_s ( "%d\t", tmp );
            printf_s ( "%d\n", tmp );
            tmp++;
      }while ( tmp <= 5 );//end while
      //初始化 initialize
      tmp = 0;
      puts ( "\nwhile circulation, execute the break when tmp = 3\n" );
      //while循环
      do{//while循环5次
            printf_s ( "%d\t", tmp );
            if ( 3 == tmp ){//当tmp为3时,执行break
                  puts ( "break...");
                  break;
            }//end if
            printf_s ( "%d\n", tmp );
            tmp++;
      }while ( tmp <= 5 );//end while
}

而break 在多重嵌套的while语句中

#include <stdio.h>
//break在while多重嵌套中的使用效果
int main () {
    //initialize
    int tmp = 0, loop = 0;
    puts ( "multiple while nesting" );
    //the first layer while
    while ( loop <= 2 ){
        loop++;
        puts ( " in the first layer while");
        //the second layer while
        while ( tmp <= 2 ){
            tmp++;
            puts ( "\tin the second while");
            if ( tmp >= 3){// execute break when tmp >= 3
                puts ( "\tbreak");
                break;
            }//end if 
            puts ( "\tout of the second while");
        }//end while
        puts ( "out the frist layer while");
    }//end while
}

break在for语句中体现:

#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
      //break in for
      //if there is no break
      puts ( "if there is no break ");
      for (int tmp = 1; tmp <= 5; tmp++){//5 loops
            printf_s ( "\nthis is No.%d", tmp );
      }//end for
      //when we add break
      puts ( "\n\nexecute the break when tmp = 2");
      for (int tmp = 1; tmp <= 5; tmp++){//5 loops
            printf_s ( "\nthis is No.%d", tmp );
            if ( 2 == tmp ){//execute when tmp = 5
                  puts ( "\tbreak" );
                  break;
            }//end if
      }//end for
}

在switch 中的运用:

#include <stdio.h>
//break 在while、for、switch语句中的运用
int main (){
      //break in switch
      //initialize
      int tmp = 1;
      
      //a switch without break
      puts ( "a switch without break");
      switch ( tmp ){
            case 0:
                  puts ( "0");
            case 1:
                  puts ( "1");
            case 2:
                  puts ( "2");
            case 3:
                  puts ( "3");
            case 4:
                  puts ( "4");
            case 5:
                  puts ( "5");
            default:
                  puts ( "switch over\n");
      }//end switch
      //add break before case 4
      puts ( "add break before case 4" );
      switch ( tmp ){
            case 0:
                  puts ( "0");
            case 1:
                  puts ( "1");
            case 2:
                  puts ( "2");
            case 3:
                  puts ( "3");
                  break;
            case 4:
                  puts ( "4");
            case 5:
                  puts ( "5");
            default:
                  puts ( "switch over");
      }//end switch
}

continue 在while 循环中的效果体现:

#include <stdio.h>
//比较验证continue在while、for语句中的运用
int main () {
    //initialize
    int tmp = 0;
    puts ( "while circulation" );
    //while with out continue
    while ( tmp <= 5 ){
        tmp++;
        printf_s ( "%d\t", tmp );
        printf_s ( "%d\n", tmp );
    }//end while
    //initialize
    tmp = 0;
    puts ( "\nwhile loop with the continue statement");
    //while loop with the continue statement
    while ( tmp <= 5 )
    {
        tmp++;
        printf_s ( "%d\t", tmp );
        if ( tmp >= 3 ){//when tmp >= 3, excute continue
            puts ( "" );    // 保证输出结果的规整
            continue;
        }//end if
        printf_s ( "%d\n", tmp );
    }//end while
}

continue在多重的while循环中表现出的作用范围同break一致,只对其所在的最近一级嵌套起作用。

#include <stdio.h>
//continue在while多重嵌套中的使用效果
int main () {
    //initialize
    int tmp = 0, loop = 0;
    puts ( "multiple while nesting" );
    //the first layer while
    while ( loop <= 2 ){
        loop++;
        puts ( " in the first layer while");
        //the second layer while
        while ( tmp <= 2 ){
            tmp++;
            puts ( "\tin the second while");
            if ( tmp >= 2){// execute continue when tmp >= 3
                puts ( "\tcontinue");
                continue;
            }//end if 
            puts ( "\tout of the second while");
        }//end while
        puts ( "out the frist layer while");
    }//end while
}

而尝试在switch 中使用continue 则会直接报错,指明continue 只能用于循环体

#include <stdio.h>
//continue 在while、for、switch语句中的运用
int main (){
      //continue in switch
      //initialize
      int tmp = 1;
      
      //a switch without continue
      puts ( "a switch without continue");
      switch ( tmp ){
            case 0:
                  puts ( "0");
            case 1:
                  puts ( "1");
            case 2:
                  puts ( "2");
            case 3:
                  puts ( "3");
            case 4:
                  puts ( "4");
            case 5:
                  puts ( "5");
            default:
                  puts ( "switch over\n");
      }//end switch
      //add continue before case 4
      puts ( "add continue before case 4" );
      switch ( tmp ){
            case 0:
                  puts ( "0");
            case 1:
                  puts ( "1");
            case 2:
                  puts ( "2");
            case 3:
                  puts ( "3");
                  continue;
            case 4:
                  puts ( "4");
            case 5:
                  puts ( "5");
            default:
                  puts ( "switch over");
      }//end switch
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值