. NET Foundation 3

Source: Internet
Author: User

1. Cyclic structure

  3 Loop statements: While, Do-while, for

In the face of the cycle we should note:

1. What does the loop do? (The thing that repeats--that is, the content of the loop body)

2. What is the termination condition of the cycle? (cyclic condition)

Features of 3 cycles:

Characteristics of the While loop: first judgment, in the execution

Characteristics of Do-while cycle: first execution, after judgment. The loop body will be executed at least once.

Characteristics of the FOR loop: the loop used for the number of known loops. Syntax: for (expression 1; expression 2; expression 3) {loop-body}

Expression 1: Used to define a loop variable and assign an initial value to a loop variable. (for example, int i = 0;)

Expression 2: Cyclic conditions (e.g., I < 10;)

Expression 3: Change the value of the loop variable (for example, i++;)

Execution process: (1) Calculation expression 1, Steering (2)

(2) evaluates the expression 2, if the value of expression 2 is true, turns (3); If the value of expression 2 is false, turn to (5)

(3) Performing the Loop body, steering (4)

(4) Execution Expression 3, steering (2)

(5) End of cycle

The three expressions for the For loop can be omitted, and if the expression 2 is omitted, it becomes a dead loop. for (;;) {}

In doing the project, we should use these 3 kinds of cyclic structure according to local conditions.

2. Break and Continue

The effect of break:

    1. Can be used in switch-case judgment to jump out of switch

2. In the loop, you can jump out (stop) the loop immediately. Note: Jumping out is the loop where the break is.

Continue   The role of:

1. In the loop, you can immediately end this cycle , no longer execute the following loop body statement, directly into the next loop.

2. For do-while and white directly to the next cycle condition judgment, if the condition is established, then again into the loop.

3. For a For loop, the expression 3 is executed before the loop condition is determined to be successful.

3. Ways to jump out of a double loop

  (1) Setting flag flags

(2) Bounce inside loop, flag = True

(3) in the outer loop, judging: flag is true when jumping out of the outer loop.

4. Enumeration

1. An enumeration is also a type, which is essentially a value type.

The role of enumerations: In order to fix the "scope" of a variable , it is also convenient for the programmer to use; Restrict users from arbitrarily assigning values, and can only be selected in the values enumerated when defining enumerations.  

[public] enum enum name

{

member 1 , member 2, ..., member n

} 

2. Note

(1) in C #, enumerated items are constants, and when you define an enumeration, you need to specify all the values of the enumeration

(2) cannot define method /Property /Event, value cannot be int type when enumeration is defined

(3) The value of the enumeration has a default number at the time of definition, numbering starting from 0

(4) When multiple enumerated items have the same numeric value, the first equal enumeration item is returned when the value is strongly turned

(5) Variables of enum type can be output directly, can be cast to an int type

(6) Convert a string to an enumeration type: (enumeration name) (Enum.parse (typeof (enumeration name), "string to be converted");

The enumeration type is converted to a string : The enumeration variable name. ToString ();

(7) The definition of enumerations, general and class definitions at the same level. In this way, all classes under the same namespace can use this enumeration.

  3. Code

When programming to assign a value to the enumeration variable, we write "Status s =" When we can press the space bar or ctrl+j, the prompt will appear.

1      Public enumStatus2     {3Open =1,4Continue2 =2,5Continue =2,6Close =2,7     }8 9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             //reciprocal conversions of enumeration values and int -Status STA =Status.close; the             //When multiple enumeration items have the same numeric value, the first equal enumeration item is returned when the value is strong -Console.WriteLine (Status.Close.ToString ());//output: Continue2, not close -Console.WriteLine ((int) STA);//Output: 2 -  +             intStaint =2; -             //The enumeration value of the first same value is taken when the value is transferred to the enumeration values +Console.WriteLine (Status) staint);//Output: Continue2 A  at             LongL =Ten; -Testenum (l);//Output: Ten -  -             //string Goto enumeration value -             stringStre ="Continue"; -Status Sta2 = (status) enum.parse (typeof(Status), stre); inConsole.WriteLine (STA2);//Output: Continue2 - console.readline (); to         } +  -         Static voidTestenum (Longs) the         { *             //int to enumeration, if the value of int exceeds the range of the enumeration, the original value remains the same, or int $ Console.WriteLine (Status) s);Panax Notoginseng         } -}

    

  

. NET Foundation 3

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.