C # basic review 02,

Source: Internet
Author: User

C # basic review 02,

Continue to update Article 2:

1: unary operator: ++ --

++: No matter whether it is the prefix or the Postfix, the variable value will eventually add one.

The difference between the add-before and add-after operations is reflected in the calculation. If the add-after operation is performed, the original value is used in the calculation first,
After the operation is completed, add one by yourself, while add one by yourself first, and then take the value after add one to participate in the operation.

--: Whether pre-or post-subtraction, the variable value will eventually be reduced by one.

+-*/% Is a binary operator. In an expression, there are both unary operators and binary operators. You must calculate the unary operator first. The priority of the unary operator is higher than that of the binary operator.

 

2: compound assignment operator

  

+ =: Eg; a = a + 5; => a + = 5;
-=: Eg; a = A-5; => a-= 5;
* =: Same as above
/=
% =

 

3: Relational operators: used to describe the relationship between the two (not to mention)

>
<
> =
<=
=
! =

The expressions connected by Relational operators are called relational expressions. Each expression can solve a fixed value.

 

4: bool type:

The bool type is used to describe right or wrong. The bool type has only two values: true and false.

 

5: logical operators:

Logic and :&&

 

// Ask the user to enter lausu's Chinese and math scores, and output the following statements to determine whether the result is correct. The output is True, and the output is False.
// 1) lausu scored more than 90 points in both Chinese and mathematics.
Console. WriteLine ("Enter the Chinese score of laosu ");
Int chinese = Convert. ToInt32 (Console. ReadLine ());
Console. WriteLine ("Enter laosu's mathematical score ");
Int math = Convert. ToInt32 (Console. ReadLine ());

 

// Bool B = chinese> 90 & math> 90;
// Console. WriteLine (B );

 

// 2) one course of Chinese and mathematics is greater than 90 points

Bool B = chinese> 90 | math> 90;
Console. WriteLine (B );
Console. ReadKey ();

 

 

Logic or: |

 

// Runyear judgment (meets one of the following two conditions ):
// The year can be divisible by 400. (2000)
// The year can be divided by four, but cannot be divided by 100. (2008)
// Let the user enter a year. If it is a runyear, true is output. If not, false is output.

 

Console. WriteLine ("enter a year ");
Int year = Convert. ToInt32 (Console. ReadLine ());

 

Bool B = (year % 400 = 0) | (year % 4 = 0 & year % 100! = 0 );
Console. WriteLine (B );
Console. ReadKey ();

 

 

Non-logical :!

 

Values of the relational expression or bool type must be placed on both sides of the logic and the logic or operator. The priority of the logic and operator is higher than that of the logic or.

 

6. syntax structure

Sequence Structure: from top to bottom.

Branch Structure:

If structure,

Syntax:

If (condition): the condition is generally bool type value, relational expression, or logical expression.
{
Code to be executed;
}

Execution Process:
When the program runs at the if clause, it first determines whether the judgment condition carried by the if clause is true. if so, true is returned,
The code is executed in braces.
If the judgment condition is not true, that is, false is returned, the braces are skipped directly.

Syntax features: first judge and then execute. A Code may not be executed.

// Ask the user to enter the age. If the input age is greater than 23 (inclusive), it will show the user that you are married.
Console. WriteLine ("Enter your age ");
Int age = Convert. ToInt32 (Console. ReadLine ());
Bool B = age> = 23;
If (B)
{
Console. WriteLine ("You can get married ");
}
Console. ReadKey ();

 

If-else Structure

Syntax:
If (condition)
{
Code to be executed;
}
Else
{
Code to be executed;
}

Execution Process: The program runs at the if. First, it determines whether the judgment condition of if is true. if so, it runs
The code in the braces of if immediately jumps out of the if-else structure after execution.
If the judgment condition is not true, the braces included in the if statement are skipped and the code included in the braces included in the else statement is executed,
Jump out of the if-else structure.
Therefore, the if-else structure will execute at least one code.
Note: else always matches the if nearest to it.

 

The user is required to enter two numbers a and B. If a and B are fully divided or a plus B is greater than 100, the value of a is output; otherwise, the value of B is output.
// Console. WriteLine ("Enter the value of ");
// Int a = Convert. ToInt32 (Console. ReadLine ());
// Console. WriteLine ("Enter the value of B ");
// Int B = Convert. ToInt32 (Console. ReadLine ());

