C language - branch statement

1. What is a statement?

C statements can be divided into the following five categories:
1.Expression statement
2. Function call statement
3. Control statements
4. Compound statement
5. Empty statement
Among them, control statements are used to control the execution flow of the program to realize various structural methods of the program. They are composed of specific statement definers. There are nine types of control statements in C language.
Can be divided into three categories:
1. Conditional judgment statements are also called branch statements: if statements, switch statements;
2. Loop execution statements: do while statement, while statement, for statement;
3. Turn to statements: break statement, goto statement, continue statement, return statement.

2. Branch statement

2.1 if statement

2.1.1 Syntax of if statement

illustrate:

1. When the program uses if, it will first determine whether the expression after if is true or false. When the expression is true, the following statement will be executed (if is followed by a statement by default); when the expression is false, then The statement will not be executed.

2. In C language, 0 means false and non-0 means true.

3. If you want to execute multiple lines of code after the condition is established, you can use { } to include the multi-line statement. In this case, a pair of { } becomes a code block.

2.1.2 Floating else

When you write code like this, you think that what matches the else below is the if aligned with it, but in fact this is the code that deceives you.

Notice:

Matching of else : else matches the nearest if .

2.1.3 Pitfalls

The result of executing the following code is:

 Answer analysis:

The original idea of ​​the above code should be: loop 10 times, and print the result of i if i==5 each time it loops.

But the == in the expression in the if statement is written as an assignment, which is equivalent to setting the value of i to 5 every time the loop is run. 5 is true, so 5 will be printed every time.

Every time i is changed to 5 for printing, the value of i will never be equal to 10, thus causing an infinite loop.

Therefore: printing 5 in an infinite loop

2.2 switch statement

2.2.1 Syntax of switch statement

 Among them, the expression in () must be an integer, and then let us see what a statement item is

Statement items are actually some case statements. What follows the case is the value of the integer expression. When your integer expression is obtained, the system will feedback to you the statement after the specific case.

2.2.2 break in switch statement

In the switch statement, take the following code as an example

 This is syntactically correct code, but when we assign 1 to day, we cannot get the desired result, as shown in the figure

In fact, it is as if you are defining a character array using char:

char arr[ ]={'c','s','d','n'};

The system will keep reading until it encounters \0, which will also cause us to print out unsatisfactory results. We need to manually add a \0 at the end.

The function of break is actually similar to \0, both of which terminate the system's continued reading (emphasis on 2.2.5) . So when we add break at the end of each line, we can get the ideal result. The code is as follows:

2.2.3 Simplification in switch statement

If we want to change the title of the above code to "If today is 1-5, then display weekday; if today is 6 or 7, then display weekend." Do we still need to write each line again? In fact, you can omit part of it to make the code simpler.

 It is equivalent to using the continue reading function in switch to merge similar items.

2.2.4 default clause

But if someone deliberately messes with it and assigns day a value greater than 7 or less than 1, will the system go wrong? We can use the default clause here.

When the value of the switch expression does not match the values ​​of all case tags , the statement following the default clause will be executed.

Therefore, only one default clause can appear in each switch statement .

 2.2.5 Other points that need attention

1. The default clause can appear anywhere in the statement list , and the statement flow will execute the default clause just like executing a case label.

2.   If there is no break statement after each case of switch , the program will be executed in order from top to bottom, including executing the default statement.

3. The case expression in the switch statement does not require an order; the default clause in the switch statement can be placed in any position, and there is no requirement that case must be placed before default (generally, it is best to place case before default)

Guess you like

Origin blog.csdn.net/m0_75186846/article/details/131776949
Recommended