手把手带你学习C++的运算符
目录
- 运算符
- 01 算术运算符
- 02 赋值运算符
- 03 比较运算符
- 04 逻辑运算符
- 总结
运算符
作用:用于执行代码的运算
运算符类型 |
作用 |
算术运算符 | 用于处理四则运算 |
赋值运算符 | 用于将表达式的值赋给变量 |
比较运算符 | 用于表达式的比较,并返回一个真值或假值 |
逻辑运算符 | 用于根据表达式的值返回真值或假值 |
01 算术运算符
运算符 | 术语 | 示例 | 结果 |
+ | 正号 | +3 | 3 |
- | 负号 | -3 | -3 |
+ | 加 | 10+5 | 15 |
- | 减 | 10-5 | 5 |
* | 乘 | 10*5 | 50 |
/ | 除 | 10/5 | 2 |
% | 取模/取余 | 10%3 | 1 |
++ | 前置递增 | a=2;b=++a; | a=3;b=3; |
++ | 后置递增 | a=2;b=a++; | a=3;b=2; |
-- | 前置递减 | a=2;b=--a; | a=1;b=1; |
-- | 后置递增 | a=2;b=a--; | a=1;b=2; |
#include<iostream> using namespace std; int main1() { //加减乘除 int a1 = 10; int b1 = 3; cout << a1 + b1 << endl; cout << a1 - b1 << endl; cout << a1 * b1 << endl; cout << a1 / b1 << endl; //两个整数相除,结果依然是整数,将小数部分去除 int a2 = 10; int b2 = 20; cout << a2 / b2 << endl; int a3 = 10; int b3 = 0; //cout << a3 / b3 << endl; //错误,除数不可以为0 double x1 = 0.5; double x2 = 0.22; cout << x1 / x2 << endl; //取模 int c1 = 10; int d1 = 3; cout << c1 % d1 << endl; int c2 = 10; int d2 = 20; cout << c2 % d2 << endl; int c3 = 10; int d3 = 0; //cout << c3 % d3 << endl; //错误,取模除数不可以为0 double x3 = 3.14; double x4 = 1.1; //cout << x3 % x4 << endl; //错误,不支持小数取模运算 //1.前置递增 int m = 10; ++m; cout << "m = " << m << endl; //2.后置递增 int n = 10; n++; cout << "n = " << n << endl; //3.前置和后置的区别 //前置递增:先让变量+1,后进行表达式运算 int m2 = 10; int n2 = ++m2 * 10; cout << "m2 = " << m2 << endl; cout << "n2 = " << n2 << endl; //后置递增:先进行表达式运算,后让变量+1 int m3 = 10; int n3 = m3++ * 10; cout << "m3 = " << m3 << endl; cout << "n3 = " << n3 << endl; system("pause"); return 0; }
02 赋值运算符
运算符 | 术语 | 示例 | 结果 |
= | 赋值 | a=2;b=3; | a=2;b=3; |
+= | 加等于 | a=0;a+=2; | a=2; |
-= | 减等于 | a=5;a-=3; |
a=2; |
*= | 乘等于 | a=2;a*=2; |
a=4; |
/= | 除等于 | a=4;a/=2; | a=2; |
%= | 模等于 | a=3;a%=2; | a=1; |
#include<iostream> using namespace std; int main2() { //赋值运算符 int a = 10; cout << "a = " << a << endl; //10 // += a += 2; cout << "a = " << a << endl; //12 // -= a = 10; a -= 2; cout << "a = " << a << endl; //8 // *= a = 10; a *= 2; cout << "a = " << a << endl; //20 // /= a = 10; a /= 2; cout << "a = " << a << endl; //5 // %= a = 10; a %= 2; cout << "a = " << a << endl; //0 system("pause"); return 0; }
03 比较运算符
运算符术语示例结果==相等于4==30!=不等于4!=31<小于4<30>大于4>31<=小于等于4<=30>=大于等于4>=31
#include<iostream> using namespace std; int main3() { //比较运算符 // == int a = 10; int b = 20; cout << (a == b) << endl; // != cout << (a != b) << endl; // > cout << (a > b) << endl; // < cout << (a < b) << endl; // >= cout << (a >= b) << endl; // <= cout << (a <= b) << endl; system("pause"); return 0; }
04 逻辑运算符
运算符术语示例结果!非!a如果a为假,则!a为真;如果a为真,则!a为假。&&与a&&b如果a和b都为真,则结果为真,否则为假。||或a||b如果a或b有一个为真,则结果为真;二者都为假时,结果为假。
#include<iostream> using namespace std; int main4() { //逻辑运算符 非 ! int a = 10; cout << !a << endl; //0 cout << !!a << endl; //1 //逻辑运算符 与 && int c = 10; int d = 10; cout << (c && d) << endl; // 1 c = 0; d = 10; cout << (c && d) << endl; // 0 c = 10; d = 0; cout << (c && d) << endl; // 0 c = 0; d = 0; cout << (c && d) << endl; // 0 //逻辑运算符 或 || int e = 10; int f = 10; cout << (e || f) << endl; // 1 e = 0; f = 10; cout << (e || f) << endl; // 1 e = 10; f = 0; cout << (e || f) << endl; // 1 e = 0; f = 0; cout << (e || f) << endl; // 0 system("pause"); return 0; }
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!
赞 (0)