goto 语句
自从提倡结构化设计以来,goto 就成了有争议的语句。
首先,由于 goto 语句可以 灵活跳转,如果不加限制,它的确会破坏结构化设计风格。其次,goto 语句经常带来错 误或隐患。
它可能跳过了某些对象的构造、变量的初始化、重要的计算等语句。
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 int main(int argc, char** argv) { 6 int score; 7 8 //从键盘上输入分数 9 cout<<"score="; 10 cin>>score; 11 12 //用带else if的条件语句判断处理 13 if (score<0 || score>100) 14 { 15 cout<<"The score is out of range!"<<endl; 16 } 17 else if (score>=90) 18 cout<<"Your grade is a A."<<endl; 19 else if (score>=80) 20 cout<<"Your grade is a B."<<endl; 21 else if (score>=70) 22 cout<<"Your grade is a C."<<endl; 23 else if (score>=60) 24 cout<<"Your grade is a D."<<endl; 25 else 26 cout<<"Your grade is a E."<<endl; 27 return 0; 28 }