第七章 循环语句
/* // 1 循环语句的老祖宗Goto语句 #include <iostream> using namespace std; int main() { int i= 1; if(i > 10){ //goto yes; } cout<<"*"<<i<<endl; i++; //goto yes; cout<<"程序结束"; cout<<"********\n"; yes:; return 0; } */ /* // 2 while语句 #include <iostream> using namespace std; int main() { int i=1; while(i < 10) { cout<<"i:"<<i<<endl; i++; } return 0; } */ /* // 3 while语句的其它用法 #include <iostream> using namespace std; int main() { int number, dot=1; cout<<"请问你想看几次:"<<endl; cin>>number; while(dot <= number) { cout<<"问君能有几多愁,恰似一江春水向东流"<<endl; dot++; } cout<<"程序运行结束"<<endl; return 0; } */ /* // 4 continue和break语句 #include <iostream> using namespace std; int main() { int i=0; int b = 10; while(i < b ) { i++; if(i == 8){ cout<<"这里进行跳出while循环"<<endl; break; } if(i==2){ cout<<"这里只是跳出本次循环2"<<endl; continue; } cout<<"i的值为:"<<i<<endl; } return 0; } */ /* // 6 do...while循环 #include <iostream> using namespace std; int main() { int number; cout<<"请问您要看几次:"<<endl; cin>>number; int dot=0; do{ cout<<"这是第"<<number<<"次循环"<<endl; dot ++; }while(dot < number); return 0; } */ /* //7 for循环 #include <iostream> using namespace std; int main() { int number, dot=0; cout<<"请问您要看几次:"<<endl; cin>>number; for(int i=number; i>0; i--) { cout<<"这是第"<<i<<"次操作"<<endl; } return 0; } */ //8 灵活的for循环 #include <iostream> using namespace std; int main() { for(int i=0, x=0,y=0; x<= 3; i++,x++,y++) { cout<<"i="<<i<<", x="<<x<<", y="<<y<<endl; } return 0; }