C# basics ④-arithmetic operators (pre-add, post-add, pre-subtract, post-subtraction), relational operators, logical expressions

table of Contents

One, arithmetic operators

Two, relational operators

Three, logical expression

Four, actual combat drills


One, arithmetic operators

Operator

description

+

Add two operands

-

Subtract two operands

*

Multiply two operands

/

Divide

%

Remainder operation

++

Self-increment operation, i++, ++i

--

Subtraction operation, i--,--i

Unary operators: ++ (pre-add, post-add), - (pre-subtract, post-subtract)

Add: i++

 //不参与运算:自身加1
int num = 10;
int number = num++;            

Console.WriteLine(number);  
Console.WriteLine(num);
Console.ReadKey();

Output result: 10 11

                                


  //参与运算:先赋值,自身加1
int num = 10;
int number = 10 + num++;   

Console.WriteLine(num);
Console.WriteLine(number);
Console.ReadKey();

Output result: 11 20

                                          


Add before: ++i

//不参与运算:自身加1
int num = 10;
int number;
number = ++num;  

Console.WriteLine(num);
Console.WriteLine(number);
Console.ReadKey();

Output result: 11 11

                                            


//参与运算:先自身加1,再赋值
int num = 10;
int number = ++num + 10;   

Console.WriteLine(num);
Console.WriteLine(number);
Console.ReadKey();

Output result: 11 21

                                                    

Jingle:

When not involved in calculation: i++: add 1 to itself

                             ++i: add 1 to itself

When participating in calculations: i++: first assign value, then add 1 to itself

                         ++i: First add 1 to itself, then assign

 


Two, relational operators

Operator

description

==

If the two operands are equal, the condition is true

!=

If the two operands are not equal, the condition becomes true

>

Operand 1> Operand 2, then the condition is true

<

Operand 1 <Operand 2, then the condition is true

>=

Operand >= Operand 2, then the condition is true

<=

Operand <= Operand 2, then the condition is true

int number = 10;          //赋值
bool result1 = 20 == 20;  //等于,相等
bool result2 = 20 != 15;  //不等于
bool result3 = 20 >= 15;  //大于等于
bool result4 = 20 <= 15;  //小于等于

Console.WriteLine(result1);
Console.WriteLine(result2);
Console.WriteLine(result3);
Console.WriteLine(result4);
Console.ReadKey();

Output result: True True True False

 


Three, logical expression

Operator

description

Jingle

&&

Logical and

All leave

||

Logical OR

True all true, all false is false

!

Logical negation

Negative, one true and one false

Expression 1

Expression 2

Expression 1 && expression 2

true

true

true

true

false

false

false

true

false

false

false

false

                                                                 Rushing: One holiday and all holiday

 

 

Expression 1

Expression 2

Expression 1 || expression 2

true

true

true

true

false

true

false

true

false

false

false

false

                                                                             Rhetoric: one true is all true, all false is false

 

Expression 1

!Expression 2

true

false

false

true

                                                                  Rhetoric: Take the opposite, one true and one false

 


Four, actual combat drills

① Let the user input Chinese and mathematics scores, and output to judge whether it is correct or not, output True for correct and False for error


//1.用户的语文和数学成绩都大于90分
Console.WriteLine("请输入语文成绩:");                //提示用户输入语文成绩
int chinese = Convert.ToInt32( Console.ReadLine());  //将接收到的字符串转换为int类型

Console.WriteLine("请输入数学成绩:");                //提示用户输入数学成绩
int math = Convert.ToInt32(Console.ReadLine());      //将接收到的字符串转换为int类型
bool result1 = chinese > 90 && math > 90;            //判断:语文成绩>90与数学成绩大于>90
             
Console.WriteLine(result1);                          //输出结果
Console.ReadKey();



//2.语文和数学有一门是大于90分的
bool result2 = chinese > 90 || math > 90;        

Console.WriteLine(result2);
Console.ReadKey();

 

②Judging the leap year problem

Year can be divisible by 400;
year can be divisible by 4 but not divisible by 100

Console.WriteLine("请输入年份");
int year = Convert.ToInt32(Console.ReadLine());

bool result = (year % 400 == 0) || (year % 4 ==0 && year % 100 != 0);

Console.WriteLine(result);
Console.ReadKey();

 

The next content continues, come on! !

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/108185837