ASP. NET knowledge reorganizing (4) ------ process control statements

Source: Internet
Author: User

When learning C language, we have learned three types of process control statements: sequence, condition, and loop. The first few flow control statements in the Tianyi programming language ranking list seem to be almost the same,

This is the truth.

1. Sequential statement execution: as the name suggests, statements are executed in sequence

Ii. conditional statements:

1. if-else statement

1. only contains the if keyword, without else:

If (expression)
Statement1

2. if the branch contains if and else:

If (expression)
Statement1;
Else
Statement2;

3. Multiple else if statements:

If (expression1)
Statement1;
Else if (expression2)
Statement2;

Else if (expression3)

Statement2;

......

Else

Statement;

2. The switch-case statement (indicating that the knowledge point was read in the morning when the C language was used)

Switch (<testVar>)
{
Case <comparisonVal1>:
<If <testVar> is equal to <comparisonVal1>, the statement is executed.>
Break;
Case <comparisonVal2>:
<If <testVar> is equal to <comparisonVal2>, the statement is executed.>
Break;
......
Case <comparisonValN>:
<If <testVar> is equal to <comparisonValN>, the statement is executed.>
Break;
Default:
<If no statement matches <testVar> <comparisonValX>
Break;
}

When the value in case is equal to the value of the expression in parentheses after the switch, the switch statement is exited when the break statement is executed; if the expression value in parentheses is different from the constant expression after all cases, execute the statement after default and exit the switch statement.

Which of the following statements is confusing?

Case <comparisonVal1>:

Case <comparisonVal2>:

Case <comparisonVal3>:

<If <testVar> is equal to any of the preceding three values, the statement is executed.>
Break;

In this case, the three cases share one execution statement.

 

Iii. Loop statement:

1. do Loop: run the Code marked as a loop and perform a Boolean test. If the test result is true, run the code again. When the test result is false, exit the loop.

Do

{

<Code to be looped>

} While (<Test>)

2. while loop: similar to do loop, but different from do loop, the Boolean test in the while loop is performed at the beginning of the loop, rather than at the end.

While (<Test>)

{

<Code to be looped>

}

3. for Loop: The process of for loop initialization-> condition-> meet the condition-> execute the statement in the loop-> operation-> next loop; if the condition is not met, the statement in the loop is skipped.

For (<initialization >;< condition >;< operation>)

{

<Code to be loop>

 

}

In C #, The foreach statement is very popular. The foreach statement executes the loop body for each element in the array or object set once.

Foreach (type identifier in expression)
{
Staterment
}

Example: foreach (var arr in arrs)

{

Var ++;

}

The function of adding one to each number in array arrs (the var keyword here can replace any type, and the compiler will judge the specific type based on the context, those who have learned javascript should be familiar with it) and do not need to calculate the length of the array like in The for loop.

)

Iv. interrupt statements

1. break: Terminate the cycle immediately

2. continue: Terminate the current loop immediately (continue to execute the next loop)

3. goto: you can immediately jump out of the loop to the marked position (it seems that the c language pointer works, but in the java and c # Safe high-level languages, goto statements are not recommended)

4. return: jump out of the loop and the included Functions

The topic of break and continue will certainly be met by beginners. After a single loop is taken out and the entire loop body is understood, it will not be confused. The goto statement can be used if you want to study it, but it is not recommended. I remember that C ++ also has a goto statement, which has the same effect, however, when it is used more often, it will obscure the program and even lead to an endless loop. But the return statement may not be familiar to everyone. When I first started to learn C language functions, the last sentence of the int main () function is always return 0, while void main () the function does not need it. Later I summarized the type of the function (called the method in java and C #) as long as it is not the void type, a return value is required; otherwise, an error is reported.

 

For more information, see http://blog.sina.com.cn/s/blog_4c83c6bf01000823.html.

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.