C # Basics (2)

Source: Internet
Author: User

Program Control statements

One, select the statement:

If statement:

The If statement implements a single branch selection structure, syntax format:

if (an expression)

{

Statement block

}

Execution procedure: If the logical value of the expression is true, execute the statement block controlled by the IF statement, and if the value of the expression is false, do not execute the statement block controlled by the IF statement.

If----Else statement:

The IF---Else statement implements a two-branch selection structure with syntax formatting:

If

{

Statement Block 1

}

Else

{

Statement Block 2

}

Execution procedure: If the logical value of the expression is true, the statement block 1 in if is executed, and if the value of the expression is false, then the statement block 2 after the else is executed.

If------ELSE if----Else statement:

Implement multi-branch selection structure, syntax format:

if (expression 1)

{

Statement Block 1

}

else if (expression 2)

{

Statement Block 2

}

else if (expression 3)

{

Statement Block 3

}

、、、

Else

{

Statement block N

}

Execution procedure: If the logical value of expression 1 is true, the statement block 1 is executed, then the IF statement is completed, and if the logical value of expression 1 is false, the value of expression 2 is evaluated, if true, the statement block 2 is executed, and the IF statement is terminated, if the value of expression 2 is Continue to judge the value of the other expression, if all logical expressions have a value of false, execute the statement block N, and then end the IF statement.

Switch statement:

Implement multi-branch selection structure, syntax format:

switch (expression)

{

case constant Expression 1:

{Statement Block 1}

Break

Case constant Expression 2:

{Statement Block 2}

Break

、、、、、

Default

{Statement block n+1}

Break

}

Execution procedure: If the value of the expression in the switch statement is equal to the value of the constant expression 1, the execution of the statement block 1, in the execution of the jump statement, if not equal, and then determine whether the value of the expression is equal to the value of the constant expression 2, if equal, the execution of the statement block 2, such as unequal to continue The statement block N+1 is executed when the values of all constant expressions are unequal; (direct search for matches, regardless of case in statement, location of default)

Note: (Switch and if----else if)

Different points: switch: generally only for equivalent comparison

If---else if: can handle scope

Same point: Multi-branch structure can be implemented

Second, iterative statements:

While statement:

Statement format:

while (expression)

{

Loop body

}

Execution procedure: If the value of the expression in the while statement is true, the program code in the loop body executes, the loop body is executed, the control goes to the beginning of the while statement, executes the while statement again, and if false, ends execution of the while statement.

While statement: First judge, then execute

Do---the while statement:

Statement format:

Do

{

Loop body

}while (expression); (Error point: often easy to forget ";")

Execution procedure: First executes the program code in the loop body, executes the loop body, determines the value of the expression in the while statement, if true, control will go to the start of the Do statement, execute the loop body again, and if false, end the loop of the while statement.

Do----The while statement: Execute first, in the judgment

For statement:

Statement format:

for (expression 1; expression 2; expression 3)

{

Loop body

}

Execution process: The value of the expression 1 is evaluated, in the judgement of the value of the expression 2, if true, the loop body is executed, then the value of expression 3 is evaluated, the logical value of the expression 2 is determined again, if true, the loop body is executed, and if False, the loop statement is ended.

In general: Expression 1 is used for looping variables and assigning initial values to variables, expression 2 is a loop condition, and expression 3 is the value of changing the loop variable.

For a three-segment expression can be omitted, but not less ";"

Three, jump statement: (Own fuzzy point)

(Break statement, Goto statement, return statement, continue statement)

Break statement: You can terminate a multi-SELECT statement or iteration statement so that the control flow goes to the next statement of the statement execution. If you execute a break statement in a loop statement, exit the layer loop and execute the next sentence of the loop statement. (Immediate termination of this cycle)

Goto statement: You can implement an unconditional jump, jump to the code line specified by the tag execution (the statement can be easily transferred from one place to another statement execution, but the statement to use with caution)

Return statement: Terminates execution of the method and returns control to the calling method

Continue statement: Allows the program process to skip the remaining statements in the loop body while executing the loop body, continuing with the next round of loops

Add:

1. Program Debugging:

(1). Set breakpoints

(). Single-Step Debugging (F11: Step over: F10: Progressive commissioning)

(). Observation variables

2. In a control statement to make the program easier, a flag flag (bool type) is often defined

3. Remember to use the try-----Catch statement to detect exceptions (see C # Basics)

C # Basics (2)

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.