表达式1 语句1 T 表达式2 语句2 F 表达式n-1 语句n-1 语句n
例子:输出学生的成绩 #include <iostream> using namespace std void maino Intx cin>>X if(x>=90 cout<<“优秀”<endi; else if(x>=80) cou<<“艮好”<<endl else if(x>=60 cou<“合格”<endl; else cou<“不及格”<endl;
例子:输出学生的成绩 #include <iostream> using namespace std; void main() { int x; cin>>x; if(x>=90) cout<<“优秀”<<endl; else if(x>=80) cout<<“良好”<<endl; else if(x>=60) cout<<“合格”<<endl; else cout<<“不及格”<<endl; }
3.2i选择语句 ◆4、if语句的嵌套if(表达式1) if(表达式2) 语句1 else 语句2 else if(表达式3) 语句3 else 语句4 2005-4-27 北京邮电大学电信工程学院计算机技术中心 18
2005-4-27 北京邮电大学电信工程学院计算机技术中心 -18- 3.2 if选择语句 if (表达式1) if (表达式2) 语句1 else 语句2 else if (表达式3) 语句3 else 语句4 4、if 语句的嵌套
T 表达式1 F 表达式2 表达式3 语句1 语句2 语句3 语句4
例子:输出a,b,c中较大的数 #include < iostream> using namespace std void minot int a.b.c cout<<(a>b? (a>c?a: c): (b>c?b: c )) cin>>a>>b>>c f(a>b) if(a>c) cout<<a<<endl else cout<<c<<endl else 11 d if(bc)cout<<b<<end else cout<<c<<endl
例子:输出a,b,c中较大的数 #include <iostream> using namespace std; void main(){ int a,b,c; cin>>a>>b>>c; if (a>b) { if (a>c) cout<<a<<endl; else cout<<c<<endl; } else { if (b>c) cout<<b<<endl; else cout<<c<<endl; } } cout<< (a>b?(a>c?a:c):(b>c?b:c ) );