3.0运算符
用于:执行代码的运算
运算符类型:算数运算符 用于处理s四则运算
赋值运算符 用于将表达式赋值给变量
比较运算符 用于表达式的比较,并返回一个真值和假2值
逻辑运算符 用于根据表达式的值返回真值或假值
3.1算数运算符
-
加号
-
减号
-
乘
/ 除
% 取模(取余)
++ 自增
-- 自减
#include <iostream> using namespace std; int main() { int a1 = 10; int b1 = 3; cout << a1 + b1 <<endl; cout << a1 - b1 <<endl; cout << a1 * b1 <<endl; cout << a1 / b1 <<endl;//两个数相除,除数不能为0 cout << float(a1) / float(b1) <<endl; return 0; }
取模运算:%
#include <iostream> using namespace std; int main() { int a1 = 10; int b1 = 3; cout << a1 + b1 <<endl; cout << a1 - b1 <<endl; cout << a1 * b1 <<endl; cout << a1 / b1 <<endl;//两个数相除,除数不能为0 cout << float(a1) / float(b1) <<endl; cout << a1 % b1 <<endl;//取模运算的本质,就是求余数 //两个数相除除数不能为0,则不能做取模运算 //两个小数不能做取模运算 return 0; }
++ --
#include <iostream> using namespace std; int main() { int a1 = 10; int b1 = 3; cout << a1 + b1 <<endl; cout << a1 - b1 <<endl; cout << a1 * b1 <<endl; cout << a1 / b1 <<endl;//两个数相除,除数不能为0 cout << float(a1) / float(b1) <<endl; cout << a1 % b1 <<endl;//取模运算的本质,就是求余数 //两个数相除除数不能为0,则不能做取模运算 //两个小数不能做取模运算 //前置递增 int a = 10; ++a;//让变量+1 cout << "a = " << a <<endl; //后置递增 int b = 10; b++;//让变量+1 cout << "b = "<< b << endl; //前置和后置的区别 //前置递增 先让变量+1 然后进行表达式运算 int a2 = 10; int b2 = ++a2 * 10; cout << "a2 = " << a2 <<endl; cout << "b2 = " << b2 <<endl; //后置递增 先进行表达式的运算,后让变量+1 int a3 = 10; int b3 = a3++ * 10; cout << "a3 = " << a3 <<endl; cout << "b3 = " << b3 <<endl; return 0; }
3.2赋值运算符
作用:用于将表达式的值赋给变量
= 赋值
+= 加等于
-= 减等于
*= 乘等于
/= 除等于
%= 模等于
#include<iostream> using namespace std; int main02() { //= int a = 10; a = 100; cout << "a = " <<a<<endl;//100 //+= a = 10; a += 2;//a=a+2; cout << "a = "<<a<<endl;//12 //-= a = 10; a -= 2;//a=a-2; cout << "a = "<<a<<endl;//8 //*= a = 10; a *= 2; cout << "a = " <<a<<endl;//20 // /= a = 10; a /= 2; cout << "a = "<<a<<endl; //%= a = 10; a %= 2; cout << "a =" <<a<<endl; return 0; }
3.3比较运算符
作用:用于表达式的比较,并返回一个真值或假值
== 相等于 4==3 0
! = 不等于 4==4 1
< 小于 4<3 0
大于 4>3 1
≤ 小等于 4≤3 0
≥ 大等于 4≥3 1
#include<iostream> using namespace std; int main03() { int a = 10; int b = 10; cout << (a==b) <<endl;// 1 cout << (a!=b) <<endl;// 0 cout << (a>b) <<endl;// 0 cout << (a<b) <<endl;// 0 cout << (a>=b) <<endl;// 1 cout << (a<=b) <<endl;// 1 }
3.4逻辑运算符
作用:用于根据表达式返回真值或假值
! 非 !a 非真为假,非假为真
&& 与 a&&b 同真为真,其余为假
|| 或 a||b 同假为假,其余为真
#include<iostream> using namespace std; int main() { //逻辑运算符 非 ! 在C++中,除了0都为真 int a = 10; cout << !a <<endl;// 0 cout << !!a <<endl;// 1 cout << !!!a <<endl;// 0 int a1 = 10; int b1 = 10; cout << (a1&&b1) <<endl;// 1 cout << (a1&&!b1) <<endl;// 0 cout << (a1||b1) <<endl;// 1 cout << (a1||!b1) <<endl;// 1 }
标签:std,03,cout,10,int,namespace,C++,运算符,Today From: https://www.cnblogs.com/smll1/p/17417326.html