Understand C # series/Core C #/judgment & amp; Cycle & amp; jump,

Source: Internet
Author: User

Understand C # series/Core C #/judgment & loop & jump,
Judgment & loop & jump description

This section describes the statements used to control the program flow in C #. "control program flow" means to control the program running flow.

Judgment

It is easy to understand: if ...... Just ......

If statement: test whether a specific condition is met. if yes, run some code. if not, run the code in else.
int i=0;if(i==0){    i=1;}else{    i=0;}

The program is very simple. Create an integer variable and initialize it to 0. After a judgment {if the condition is met: I is equal to 0, assign 1 to I. If the condition is not met, that is, when I is not equal to 0, I is assigned 0 }.
(X = y). Two equal signs indicate determining whether the values on both sides of the equal sign are equal. The result is a Boolean value of true or false. An equal sign is a value assignment operation, which is a frequent mistake made by new users.

Switch statement: compare a variable with multiple values and execute the code corresponding to the value if the value is equal to the value.

I have never used the switch statement, because almost everything that the switch can do can be done with if.

If you use it, note that each case ends with a break, and you can use default to implement the else effect.

Loop for Loop

For (Code executed before the first loop starts; Condition Statement-execute the next loop only if the condition is met; Code executed after each loop ends) {loop body}

For is the most powerful loop statement. It can be used to do everything other loop statements can do, but in some cases, it is relatively simple and convenient to use other loop statements, next, we will introduce when.

While Loop

While (Condition Statement-execute the next loop only if the condition is met) {loop body }.

While (true) // infinite loop {if (expression) // if a condition is met {break; // skip loop }}

A break statement is a jump statement. It is introduced in advance to exit the loop, that is, to end the loop.

Do... while loop

Do {loop body} while (Condition Statement-If yes, the next loop is executed ).

This is different from while: in any case, first execute the content in one side of the loop body, and then consider the loop.

Foreach Loop

Foreach (variable type variable name in set) {loop body}

Set a temporary variable to access each individual in the set. The number of objects in the set is repeated several times.

In the loop body, you can use this temporary variable to integrate the corresponding individual in the loop, access it, call its method, modify its attributes, but not modify the temporary variable, that is, you cannot assign values to temporary variables.

Jump statement break statement

As mentioned earlier, this function is used to jump out of a loop. In addition, break can also be used in the switch statement to exit a case statement.

If the break is placed outside the switch statement or loop, a compilation error occurs.

Continue statement

Used in the loop body to end the cycle of this round and start the next loop directly. Like the break statement, it is usually used together with the if statement.

After the continue statement is executed, the for loop is still executed (the Code executed after each loop ends ).

Return Statement

Return is used to exit the class method and return the control to the caller of the method.

If the return type of the method is void (indicating that the return value is not required), return directly using return. Otherwise, the return statement must return the value of this type. If the return type is a reference type, null is allowed ).

Goto statement

Most people have never used it ...... The running process is too messy due to the goto statement ......

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.