Break、continue、return用法(C++)

(1)break

直接調出當前循環體。若是是嵌套循環,他只能調出一層循環體。ios

Exp-1:函數

程序:spa

#include<iostream>

using namespace std;

int main()

{

    for(int i=0;i<3;i++)

    {

       cout<<"the first circle"<<"  "<<i<<endl;

       for(int j=0;j<3;j++)

       {

           if(j==1)

              break;

           cout<<"the second circle"<<"  "<<j<<endl;

       }

    }

    return 0;

}

 

運行結果:操作系統

the first circle  0code

the second circle  0blog

the first circle  1ci

the second circle  0io

the first circle  2class

the second circle  0stream

請按任意鍵繼續. . .

(2) continue

忽略循環體中continue後面的的語句,進入下一次循環開始前的條件判斷,不跳出該循環。   

Exp-2:

程序:

 

#include<iostream>

using namespace std;

int main()

{

    for(int i=0;i<3;i++)

    {

       cout<<"the first circle"<<"  "<<i<<endl;

       for(int j=0;j<3;j++)

       {

           if(j==1)

              continue;

           cout<<"the second circle"<<"  "<<j<<endl;

       }

    }

    return 0;

}

 

運行結果:

the first circle  0

the second circle  0

the second circle  2

the first circle  1

the second circle  0

the second circle  2

the first circle  2

the second circle  0

the second circle  2

請按任意鍵繼續. . .

 

(3) return

return表示停止當前函數的運行,並將操做權返回給調用者。    若是是在main函數中,表示將操做權返回給操做系統。

相關文章
相關標籤/搜索