[C# Introduction] Day 2: Operators and Code Structure

1. Operators

1. Arithmetic operators

Common arithmetic operators: + - * / % ++ –
/ (division): division of integers, the quotient is an integer, if one of them is a decimal, the quotient is a decimal
Example 1: 5 / 3 = 1
Example 2: 5 / 3.0 = 1.66666666666667
% (modulo operation, take the remainder), for example: 5 % 3 = 2
Note :
The difference between i++ and ++i:
used alone, the effect is the same, both are i+1,

int i = 0;
// ++i;
i++;
Console.WriteLine(i); // ++i和i++一样,均显示1
Console.ReadKey();

Used together with other operators or statements, i++ uses the value before self-increment (use i), ++i uses the value after self-increment (use i+1)

int i = 0;
//Console.WriteLine(i++); // 显示0
Console.WriteLine(++i); // 显示1
Console.ReadKey();

2. Relational operators

==: Equal
!=: Not equal
>: Greater than
>=: Greater than or equal to
<: Less than
<=: Less than or equal to
relational operators, you can finally get a bool (Boolean) value, that is, true or false, for example: 3 < 5 result is true

Note : Equality uses 2 equal signs (==) instead of 1 equal sign (=). C# uses 1 equal sign for conditional judgment and will directly prompt an error, which can avoid this problem

3. Logical operators

&&: AND
||: OR
!: Not
Logical operator is the operation between bool type variables
&&: same as true is true, one false is false
||: one true is true, both false is false
Note :
|| When the first condition is true, subsequent code will not be executed. Most development languages ​​are the same as C#, but VBA is different (if you want to know, please try it yourself)

bool a = true;
int b = 0;
if (a || ++b > 0) { // 因为a为true,条件直接成立,++b不会执行,b为0
    Console.WriteLine(b); // 如果把||改成&&,++b就会执行,则会输出b为1
}
Console.ReadKey();

4. Bitwise operators

&: bitwise AND
|: bitwise OR
^: bitwise XOR
<<: left shift
>>: right shift

5. Other operators

con ? A : B if the condition con is true, the result is A, otherwise
B

2. Operator precedence

If there are parentheses, calculate the ones inside the parentheses first, regardless of the priority of the operator. Unary
operation is the highest: ! ++ – Binary
operation is second (multiply and divide first): * / %
      (addition and subtraction after): + -
shift operator: >> <<
Relational operators: < <= > >= == !=
Bitwise operators: & | ^ Logical
operations: && ||

3. Code comments

//: Single-line comment
/* */: Block comment
///: Documentation comment (the difference between the single-line comment and the single-line comment has not been obtained yet, it will be clear later, and it will be added)
#region #endregion: Block preprocessing, you can fold the code
insert image description here

4. Code structure

There are three basic structures of the code: sequential structure, branch structure and loop structure.

1. Sequential structure

Code execution order: from top to bottom, line by line

2. Branch structure

a).if…else…combination, just pay attention to the writing of else if, most of them use else if, but there are exceptions, for example, python uses elif

int score = 70;
if (score < 60) 
{
    Console.WriteLine("差");
}
else if (score >= 60 && score < 80)
{
    Console.WriteLine("一般");
}
else {
    Console.WriteLine("良好"); 
}

Console.ReadKey();

b). Switch...case combination,
the types that can be stored in C# switch: bool, char, string, integer, enumeration or the corresponding type that can be null
In C language, switch can only hold integer and character types, yes You can't put strings, C# and java can put strings
insert image description here

3. Cycle

while: when the number of loops is unknown, for example: 2^n > 50, find the smallest integer n

do...while...: It will be executed once, and the usage scenario is the same as while

for loop: the most used, when the number of loops is known, use
for (int i = 0; i < 10; i++) {}
int i = 0; initialize i, count from 0
i < 10; specify the number of loops 0- 9. Execute 10 times; or understand that the condition for jumping out of the loop is i = 10
i++; i is self-incrementing

End loop: break

Jump out of the current situation and continue to execute the next cycle: continue

···
for (int i = 0; i < 10; i++)
{ if (i == 1) { continue; // will not print 1 } if (i == 5) { break; // will not print 5 ,6,7,8,9 } Console.WriteLine(i); } Console.ReadKey(); ···











ps: The for loop can actually solve the problem of 2^n > 50, finding the smallest integer n, just set n to a larger integer, and when the condition is met, use break to jump out of the loop (readers please try it yourself~)

foreach: Applicable to object collections (eg: arrays, lists, etc.), no need to know the index (number)
insert image description here

Guess you like

Origin blog.csdn.net/guggle15/article/details/123335310