// If (a % B = 0) | (a + B)> 100)
//{
// Console. WriteLine ();
//}
// Else
//{
// Console. WriteLine (B );
//}

 

Select structure:

If else-if structure,

This result is used to determine multiple conditions, indicating that one of the multiple conditions is selected for execution.

Syntax:
If (condition)
{
Code to be executed;
}
Else if (condition)
{
Code to be executed;
}
Else if (condition)
{
Code to be executed;
}
Else if (condition)
{
Code to be executed;
}
Else
{
Code to be executed;
}
Execution Process: The program first checks whether the judgment condition in the parentheses of the first if is true. If yes,
Then, execute the code in the braces of the if. After the code is executed, it jumps out of the current if else-if structure.
If the judgment condition of the first if clause is not true, the next judgment is performed to determine whether the conditions in each if clause are true.
If it is true, the code in the braces included in the if statement is executed. After the execution is complete, the if else-if structure exists. If
If the condition for each if clause is not true, check whether the current if else-if structure contains the else
If else exists, the code in else is executed. If no, the Code jumps out of the current structure and does nothing.

 

Static void Main (string [] args)
{
// Evaluate the final exam score of the students (if good or if-else good) // score> = 90: A // 90> score> = 80: B // 80> score> = 70: C // 70> score> = 60: D // score <60: E
Int score = 0;
Bool B = true;
String level = "";
Console. WriteLine ("Enter your exam score ");
Try
{
Score = Convert. ToInt32 (Console. ReadLine ());
If (score> = 90)
{
Level = "";
}
Else if (score> = 80)
{
Level = "B ";
}
Else if (score> = 70)
{
Level = "C ";
}
Else if (score> = 60)
{
Level = "D ";
}
Else
{
Level = "E ";
}
}
Catch
{
Console. WriteLine ("incorrect input, program exited ");
B = false;
}

If (B)
{
Console. WriteLine (level );
}
Console. ReadKey ();
// Console. ReadKey ();
}

(The demo contains try-catch)

 

Switch-case structure.

Syntax:
Switch (variable value or expression)
{
Case value 1: code to be executed;
Break;
Case value 2: the code to be executed;
Break;
Case value 3: code to be executed;
Break;
Case value 4: code to be executed;
Break;
.....
Default:
Code to be executed;
Break;
}
Execution Process: The program first calculates the value of the variable or expression in the switch parentheses.
Match the calculated values with the values of each case in sequence.
If the match is successful, run the Code contained in the case. After the execution is complete, the break occurs,
Jump out of the current switch-case structure.
If it does not match the value of each case, check whether default exists in the current switch-case.
If default exists, the code in default is executed. If no default exists, the structure will not do anything.

 

(

If multiple conditions are judged, it is an interval judgment. If else-if.
If the value is a multi-condition value. Switch-case is recommended.

)

 

// Evaluation of the final exam score of students // score> = 90: A // 90> score> = 80: B // 80> score> = 70: C // 70> score> = 60: D // score <60: E

Console. writeline ("Enter your exam score ");
Int score = convert. toint32 (console. readline ());
String level = "";
Switch (score/10)
{Case 10: case 9:
Level = "";
Break; case 8:
Level = "B ";
Break; case 7:
Level = "c ";
Break; case 6:
Level = "d ";
Break;
Default:
Level = "e ";
Break ;}
Console. writeline (level );

 

 

 

Loop Structure:The so-called loop refers to the same thing without stopping. Even a loop ends. If a loop is alwaysWill not end, this loop is called an endless loop.

While loop,

While loop Syntax:
While (Cyclic condition) // bool type value, relational expression, logical expression
{
Loop body; // it refers to the thing that is constantly being done.
}
Execution Process: when the program runs at the while clause, it first checks whether the loop conditions in the while parentheses are true.
If the loop condition is true, the loop body in the while braces is executed.
After executing the statement, we return to the cycle condition to continue judgment. If the cycle condition is true, we continue to execute the loop body,
If the loop condition is not true, the current while loop will jump out immediately.
In a loop, there will always be a line of code that can change the cycle conditions so that they will not be valid one day,
If a line of code does not change the loop condition, this loop is called an endless loop.
The most common endless loop:
While (true)
{

}

Break keyword
1) jump out of the current switch-case structure
2) can jump out of the current loop
Break is most often used in combination with the while (true) endless loop.

 

