C # basic organization 2

Source: Internet
Author: User

1. Basic syntax of the if Structure

If (condition)

Statement 1;

Execution Process: first, judge the result of the condition. If the condition is True, Statement 1 is executed. If the condition is false, Statement 1 is skipped and subsequent statements are executed.

1) if conditions in parentheses must be computed as a Bool value.

2) by default, the if statement can only contain one sentence (if no parentheses are added ).

  1. If-else Structure

If (condition)

{Statement 1 ;}

Else

{Statement 2 ;}

Execution Process: if the condition is True, statement Block 1 with if is executed and statement Block 2 with else is skipped .. If the condition is false, skip statement Block 1 in if and execute statement Block 2 in else.

NOTE: For the preceding two statement blocks, if the condition ends with True or False, one statement is always executed.

  1. If-else if Structure

In the if-else if statement, the next if statement is entered and the condition judgment after the if statement is performed only when a condition is not met, once the condition after an if statement is true, the block containing the if statement is executed. After the statement block is executed, the if-else if structure is immediately displayed, if none of the if conditions are true, if the last statement to be executed is else, the statement included in else will be executed; otherwise, nothing will be executed.

  1. Switch-case structure

Switch (expression)

{

Case value 1: Statement Block 1;

Break;

Case value 2: Statement Block 2;

Break;

Default: Statement block 3;

Break;

}

Execution Process: first, calculate the expression in parentheses, and then execute the statement following the matching item based on the calculation result and the value following the matching case, until the break statement jumps out of switch-case. If all the case values do not match, the statement after the Default statement is executed until the break ends. If the default statement does not exist, the switch-case statement jumps out and nothing is executed.

  1. If-else comparison between if and switch

Similarities: multiple branch structures can be implemented.

Difference: switch: normal. It can only be used for equivalent comparison.

If-else if: processing range.

  1. Loop Structure

1) while loop syntax

While (condition) // The condition is called a loop condition.

{

N programs to be executed cyclically; // The loop body
}

Execution Process:

(1) judge the cycle condition first. If the condition is true, turn to (2). If the condition is false, turn to (3 ).

(2) execute the loop body. After the loop body is executed, turn to (1 ).

(3) jump out of the loop and the loop ends.

Note: In the loop body, you must change the value of a variable in the loop condition so that the cycle condition is true one day.

Features: Judge first and execute.

2) do-while loop syntax

Do

{
} While (condition );

Execution Process: (1) execute the loop body. After the execution, turn to 2.

(2) Determine whether the condition is true. If the condition is true, 1 is switched. If the condition is false, 3 is switched.

(3) jump out of the loop and the loop ends.

Note: When a loop condition is added, it is not true at the beginning. For a while loop, it is not executed at once. For a do-while loop, it is executed once. Therefore, the do-while loop is executed once.

While first judge, then execute.

Do-while is executed first and then judged.

  1. Program debugging

1) set breakpoints

2) one-step operation

3) Observe Variables

  1. For Loop

For (expression 1; expression 2; expression 3)

{

Loop body;
}

Note: Generally, expression 1 is used to define cyclic variables and assign initial values to cyclic variables. Expression 2 is the cyclic condition, and expression 3 is the value used to change the cyclic variable.

Execution Process: (1) Execution expression 1, move to step 2.

(2) Judge expression 2 (Cyclic condition). If expression 2 is set to true, go to step 3. If the expression value is set to false, go to step 2.

(3) execute the loop body and move to step 4.

(4) execute expression 3 and move to step 2.

(5) The loop ends and jumps out of the loop.

  1. Break and continue

(1) break is used in a loop to exit the current loop.

1) it can be used to determine the switch-case and jump out of the switch.

2) used in a loop to immediately exit (terminate) the loop

Note: When used in a loop, the loop where the break is located exists.

(2) continue immediately ends the cycle and determines the cycle condition. If it is true, it enters the next cycle. Otherwise, it exits the loop.

Note: in a loop, once the program executes the continue statement, it immediately ends the loop (that is, the statement below the continue in the execution loop body ), directly go to the next loop (do-while/while) to directly judge the next loop condition. If the condition is true, then go to the loop again. For a for loop, first execute expression 3 to determine whether the loop condition is true ).

  1. Section

(1) sequential execution of Ordered Structure statements

(2) The branch structure statement is executed when the condition is met.

If-else if switch Table 1? Table 2: Table 3 (ternary Operation)

(3) loop structure: the statement is executed cyclically multiple times when the condition is met.

While do-while

(4) Jump statement

Break continue goto

(5) What is a ternary expression Table 1? Table 2: Table 3

Execution Process: first, expression 1 is calculated. Expression 1 should be a value that can be computed as a bool type. If expression 1 is set to true, the value of expression 2 is used as the value of the entire expression, if the expression value is false, the value of expression 3 is used as the value of the entire expression.

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.