第五章 if语句与运算符
第五章 if语句与运算符
5. 赋值运算符与数学运算符的联合
a+=a-=a+a是什么意思?
赋值运算符(=)结合方向自右向左,以等号为界上式等价于a+=(a=a-(a+a))=>a=a+(a=a-(a+a))=>a=2a;
类似的还有 -= *= /= %=;
5/3=1,5/2=2,5/2.0=2.5,5%2=1。(int)
5/3=1.66667,5/2=2.5,5/2.0=2.5,5%2=1。(float)
8. 关系运算符
== != < > <= >=
9. if语句
分号; 和 大括号{} 都可以看作一条语句,申明:(); 定义{;}
13. 带括号的嵌套语句
else与最近的if(往上找)要配对,为了避免出错最好加括号{}
19. 三目运算符
cout<< (a>='A'&& a<='Z')?(a+32):a<<"\n";大写字母转小写字母
总结:一定要有括号
①int a=1,b=2;cout<<z=a>b?a:(a>b?a:b)
②int a=1;float b=2.1f;cout<<(a>b?a:b)
本章总结:
1 //1. (int) 5/3=1, 5/2=2, 5/2.0=2, 5%2=1 2 // (float) 5/3=1.66667, 5/2=2.5, 5/2.0=2.5, 5%2=1 3 //2. 要养成if语句与{}配套使用的好习惯 4 //3. ' '为字符,” ”为字符串 5 //**************************************************************************************************************************** 6 #include<iostream> 7 using namespace std; 8 int main() 9 { 10 //将大写字母转化为小心字母*****************① 11 int a=1,b=2; 12 cout<<(a>b?a:(a>b?a:b))<<endl;//不要忘记括号 13 char x; 14 cout<<"请输入一个字母:\n"; 15 cin>>x; 16 cout<<(x=(x>='A' && x<='Z')?(x+32):x)<<endl; //一定要有() 17 cout<<(x=(x>=65 && x<=90)?(x+32):x)<<endl; 18 return 0; 19 } 20 //****************************************************************************************************************************