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函数中,表示将操做权返回给操做系统。

相关文章
相关标签/搜索