Detailed description of. net loops and logical statement blocks (basic knowledge); Detailed description of. net

Source: Internet
Author: User

Detailed description of. net loops and logical statement blocks (basic knowledge); Detailed description of. net

Loop and logical statement Blocks

I haven't written a blog for a long time. I 've been disconnected for several days. From last Friday to today, Beijing to Shanghai have crossed 1213.0 kilometers, from a familiar city to a strange city, fortunately, I have a good adaptability and I would like to thank my friends for their best practices. Everything is fine. Now, let's get started:

This article is still the basic part of. NET. It mainly describes the cycle and determines:

Loop:

For Loop

Syntax:

For (expression 1; expression 2; expression 3) {loop body ;}

Expression 1 is generally a declared loop variable, recording the number of cycles (int I = 0 ;)

Expression 2 is generally a cyclic condition (I <10)

Expression 3 is generally the code that changes the cycle condition, so that the cycle condition will not be valid one day (I ++ ).

Execution Process: The program first executes expression 1 and declares a loop variable to record the number of cycles,

Then execute expression 2 to determine whether the loop condition is true. If expression 2 returns true,

The execution cycle body. 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, execute the loop body. If it is not true, it jumps out of the for loop.

Case:

Note: In this case, "Daffodils" do not know Baidu.

Foreach loop:

Syntax:

Foreach (data type identifier in expression) {loop body}

Foreach (type identifier in expression)

{

Statement

}

Where:

Type

The identifier type.

Identifier

Indicates the iteration variable of the Set element. If the iteration variable is of the value type, the read-only variables that cannot be modified are also valid.

Expression

Object set or array expression. The type of the Set element must be convertible to the identifier type. Do not use a null expression.

Calculate the type to implement IEnumerable or declare the type of the GetEnumerator method. In the latter case, GetEnumerator should either return the type implementing IEnumerator or declare all methods defined in IEnumerator.

Statement

The embedded statement to be executed.

Case:

Note: In this case, an array of the int type is declared. The array and set will be described in later chapters.

While loop:

Syntax:

While (loop condition) {loop body ;}

Execution Process: when the program runs at the while, first judge whether the loop condition in the parentheses in the while is true,

If true is returned, the loop body is executed. After the loop body is executed again

If the loop condition is still true, the loop body is executed. If the loop condition is not true, the while loop is exceeded.

In a while loop, there will always be a line of code that can change the loop conditions so that they will not be valid one day,

If not, a line of code can change the loop condition, that is, the loop condition is always true. We call this loop.

It is called an endless loop.

The simplest and most commonly used endless loop:

while(true){}

Feature: first judge and then execute. It is possible that all loops are not executed.

Case:

Do-while loop

Syntax:

Do {cyclic body;} while (Cyclic condition );

Execution Process: The program will first execute the loop body in do. After the execution is complete, it will judge the loop condition of the do-while loop,

If it is true, execute the do loop body. If it is not true, the do-while loop will jump out.

Features: first loop, then judge, at least one loop body is executed.

Case:

Nested loop:The outer loop is used to control the number of output rows, and the inner loop is used to control the number of output columns.

For (expression 1; expression 2; expression 3) {for (expression 1; expression 2; expression 3) {loop body ;}}

For example, 99 multiplication table:

Logical statement block:

If statement:

Syntax:

If (condition) {code to be executed ;}

Condition: It is generally a relational expression or a bool value.

Execution Process: The program runs at the if. First, it judges the judgment conditions in the parentheses contained in the if statement,

If the condition is true, that is, true is returned, the code in the braces included in if is executed,

If the condition is not true, a false value is returned. The if structure is skipped and the execution continues.

If Structure Features: first judge and then execute

Case:

If-else

Syntax:

If (condition) {executed code;} else {executed code}

Execution Process: when the program runs at the if clause, it first determines whether the judgment condition in the parentheses contained in the if clause is true,

If true is returned, the code in the braces included in if is executed,

After the execution is complete, the if-else structure is displayed.

If the judgment condition in the parentheses of if is not true, that is, false is returned,

Skip the if statement, execute the statement in braces in else, and then exit the if-else structure.

If-else features: first judge and then execute, at least one piece of code must be executed.

Used to determine two situations

Note: else always matches the if nearest to it.

Case:

If else-if

Function: used to process interval-based judgments with multiple conditions.

Syntax:

If (Judgment condition) {code to be executed;} else if (Judgment condition) {code to be executed ;} else if (Judgment condition) {code to be executed ;}........ else {code to be executed ;}

Execution Process. The program first judges the judgment condition in parentheses of the first if. if the condition is true, a true value is returned,

Then, execute the code in the braces of the if. After the code is executed, the if else-if structure jumps out immediately.

If the judgment condition of the first if clause is not true, that is, if false is returned, the next judgment is continued, and each

If it is true, the code in the braces included in the if statement is executed. if it is not true, the next statement is executed,

If the condition for each if clause is not true, check whether the current if else-if structure contains else.

If else exists, the Code contained in else is executed. if else exists, the entire if-else if Shenma will not be executed.

Else can be omitted.

Case:

According to the above, you will see that if else-if will cause a long and bad looking code when there are many conditions, and we will replace it with another one.:

Switch-case

It is used to process the judgment of Multi-condition values.

Syntax:

Switch (value of a variable or expression) {case value 1: code to be executed; break; case value 2: code to be executed; break; case value 3: code to be executed; break ;.......... default: code to be executed; break ;}

Execution Process: The program runs at the switch. First, the variable or expression value in the brackets is calculated,

Then match the value with the value next to each case in sequence. Once the matching is successful, execute

The code in this case, after execution is complete, encounters a break. Jump out of the switch-case structure.

If it does not match the value of each case. Check whether the current switch-case structure exists.

Default: If default exists, the statement in default is executed. If no default exists, the switch-case structure is used.

Do nothing.

Case:

Finally, let's get an egg:

Break, continue, return:

First, the break statement is usually used in loop statements and switch statements. When the break statement is used in do-while, for, and while loop statements, the program can terminate the loop and execute the statements following the loop, generally, the break statement is always associated with the if statement, that is, when the condition is met, it jumps out of the loop. For example, note:

1) The break statement does not work for the if-else Condition Statement.

2) in a multi-tier loop, a break statement only jumps one layer outward.

Second, the function of the continue statement is to skip the remaining statements in the loop and forcibly execute the next loop. The continue statement is only used in loop bodies such as for, while, and do-while. It is often used together with the if Condition Statement to accelerate the loop. In fact, continue skips a loop and subsequent statements to perform the next loop.

Third, the return Statement returns the value of the function to the main function. For example:

The return Statement is generally in the following format:

Return expression

Or:

Return (expression)

Well, this article is here, and the case provided in this article is only, there is no running result, please handle it yourself. I hope it will help beginners. I also hope that the great gods will take us with us, pack us with force, and fly us with us...

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.