1. Branch structure program

1. Branch structure program

1.1. Relational operators and expressions

It is often necessary to compare the size relationship between two quantities in a program to determine the next step of the program's work. The operator that compares two quantities is called a relational operator. In C language, there are the following relational operators:

Operator Function
< Less than
<&# 61; Less than or equal to
> Greater than
>&# 61; Greater than or equal to
=&# 61; be equal to
!&# 61; Not equal to

Relational operators are all binocular operators, and their associativity is left conjunction. The priority of relational operators is lower than arithmetic operators and higher than assignment operators. Among the six relational operators,<,&# 60;=,>,>= The priority is the same, higher than==and!=== And= The priority is the same.

The general form of a relational expression is:

Expression relation operator expression

Example:

a+b>c-d, x>3/2, 'a'+1<c, -i-5*j==k+1;

All are valid relational expressions. Because expressions can also be relational expressions. Therefore, nested situations are also allowed, such as a>(b>c), a!=(c==d), etc. The values of relational expressions are "true" and "false", represented by "1" and "0".

1.2. Logical operators and expressions

The C language provides three types of logical operators:&&and | | or operation! Non arithmetic and the operators&&and/or | | are both binary operators with left associative properties. Non operator! It is a monocular operator with right associativity. The relationship between the priority of logical operators and other operators can be expressed as follows: 

According to the priority order of the operators, it can be concluded that:

a>b && c>d equal to (a>b) && (c>d)

>!b==c||d<a equal to ((&#33;b)&#61;=c)||(d<a)

a+b>c && x+y<b euqal to ((a+b)>c) && ((x+y)<b)

The values of logical operations are also "true" and "false", represented by "1" and "0". The evaluation rules are as follows:

When both quantities involved in the operation are true, the result is true; otherwise, it is false. For example, if 5>0&&4>2, since 5>0 is true, 4>2 is also true, and the result of phase and phase is also true.

  1. OR operation&# 124&# 124; As long as one of the two quantities involved in the operation is true, the result is true. When both quantities are false, the result is false. For example: 5>0&# 124&# 124; 5> 8. Since 5>0 is true, the result of phase OR is also true

  2. Non computational! When the computational complexity is true, the result is false; When the computational complexity is false, the result is true. For example:! The result of (5>0) is false.

Although C compiler uses "1" to represent "true" and "0" to represent "false" when giving logical operation values. But in reverse, when judging whether a quantity is "true" or "false", use "0" to represent "false" and use non zero values as "true". For example, since both 5 and 3 are not "0", the value of 5&&3 is "true", which is 1.

For example: 5&# 124&# 124; The value of 0 is' true ', which is 1.

The general form of logical expression is: expression logical operator expression. The expressions in the expression can also be logical expressions, thus forming a nested situation. For example, based on the left associativity of logical operators, the above equation can also be written as: the value of a&&b&&c logical expression is the final value of various logical operations in the equation, represented by "1" and "0" for "true" and "false", respectively.

1.3. if sentence

The if statement can form a branch structure. It makes judgments based on given conditions to determine the execution of a branch program segment. There are three basic forms of if statements in C language.

  1. The first form is the basic form

If (expression) statement;

Its semantics are: if the value of the expression is true, execute the following statement; otherwise, do not execute the statement.

  1. The second form is the if else form

If (expression)

Statement 1;

else

Statement 2;

Its semantics are: if the value of the expression is true, execute statement 1; otherwise, execute statement 2.

Input two integers and output the larger one. Use if else statements to determine the size of a and b. If a is large, output a; otherwise, output b.

  1. The third form is the if else if form

The first two forms of if statements are generally used in the case of two branches. When there are multiple branch selections, the if else if statement can be used, which generally takes the form of:

If (expression 1)

Statement 1;

Else if (expression 2)

Statement 2;

Else if (expression 3)

Statement 3;

Else if (expression m)

Statement m;

else

Statement n;

Its semantics are: to sequentially determine the value of an expression, and when a value is true, execute its corresponding statement. Then jump outside the entire if statement and continue executing the program. If all expressions are false, execute statement n. Then continue to execute the subsequent procedures.

When using if statements, the following issues should also be noted

(1)In all three forms of if statements, the expression follows the if keyword. This expression is usually a logical expression or a relational expression, but it can also be other expressions such as assignment expressions, or even a variable. For example: if (a=5) statement; If (b) statement; All are allowed. As long as the value of the expression is non-zero, it is considered 'true'. If (a=5); The value of a middle expression is always non-zero, so subsequent statements must always be executed. Of course, this situation may not occur in a program, but it is legal in syntax。

For example, there are program segments:

if (a = b)
    printf("%d", a);
else
    printf("a=0");

The semantics of this statement are to assign the value of b to a, output the value if it is non-zero, otherwise output the string 'a=0'. This usage is frequently seen in programs.

(2)In an if statement, conditional expressions must be enclosed in parentheses and followed by semicolons.

(3)In the three forms of if statements, all statements should be individual statements. If you want to execute a set (s) of statements when the conditions are met, you must enclose this set of statements in {} to form a composite statement. But it should be noted that semicolons cannot be added after}. For example:

if(a>b)
{
    a++;
    b++;
}
else
{ 
    a=0;
    b=10;
}

1.4. Conditional operators and conditional expressions

If only a single assignment statement is executed in a conditional statement, a conditional expression can often be used to implement it. Not only does it make the program concise, but it also improves running efficiency.

What is the conditional operator? And:, it is a trinary operator, which means there are three quantities involved in the operation. The general form of a conditional expression composed of conditional operators is:

Expression 1? Expression 2: Expression 3

The evaluation rule is: if the value of expression 1 is true, then the value of expression 2 is used as the value of the conditional expression; otherwise, the value of expression 2 is used as the value of the entire conditional expression. Conditional expressions are typically used in assignment statements.

For example, a conditional statement: if (a>b) max=a; else max=b; Can the conditional expression be written as max=(a>b)? a:b; The semantics of executing this statement are: if a>b is true, assign a to max, otherwise assign b.

When using conditional expressions, the following points should also be noted:

  1. The operation priority of conditional operators is lower than that of relational operators and arithmetic operators, but higher than that of assignment operators. So max=(a>b)? a: Can b be written as max=a>b without parentheses? a:b

  2. Conditional operator? And: is a pair of operators that cannot be used separately.

  3. The combination direction of conditional operators is from right to left.