跳转至

运算符#

运算符是告诉编译器执行特定数学/逻辑操作的符号。由运算符和操作数组成的式子称为表达式

算术运算符#

+ - * / %

注意事项:

  1. 两个整数 / 得到的一定是整数(截断小数位)
  2. a / 0a % 0(整数)会报错
  3. 小数除以 0 得到 ,对 0 取余得到 NaN
Console.WriteLine(1 + 1);
int int1 = 2 + 3;
Console.WriteLine(5 - 2);
Console.WriteLine(5 * 5);

// int 和 int 运算,只能得到 int
int int2 = 2;
int int3 = 4;
Console.WriteLine(int2 / int3);        // 0(截断小数)
Console.WriteLine((double)int2 / int3); // 0.5(强制转 double)

// % 取余
Console.WriteLine(5 % 2);   // 1
Console.WriteLine(10 % 2);  // 0

// 0 不能作为整数除数和取余数
// Console.WriteLine(10 / 0);
// Console.WriteLine(10 % 0);
Console.WriteLine(2.2 / 0);  // ∞
Console.WriteLine(2.2 % 0);  // NaN

// + 运算符:既可以数学运算,也可以字符串拼接
string name = "吴亦凡";
int age = 20;
string sex = "男";
string info = name + "今年" + age + "岁,性别是" + sex;
Console.WriteLine(info);

// 字符串和数值拼接时,不会进行数学运算
string info1 = name + "今年" + age + 10 + "岁,性别是" + sex;
// → "吴亦凡今年2010岁,性别是男"(age+10 被当作拼接)

string info2 = name + "今年" + (age + 10) + "岁,性别是" + sex;
// → "吴亦凡今年30岁,性别是男"(括号内先运算)

总结:

  1. 整数和整数运算,一定得到整数
  2. 整数和小数运算,可以得到小数
  3. 整数不能除以 0,也不能对 0 取余
  4. 小数可以除以 0,得到无穷大,对 0 取余得到 NaN

赋值运算符#

=, +=, -=, *=, /=, %= 中间不能加空格,它们是一个整体。

// = 用于赋值
int i1 = 10;
i1 += 5;  // 等同于 i1 = i1 + 5
Console.WriteLine(i1);  // 15

// 当出现 a = a + n 的时候,就可以简写为 a += n
// i1 -= 5;  // i1 = i1 - 5
// i1 *= 5;  // i1 = i1 * 5
// i1 /= 5;  // i1 = i1 / 5
// i1 %= 5;  // i1 = i1 % 5

自增 / 自减运算符(++ / --)#

自加 1,自减 1。

  • ++ 在变量:先赋值,再自增(后置)
  • ++ 在变量:先自增,再赋值(前置)
  • -- 同理
int a = 10;
int b = ++a;  // b = 11, a = 11(先自增再赋值)
Console.WriteLine(b);

int c = 11;
int d = c++;  // d = 11, c = 12(先赋值再自增)
int e = ++c;  // e = 13, c = 13
Console.WriteLine(e);

比较运算符(关系运算符)#

<  >  <=  >=  ==  !=

返回 bool 类型。

int aa = 3;
int bb = 5;
bool b1 = aa > bb;  // false
b1 = aa < bb;       // true
b1 = aa <= bb;      // true
b1 = aa >= bb;      // false
b1 = aa == bb;      // false
b1 = aa != bb;      // true
Console.WriteLine(b1);

三目运算符#

格式:比较表达式 ? 值1 : 值2

执行逻辑:比较表达式为 true 返回值 1;为 false 返回值 2。

// 基本用法
string name1 = true ? "吴亦凡" : "罗志祥";
Console.WriteLine(name1);

// 判断是否成年
int age1 = 25;
string remark = age1 < 18 ? "未成年" : "成年";
Console.WriteLine(remark);

// 判断三个数中的最大值(可嵌套)
int in1 = 13, in2 = 5, in3 = 2;
int max = in1 > in2 ? (in1 > in3 ? in1 : in3) : (in2 > in3 ? in2 : in3);
Console.WriteLine(max);

逻辑运算符#

运算符 名称 特性 说明
& 非短路与 两边都执行 全真才真
&& 短路与 遇假则停 左边为 false 右边不执行
\| 非短路或 两边都执行 一真则真
\|\| 短路或 遇真则停 左边为 true 右边不执行
! 取反
// & 非短路与
Console.WriteLine(true & true);    // True
Console.WriteLine(true & false);   // False

// && 短路与
Console.WriteLine(true && true);   // True
Console.WriteLine(false && true);  // False(右边不执行)

// | 非短路或
Console.WriteLine(true | false);   // True

// || 短路或
Console.WriteLine(true || false);  // True(右边不执行)

// ! 非
Console.WriteLine(!true);          // False
Console.WriteLine(!(10 > 20));     // True

短路特性示例:

int num1 = 3, num2 = 4;

// &:两边都执行 → num1++ 会执行
bool bools1 = num1 > num2 & num1++ == 3;
// bools1 = False, num1 = 4

// &&:左边为 false,右边不执行 → num1 不变
bool bools2 = num1 > num2 && num1++ == 3;
// bools2 = False, num1 = 4(还是原来的值)

// |:两边都执行
bool bools3 = num1 < num2 | num1++ == 3;
// bools3 = True, num1 = 5

// ||:左边为 true,右边不执行
bool bools4 = num1 < num2 || num1++ == 3;
// bools4 = True, num1 = 5(num1++ 未执行)

运算符优先级(从高到低)#

  1. . () {} ;
  2. ++ -- !
  3. * / %
  4. + -
  5. < > <= >=
  6. == !=
  7. &
  8. |
  9. &&
  10. ||
  11. ? :

记忆技巧:不确定优先级时使用括号明确运算顺序,代码更易读。

位运算符#

& | ^(按位与、按位或、按位异或),对整数的二进制位进行运算。

数学运算(Math 类)#

Math 类提供了常用的数学运算方法,详见 Math 数学运算

Console.WriteLine(Math.Abs(-33));   // 33
Console.WriteLine(Math.Round(3.4)); // 3
Console.WriteLine(Math.Floor(2.9)); // 2
Console.WriteLine(Math.Ceiling(2.9)); // 3
Console.WriteLine(Math.Max(10, 30)); // 30
Console.WriteLine(Math.Min(10, 30)); // 10
Console.WriteLine(Math.Pow(2, 4));   // 16
Console.WriteLine(Math.Sqrt(9));     // 3
Console.WriteLine(Math.PI);          // 3.14159...

相关笔记#