C language programming choice structure programming

1 Conditional judgment

1.1 The meaning of conditional judgment

The result of conditional judgment is a logical value: "Yes" or "No". In computer language, "True" and "False" are used to represent "Yes" or "No".

1.2 Relational operators and relational expressions

The symbols used to compare sizes are called relational operators.

1.2.1 Relational operators and their order of precedence

C language provides 6 kinds of relational operators:
<less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
! =Not equal to the
first four relational operators have the same precedence level, and the latter two are also the same. The first four are higher than the latter two. For example: ">" has priority over "==", ">" and "<" have the same priority.
The priority of relational operators is lower than that of arithmetic operators, and the priority of relational operators is higher than that of assignment operators.

1.2.2 Relational expressions

A formula that connects two expressions (arithmetic expressions, logical expressions, assignment expressions, character expressions) with relational operators is called a relational expression.
For the structure of relational operations, the value "1" represents "true" and the value "0" represents "false".

1.3 Logical operators and logical expressions

Need to use two (or more expressions) and logical operators.

1.3.1 Logical operators and their order of precedence

C language provides three kinds of logical operators:
&& logical and and
|| logical or or
! Logical not not
"&&" and "||" are binocular (meta) operators, requiring an operation on each side of the operator Object, such as a>b || x>y.
"!" is an item (meta) operator, and only requires an operation quantity on its right side, such as !a.

Truth table of logical operations
Insert picture description here
Priority:
Insert picture description here

1.3.2 Logical expression

A formula that connects relational expressions or logical quantities with logical operators is a logical expression.
When judging whether a quantity 2 is "true", "0" represents false, and non-zero represents "true", that is, a non-zero value is regarded as "true".
The result of logical operation is either 0 or 1.
Such as: a=4 The value of a is non-zero and is considered "true".
Truth Table for Logical Operations
Insert picture description here

2 Use the if statement to realize the selection structure

2.1 General form of if statement

2.1.1 if(expression) statement
For example:
if(x>y) printf("%d",x);
Insert picture description here

2.1.2 if (expression) statement 1 else statement 2
such as;
if(x>y)
printf("%d",x);
else
printf("%d",y);

Insert picture description here

2.2 Explanation of the use of if statement

1. The expression after if is generally a logical expression or a relational expression, and can also be any numeric type (integer, real, character, pointer data). E.g:

Insert picture description here
2. The else clause cannot be used alone as a statement, and must be paired with if. The else is always paired with the most recently unpaired if on it.
3. The operation sentence after if and else can have multiple operation sentences. At this time, use "{}" to enclose several sentences into a compound sentence.
Insert picture description here

2.3 Use nested if statements to achieve multi-level conditional judgment

if (Expression 1) statement 1
the else if (Expression 2) statement 2
the else if (Expression 2) statement. 3
.
...
the else if (expression m) statement m
the else statement n

For example:
Insert picture description here
improve:
Insert picture description here

3 Use the switch statement to realize the multi-branch selection structure

switch(expression)
{ case constant expression 1: statement 1 case constant expression 2: statement 2 case constant expression n: statement n default: statement n+1 } For example: output percent score according to the test score Section Description: 1. The "expression" in parentheses after switch can be numeric or character data. 2. When the value of the expression is equal to the value of the constant expression after a certain case, the statement after the case is executed. If all the constant expression values ​​are useless to match the expression, the statement after default is executed. 3. The value of the expression in each case must be different from each other. 4. The order of appearance of each case and default does not affect the execution result. 5. If the statement following a case is executed, the flow control transfers to the next case to continue execution. 6. To terminate the execution of the switch statement, a break statement can be used to achieve this goal. E.g:







Insert picture description here







Insert picture description here
Insert picture description here

4 Comprehensive example of the program

4.1 Determine whether a year is a leap year.
The conditions for a leap year should meet one of the following two conditions:
one, divisible by 4, but not divisible by 100.
Second, it can be divisible by 4, and it can be divisible by 400.
Insert picture description here

5 Improved part

5.1 Use conditional expressions to implement a simple selection structure

General form:
expression 1? expression 2: expression 3
where "?:" is a conditional operator. Three operands are required, which are called ternary (meta) operators.
The conditional expression is a selection structure.
Operation process:
Insert picture description here
Expression 2 and Expression 3 can be not only numerical expressions, but also assignment expressions or function expressions. Such as:
Insert picture description here

The type of expression 1 can be different from the types of expression 2 and expression 3. For example:
Insert picture description here
if the integer variable x is 0, the value of the conditional expression is'b'.
Priority
Insert picture description here
The combination direction of conditional operators is "from right to left". Such as:
Insert picture description here
equivalent to
Insert picture description here

5.2 Use conditional expressions in programs

Input a character and judge whether it is an uppercase character, if it is, convert it to a lowercase character; if it is not, do not convert it and output it directly.
Insert picture description here

6 Summary

Arithmetic operators, relational operators, logical operators and arithmetic expressions, relational expressions, and logical expressions. Use 1 to represent true and 0 to represent false.
Use the if statement to implement the selection structure, and use the switch statement to implement the multi-branch selection structure. The case constant expression only serves as a statement label.
break statement.
Conditional operator.

Guess you like

Origin blog.csdn.net/qq_45059457/article/details/113858397