C # Quick Warm up

Source: Internet
Author: User
Tags case statement

If structure
1.if structure
*IF selection structure is determined according to the conditions and then processed.
① BASIC If structure
if (condition)
{
code block
}
②IF-ELSE selection Structure
if (condition)
{
Code Block 1
}
Else
{
Code Block 2
}
③ Multiple If structures
if (condition 1)
{
Code Block 1
}
else if (condition 2)
{
Code Block 2
}
④ Nested IF structure
if (condition 1)
{
Code Block 1
if (condition 2)
{
Code Block 2
}
}


Switch structure
1. Type of expression for conditional judgment
① integer
② characters
③ string

Requirements for 2.break statements
① every case has a break
②default must have a break, too.
A break statement is not required when there are no other statements in ③case

3. Precautions
①case clauses are not ordered in sequence, can be arbitrarily adjusted
②case statement cannot have the same value
The value in ③case must be a constant expression, and no variable is allowed


While loop structure
1.while Cycle
while (loop condition)
{
Loop body
}
* Execute first, then judge

Do-while Cycle Structure
1.do-while Cycle
Do
{
Loop body
}while (cyclic condition)
* Execute first, then judge


The difference between *while cycle structure and do-while cycle structure
(1) Different syntax
(2) different execution order
(3) When the initial condition does not meet the cycle conditions
①while Loop does not execute at once
②do-while cycle is performed at least once in any case

For loop structure
Syntax and execution order for 1.for loops
for (parameter initialization; condition judgment; update loop variable)
{
Cyclic operation body
}

(1) Execution order
① First cycle: parameter initialization → condition determination → cyclic operation body → update loop variable
② cycle after the first time: condition determination → cyclic operation body → update loop variable
(2) Code specification
① format Alignment
Indentation of the ② code

foreach Loop structure
1.foreach syntax
foreach (element type element variable name in array name) {
Cyclic operation body
}

2.foreach is the traversal of all elements of a given array, so the Foreach loop structure cannot change the values of the elements in the array

3.Var
var can be understood as an anonymous type, that he is a placeholder for a variable, the type of an array
Characteristics:
1) must be initialized in the definition (var s = "abc"; Cannot: Var s; s = "AAAA")
2) Once the initialization is complete, the variable can no longer be assigned and initialized
3) var requires a local variable
4) Unlike object, VAR has the same efficiency as defining a variable using the coercion type method.
Declaring and assigning an initial value
var name = "Zhang San";
var age = 10;
var sex = true;

Get the data type of a variable
Type t_name = Name. GetType ();//String
Type T_age = age. GetType (); Int
Type t_sex = Sex. GetType (); bool

Print results
Console.WriteLine ("Variable Name is of type {0}\n, variable age is of type {1}\n, variable sex is of type {2}", T_name.tostring (), t_age.tostring (), T_ Sex.tostring ());

Do not close automatically, waiting for user input
Console.ReadLine ();

Jump statement
1.break statement:
The ① is used in the switch structure to exit a case statement.
② loop in which the program jumps out of the current loop structure and resumes execution of the loop structure after the statement

2.continue statements
Ends this loop of the current loop and starts the next loop of the current loop


The difference between cyclic applications
1.while Loop:
The while loop is the first to determine whether the conditional expression is true. If the condition is established, the loop body is executed; otherwise end loop body
2.do-while Cycle:
The Do-while loop first executes the loop body again to determine whether the expression is true. If the condition is true, the loop continues; otherwise end loop
3.for Loop:
The For loop must use an integer variable to do a loop calculator, and a conditional expression to limit the value of the counter variable to control the loop
4.foreach Cycle
A foreach loop is used to traverse all values of a given array

Array

1. An array is a variable that stores a set of data of the same data type
* So ↓
① declare a variable is to draw an appropriate space in the memory space
② declares an array is a contiguous set of spaces in the memory space

2. Array Essentials
① Identifier: The name of the array used to distinguish between different arrays
② array elements: Data to be stored in the array
③ element subscript: The array element is numbered, starting with 0, and each element in it can be accessed by subscript
④ element type: array type of array element

3. Summary:
① array length fixed, avoid array out of bounds
All elements in the ② array must belong to the same data type

4. Four steps to use an array
(1) declaring an array:
Tell the computer what the data type is
data type [] array name;
* Array length is not specified when declaring an array
(2) Allocated space:
Tell the computer to allocate several contiguous spaces
data type [] Data name = new data type [size];
(3) Assign value
Put the data in the allocated grid
* Assignment method
① Edge Declaration Edge Assignment
② dynamically input information from the keyboard and assign values to it
(4) Processing data

5. Get the length of the array:
The array name. Length
Object array

1. Array initialization
①int[] arr = new int[5]{0,1,2,3,4};
②int[] arr = new int[]{0,1,2,3,4}
* Omit length, the number of elements assigned is the length of the initial array
③int[] arr = {0,1,2,3,4}
* Omit New

2. Summary
① user-defined classes can also create arrays (object arrays)
Each element in the ② array is the type of the user-defined class

Bubble sort

1. Bubble sort: Compare adjacent two numbers each time, small swap to front, each round end after the maximum number of exchanges to the last

2. Bubble sort using double loop implementation (ascending)
For (i=0;i< array name. length-1;i++) {
For (j=0;j< array name. length-1-i;j++) {
if (array name [j]> array name [j+1])
{
int item = array name [j];
Array name [j] = array name [j + 1];
Array name [j + 1] = Item;
}
}
}

3. Bubble sort Shorthand formula (ascending)
N numbers to queue up
22 compared to a small front
Outer Loop N-1
Inner-layer Cyclic n-1-i
*n represents the number of elements (array length)
* If you want to sort in descending order, just change the greater than sign of the IF condition in the program to less than the number.

C # Quick Warm up

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.