C language basics - (select programming [choose structure, conditional judgment])

Comments from friends are welcome✨✨ This chapter series is an in-depth thinking and summary of C language, and the content of C language will be continuously updated.


foreword

In the sequential structure, each statement is executed in order from top to bottom, and the next statement is automatically executed after the previous statement is executed, which is unconditional and does not need to make any judgments. This is the simplest program structure. In fact, in many cases, it is necessary to decide whether to perform a specified operation task according to whether a certain condition is satisfied, or to choose one of two or more given operations. This is the problem to choose the structure to solve.


1. Introduction to the selection process

Due to the needs of the program to deal with the problem, most programs will contain a selection structure, which requires a conditional judgment before proceeding to the next operation.
C language has two selection statements:
(1) if statement , used to realize the selection structure of two branches;
(2) switch statement , used to realize the selection structure of multiple branches.

Second, the selection structure of the if statement

2.1. The general form of the if statement is as follows:

if (expression) statement 1
[else statement 2]
The "expression" in the if statement can be a relational expression, a logical expression, or even a numerical expression. Among them, the most intuitive and easy to understand are relational expressions. A relational expression is a formula for comparing two values. In the general form of the if statement above, the part enclosed in square brackets (the else clause) is optional and may or may not be present.
Statement 1 and statement 2 can be a simple statement, a compound statement, or another if statement
(that is, one if statement includes another or more embedded if statements).
According to the general form of the if statement, the if statement can be written in different forms, the most commonly used are the following three forms:
(1) if (expression) (without the else clause part)
statement 1
(2) if (expression) ( There is an else clause part)
statement 1
else
statement 2
(3) if (expression 1) (in the else part, there are multiple layers of if statements nested) statement
1
else if (expression 2)
statement 2
else if (expression 3)
Statement 3
...
else if (expression m)
statement m
else
statement m+1

Explanation:
(1) The entire if statement can be written on multiple lines or on one line.
Such as:
if(x>0) y=1; else y = -1;
however, for the clarity of the program, it is recommended to write in a sawtooth form.
(2) In the general form, "statement 1", "statement 2", "statement m" and so on in (3) are "embedded statements" in the if statement. They are part of an if statement. There should be a semicolon at the end of every embedded statement, because semicolons are required in statements.
Such as: if (x>0)
y = 1; //There must be a semicolon at the end of the statement
else
y = -1; //There must be a semicolon at the end of the statement
Cannot be written as:
if(x>0) y = 1 else y= - 1; //Missing semicolon at the end of "Statement 1"
Without this semicolon, a syntax error occurs.
(3) No matter how many lines the if statement is written on, it is a whole and belongs to the same statement. Don't mistake the if part for one statement and the else part for another statement. Don't assume the semicolon is the end of the if statement. When the system compiles the if statement, if it finds the end of the embedded statement (a semicolon appears), it also needs to check whether there is an else after it. If there is no else, it is considered that the entire if statement is over. If there is an else, the else clause is regarded as part of an if statement. Note that the else clause cannot be used alone as a statement, it must be a part of the if statement, paired with if.

(4) "Statement 1" Statement 2"..."Statement m" can be a simple statement or a compound statement including multiple statements. Note: compound statements should be enclosed in curly braces. (5
) The embedded statement can also be an if statement, which also includes an else part.
(6) In the if statement, the given condition should be checked to determine whether the given condition is true. The
result of the judgment is a logical value "Yes" or "No".

2.2. Choice nesting of if statement:

One or more if statements contained in an if statement is called nesting of if statements.
Its general form is as follows:
if()
if() statement 1
else statement 2
else
if() statement 3
else statement 4

The pairing of if and else should be noted. The else is always paired with the nearest unpaired if above it.

3. The selection structure of the switch statement

The if statement has only two branches to choose from, but multi-branch choices are often needed in practical problems.
Of course, these can be handled with nested if statements, but if there are many branches, the number of nested if statements will be many, the program will be lengthy and the readability will be reduced. C language provides switch statement to directly handle multi-branch selection.
The switch statement is a multi-branch select statement.
The general form of a switch statement is as follows:

