An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C# has a wealth of built-in operators, classified as follows:
- arithmetic operators
- relational operator
- Logical Operators
- bitwise operators
- assignment operator
- Other Operators
Arithmetic
Operators The following table shows the arithmetic operators supported by C#. Assuming that the value of variable A is 10 and the value of variable B is 20, then:
Note: ++a is to perform an auto-increment operation on the value of a first, Then assign it to a variable;
relational operators
The following table shows all the relational operators supported by C#, assuming that the value of variable A is 10 and the value of variable B is 20, then:
Logical operators
The following table shows all the logical operators supported by C# operator, assuming that variable A is true and variable B is boolean false, then:
Bitwise operators
Bitwise operators act on bits and perform operations bit by bit. The truth table for &, |, and ^ is as follows:
If A equals 60, and B=13, now in binary format, they look like this:
A=0011 1100
B=0000 1101
-------------------
A&B=0000 1100 //与运算,11为1,其余为0
A|B=0011 1101 //或运算,00为0,其余为1
A^B=0011 0001 //异或运算,相同为0,不同为1
~A=1100 0011 //取反
The following table lists the bit operators supported by C#. Assuming that the value of variable A is 60 and the value of variable B is 13, then:
Assignment operator
Other operators
The following table lists some other important operators supported by C#, Including sizeof, typeof, and ? :.
Operator precedence in C#
Note:
The results of &, | are exactly the same as &&, ||, but && and || have better performance. Because && and || both check the value of the first operand, if the result can already be judged, the second operand is not processed at all
C# judgment##
1. If-else combination
if("条件表达式")
{
}else if("条件表达式"){
}
else
{
}
2、switch
demo:
static void Main(string[] args)
{
Console.WriteLine("请输入您的成绩等级:");
char grade =Convert.ToChar(Console.ReadLine());
switch(grade)//switch后面的表达式必须为整型或者枚举类型,或是一个class类型,其中class有一个单一的转换函数将其转换为整形或枚举类型
{
case 'A'://case后面的表达式必须与switch中的变量具有相同的数据类型,且必须为常量
Console.WriteLine("you are so great!");
break;
case 'B':
case 'C':
Console.WriteLine("do well");
break;
case 'D'://在下一个case语句之前必须有break
Console.WriteLine("try again");
break;
default:
Console.WriteLine("this gread is invalid");
break;
}
Console.WriteLine("您的成绩为:{0}", grade);
Console.ReadKey();
}
3. Nested switch
static void Main(string[] args)
{
Console.WriteLine("select a integer from 100,200:");
int a =Convert.ToInt32(Console.ReadLine());
Console.WriteLine("select another integer from 100,200:");
int b= Convert.ToInt32(Console.ReadLine());
switch(a)
{
case 100:
Console.WriteLine("第一个输入的值为100进入第二关");
switch(b)
{
case 100:
Console.WriteLine("第二关输入的值仍为100");
break;
case 200:
Console.WriteLine("第二关输入的值为200");
break;
default:
Console.WriteLine("第二关输入不正确,挑战失败");
break;
}
break;
default:
Console.WriteLine("第一关挑战失败,无法继续游戏");
break;
}
Console.WriteLine("第一次输入的值为:" + a+ ",第二次输入的值为:" + b);
Console.ReadKey();
}
Conditional operator? :
C# loop
1. While loop: If the condition is false, it will jump out of the loop theme and execute the next sentence immediately following the while loop.
2. For and foreach
demo:
static void Main(string[] args)
{
int[] inbarry = new int[] {11,4,5,9,66,8,59,78,5 };
foreach(int i in inbarry)
{
Console.WriteLine("使用foreach遍历结果:"+i);
}
Console.WriteLine();
for(int i=0;i<inbarry.Length;i++)
{
Console.WriteLine("使用for遍历结果:" + inbarry[i]);
}
Console.WriteLine();
//设置集合中元素的计算器
int count = 0;
foreach(int i in inbarry)
{
Console.WriteLine("Element #{0}:{1}", count, i);
count++;
}
Console.WriteLine("Number of elements in the array:{0}", count);
}
do-While loop
do-While is guaranteed to be executed at least once
static void Main(string[] args)
{
int a = 66;
do
{
Console.WriteLine("do while里面的a的值:{0}", a);
a++;
} while (a < 67);//注意此处有分号
while(a<67)//此时的a已经变为了67所以已不再成立
{
Console.WriteLine("while里面的a的值:{0}", a);
a++;
}
Console.ReadKey();
}
Loop nesting
eg: Use loop nesting to find prime numbers from 2 to 100