// if 实现选择语句 #include<iostream> usingnamespace std; intmain(){ int year; bool isLeapYear; cout<<"Enter the year: "; cin>>year; //将提取符作用在流类对象cin上,键盘输入 isLeapYear = ((year%4 == 0 && year%100!=0) || (year%400 == 0)); if (isLeapYear) cout<<year<<" is a leap year"<<endl; // endl 插入换行符,并刷新流 else cout<<year<<" is not a leap year"<<endl; return0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 嵌套的if语句
#include<iostream> usingnamespace std;
intmain(){ int x, y; cout<<"Enter x and y:"; cin>>x>>y; if (x!=y) if (x>y) cout<<"x>y"<<endl; else cout<<"x<y"<<endl; else cout<<"x=y"<<endl; return0; }