switch(expression)
{ case constant 1: statement 1 case constant 2: statement 2 ... case constant n: statement n default: statement n+1 }





Explanation
(1) The "expression" in the parentheses in the general form of switch above should be of integer type (including character type).
(2) Inside the curly braces below the switch is a compound statement. This compound statement consists of several statements, which is the statement body of the switch statement. The statement body contains multiple statement lines beginning with the keyword case and at most one line beginning with default. case is followed by a constant (or constant expression), such as: case 'A', both of them and default play the role of labels (label, or label, mark), used to mark a position. When executing a switch statement, first calculate the value of the "expression" behind the switch, and then compare it with each case label. If it is the same as the constant in a certain case label, the flow will go to the statement behind the case label. If there is no case constant matching the switch expression, the flow goes to the statement following the default label.
(3) There may be no default label. At this time, if there is no case constant matching the switch expression, no statement will be executed, and the flow will go to the next statement of the switch statement.
(4) The order in which each case label appears does not affect the execution result. For example, the default label can appear first, then "case 'D':...", and then "case 'B':..." 0 (5)
Each case constant must be different from each other; otherwise, there will be contradictions (There are two or more execution plans for the same value of the switch expression).
(6) The case label only serves as a marker. When the switch statement is executed, the matching entry label is found according to the value of the switch expression, and the condition check is not performed here. After the statement behind a case label is executed, it is executed from this label without further judgment.
Notice:In general, after executing a case clause, the break statement should be used to make the process jump out of the switch structure, that is, to terminate the execution of the switch statement. There is no need to add a break statement in the last case clause (now the default clause), because the process has reached
the end of the switch structure.
(7) Although more than one execution statement is included in the case clause, it is not necessary to enclose it in curly braces, and all the statements after the label of this case will be automatically executed sequentially. Of course, curly braces can also be added.
(8) Multiple case labels can share a set of execution statements, for example:
case 'A':
case 'B':
case 'C': printf(">60\n"); break;
when the value of grade is 'A ', 'B', and 'C' all execute the same set of statements, output ">60", and then wrap.

Fourth, the selection structure of the ternary (conditional) operator

The conditional operator consists of two symbols (? and :) that must be used together. Three operation objects are required, called the ternary (element) operator, which is the only ternary operator in the C language.
The general form of a conditional expression is
expression1? Expression 2:
Explanation of Expression 3:

(1) Execution order of conditional operators:
first solve expression 1, and if it is not 0 (true), then solve expression 2, and the value of expression 2 is taken as the value of the entire conditional expression.
If the value of expression 1 is 0 (false), expression 3 is solved, and the value of expression 3 is the value of the entire conditional expression.
Assignment expression
max=(a>b) ? a:
The execution result of b is to assign the value of the conditional expression to max, that is, to assign the larger of a and b to max.
(2) The conditional operator is prior to the assignment operator, so the process of solving the assignment expression is to solve the conditional expression first, and then assign its value to max.
(3) The above example uses the value of the conditional expression and assigns it to a variable max.
In fact, instead of assigning the value of the conditional expression to a variable, assign a value to max in expression 2 and expression 3 in the conditional expression, and add a semicolon after the conditional expression to become an independent statement . Such as:
a>b?(max=1):(max=b);//expression 2 and expression 3 are assignment expressions
equivalent to:
if(a>b) max = a;
else max = b ;
condition The expression can also be written in the following form:
a>b ? printf(“%d”,a) : printf(“%d”,b),
that is, “expression 2” and “expression 3” can not only be numerical expressions, Can also be an assignment expression or a function expression.
The above conditional expression is equivalent to the following if ... else statement:
if ( a>b)
printf(“%d”,a);
else
printf(“%d”,b);

V. Summary

In practical applications, in many cases, it is necessary to decide whether to perform a specified operation task according to whether a certain condition is satisfied, or to choose one of two or more given operations.
Therefore, choosing a structure is very important and frequently used in practical applications.

Guess you like

Origin blog.csdn.net/weixin_44759598/article/details/128522647