// Calculate the sum of all integers between 1 and the sum of all odd numbers between 1 and
// Loop body: Continuous Addition
// Cycle condition: not added to 100
// Int sum = 0; // sum
// Int I = 1000;
// While (I <= 100)
//{
// If (I % 2 = 0)
//{
// Sum + = I; // sum = sum + I;
//}
// I ++;
//}
// Console. WriteLine (sum );
// Console. ReadKey ();

 

 

Do-while loop,

Syntax:
Do
{
Loop body;
} While (Cyclic condition );

Execution Process:
The program first executes the loop body in do. After the execution is complete, it judges whether the cyclic condition is true with the execution result.
If it is true, execute the do loop body. If it is not true, the do-while loop will jump out.
Features: Execute the statement first and then judge whether to execute the loop body at least once.

 

Static void Main (string [] args)
{
// Alan will be on stage tomorrow. The teacher said that he would sing the song for tomorrow. If he is satisfied, Alan will be able to go home. otherwise, you need to practice it again until the teacher is satisfied. (y/n)

// Loop body: I sang a song and asked the teacher if you are satisfied? The instructor replied
// Cycle condition: the instructor is not satisfied

// Console. WriteLine ("Teacher, I sang this one. Are you satisfied? ");
// String answer = Console. ReadLine ();

// While (answer = "no ")
//{
// Console. WriteLine ("Teacher, I will sing it again. Are you satisfied? ");
// Answer = Console. ReadLine ();
//}
// Console. WriteLine ("satisfied, you can go home ");

String answer = "";
Do
{
Console. WriteLine ("I sang this time, are you satisfied with the teacher? ");
Answer = Console. ReadLine ();
} While (answer = "no ");

 


Console. ReadKey ();
}

 

 

For loop,

Purpose: This function is used to process loops with known cycles.
Syntax:
For (expression 1; expression 2; expression 3)
{
Loop body;
}
Execution Process:
First, execute expression 1, declare the loop variable, and then execute expression 2 to determine whether the loop condition is true.
During the first loop, no expression 3 is executed, but the loop body is directly entered.
After the loop body is executed, execute expression 3 and then execute expression 2 to determine whether the loop condition is true,
If it is true, the system continues to execute the loop body. If it is not true, the system jumps out of the current for loop.

 

// How many daffodils are there between and?
// Daffodils: the number of hundred-bit cubes + ten-bit cubes = the number of hundred-bit cubes

// 153 1 + 125 + 27 = 153
// 153
// Hundred bits: 153/100
// 10: 153% 100/10
// Single digit: 153% 10
// 3*3*3


// GC Garbage Collection

For (int I = 100; I <= 999; I ++)
{
Int bai = I/100;
Int shi = I %100/10;
Int ge = I % 10;
If (bai * bai + shi * shi + ge * ge = I)
{
Console. WriteLine (I );
}
}
Console. ReadKey ();

 

 

Foreach Loop

AboutForeach loop let's take a look at this simple demo: It's hard to find C # foreach loop traversal Array

 

7: try-catch exception capture

Syntax:
Try
{
There may be abnormal code;
}
Catch
{
Code to be executed when an exception occurs;
}
Execution Process:
If an exception occurs in the code in try, even one hundred lines of code will not be executed after this line of Exception Code,
Instead, it directly jumps to the catch and executes the code in the catch.
Otherwise, if no exception occurs in the try code, the catch code will not be executed;

(Try-catchFinally means that if finally is added, the finally code must be executed at the end)

Static void Main (string [] args)
{
Int number = 0;
Bool B = true;
Console. WriteLine ("enter a number and we will print this number twice ");
Try
{
Number = Convert. ToInt32 (Console. ReadLine (); // 50a
Console. WriteLine (" ");
Console. WriteLine (" ");
Console. WriteLine (" ");
Console. WriteLine (" ");

}
Catch
{
Console. WriteLine ("the entered number is incorrect. The program exits !!!! ");
B = false;
}
If (B)
{
Console. WriteLine (number * 2 );
}

Console. ReadKey ();
}

 

 

 

8: scope of local variables

In the Main function, all declared variables are called local variables. The scope of local variables has a range.
The scope refers to the scope that can use this variable (for example, the braces can be regarded as a scope ).

 

 

(For the time being, all the demos are the most basic hope to understand. It may be a bit of a typographical problem and I hope to be patient. If something is wrong, please advise .)

 

2015-04-01

36E

I wish you a happy New Year !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.