本系列文章是配合我发布的《C++编程基础》系列视频教程的补充。
在《C++编程基础》介绍循环语句结构的课里,我们在do-while结构中用cin来通过输入流将接收到的整数赋给x并判断数值的范围,简化后代码如下:
1#include
2
3int main(int argc, const char * argv[]) {
4 using std::cout;
5 using std::endl;
6 using std::cin;
7
8 int x;
9 do{
10 cout<<"Please input an integer:"<<endl;
11 cin>>x;
12 if(x>10){
13 cout<<"x is greater than 10."<<endl;
14 if(x==15){
15 cout<<"x is equal to 15."<<endl;
16 }
17 }
18 else if(x<5){
19 cout<<"x is less than 5."<<endl;
20 }else{
21 cout<<"x is between 5 and 10."<<endl;
22 }
23 }while(x!=0);
24 return 0;