表达式1 语句1 表达式2>T 语句2 T 表达式n-1 语句n-1 语句n 2021-2-24
2021-2-24 -16-
例子:输出学生的成绩 #include <iostream using namespace std void mal int X;cin>>X if(x>=90 cout<<s, 优秀”<<end else if(x>=80) cout<< 良好”<<endl se if(x>=60) cout<<so 合格”<<endl ese cout<<o 不及格”<<endl 2021-2-24
2021-2-24 -17- 例子:输出学生的成绩 #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; elsecout<<“不及格”<<endl; }
3.2ⅲ选择语句 ◆4、i语句的嵌套if(表达式1) if(表达式2 语句 else 语句2 else if(表达式3) 句3 else 语句4 2021-2-24
2021-2-24 -18- 3.2 if选择语句 4、if 语句的嵌套 if (表达式1) if (表达式2) 语句1 else 语句2 else if (表达式3) 语句3 else 语句4
T 表达式1 F 表达式2 表达式3 F 语句1 语句2 语句3 语句4 2021-2-24
2021-2-24 -19-
例子:输出a,b,c中较大的数 #include <iostream using namespace std void maino int a b cout<<(a>b?(a>c?a: c): (b>c? b: c )) cin>>a>>b>>c if(a>b) if (a>c)cout<<a<<endl else cout<<e<<end: e se if(>c)cout<<b<<endl; else cout<<c<<endl: 2021-2-24
2021-2-24 -20- 例子:输出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 ) );