标签:
一.运算符
运算符包括:算数运算符、关系运算符、条件运算符、赋值运算符
1. 算数运算符:++,--,*,/,%,+,-
int a = 3++;//常量不可以使用++,--
int a = 2;
a++;//这句代码可以写成a=a+1;
++a;
Console.WriteLine(a);
前加加和后加加的区别
int a = 3;
int b = a++;//====>int b = a; a=a+1;
int c = ++a;//====>a=a+1; int c =a;
Console.WriteLine("b="+b);
Console.WriteLine("c="+c);
2. 关系运算符:>,<,>=,<=,==,!=
int a = 3;
int b = 5;
bool c = a >= b;
bool d = a == b;
bool e = a < b;
Console.WriteLine(e);
3.逻辑运算符:&&与,||或,!非
int a = 1;
int b = 2;
int c = 3;
bool d = a >= b && a < c;//两者必须都同时满足
bool e = a < b || a > c;//两者只要有一方满足
//返回值不是true就是false
Console.WriteLine(e);
4.条件运算符: ?:
问号前面是关系表达式,问,此关系表达式对么?
如果返回值是true,得到冒号前面的值
如果返回值是false,得到冒号后面的值
这是唯一的一个三元(目)运算符
int a = 1;
int b = 2;
string c = (a > b) ? "这是对的" : "这是错的";
string d = a < b ? "这是对的" : "这是错的";
Console.WriteLine(d);
5.赋值运算符: = , += , -= , *= , /= , %=
int a = 1;
int b = 2;
a += b;//a = a +b; a = 3;
a -= b;//a=1;
a *= b;//a =2;
a /= b;//a=1;
Console.WriteLine(a);
6.% (模)取最后的余数
int a = 10;
int b = 4;
int c = a % b;
Console.WriteLine(c);
标签:
原文地址:http://www.cnblogs.com/arxk/p/5157358.html