C Language Learning Tutorial Chapter III-C language programming preliminary (4)

Source: Internet
Author: User
Tags expression logical operators printf

Branch Structure Program

relational operators and expressions

It is often necessary to compare the two-magnitude relationship in a program to determine the next step in the program's work. An operator that compares two quantities is called a relational operator. The following relational operators are available in the C language:
< less than
<= less than or equal to
> Greater than
>= is greater than or equal to
= = equals
!= is not equal to
Relational operators are both binocular operators, and their binding is left-bound. The precedence of the relational operator is lower than the arithmetic operator, which is higher than the assignment operator. In six relational operators, the <,<=,>,>= have the same precedence, which is higher than = = and!=,== and!=.
Relationship expressions
The general form of a relational expression is an expression relational operator expression such as: A+B&GT;C-D,X&GT;3/2, and ' a ' +1<c,-i-5*j==k+1; are all legitimate relational expressions. Because an expression can also be a relational expression. Nested situations are also allowed, such as:a> (B>c), a!= (C==d), and so on. The value of the relationship expression is "true" and "false", represented by "1" and "0".
For example: The value of 5>0 is "true", that is, 1. (a=3) > (b=5) because 3>5 is not tenable, so its value is false, that is 0.
void Main () {
Char c= ' k ';
int i=1,j=2,k=3;
float x=3e+5,y=0.85;
printf ("%d,%d\n", ' a ' +5<c,-i-2*j>=k+1);
printf ("%d,%d\n", 1<j<5,x-5.25<=x+y);
printf ("%d,%d\n", i+j+k==-2*j,k==j==i+5);
}
Char c= ' k ';
int i=1,j=2,k=3;
float x=3e+5,y=0.85;
printf ("%d,%d\n", ' a ' +5<c,-i-2*j>=k+1);
printf ("%d,%d\n", 1<j<5,x-5.25<=x+y);
printf ("%d,%d\n", i+j+k==-2*j,k==j==i+5);
In this example, the values of the various relational operators are derived. The character variable is involved in the operation with its corresponding ASCII code. For expressions with multiple relational operators, such as k==j==i+5, according to the left binding of operators, first compute K==J, the formula is not tenable, its value is 0, then calculate 0==i+5, also does not set up, so the expression value is 0.

logical operators and expressions

Logical operator C language provides three logical operators && and Operations | | OR operation! Non-operations and operators && and operators | | are both binocular operators. Has a left-binding nature. Non -
Operator! is a single-eye operator with right binding. The relationship between logical operators and other operator precedence can be expressed as follows:
According to the precedence order of the operators, you can draw:
A>b && c>d equivalent to (a>b) && (c>d)
!b==c| | D<a equivalent to ((!b) ==c) | | (D<a)
A+b>c && x+y<b equivalent to ((a+b) >c) && ((x+y) <b)
The value of the logical operation
The value of the logical operation is also "true" and "false", which is represented by "1" and "0". Its evaluation rules are as follows:
1. The result is true when the two quantities of the operation && participation operation are true, otherwise it is false. For example, 5>0 && 4>2, because 5>0 is true, 4>2 is true, and the result is true.
2. or Operation | | Two quantities of participating operations as long as one is true, the result is true. Two of the quantity is false, the result is false. For example: 5>0| | 5>8, because the 5>0 is true, the result of the phase or is true
3. Non-operation! The result is false when the number of the participants is true, and the result is true when the amount of the operation is false.
For example:! The result of (5>0) is false.
Although C compilation gives the logical operation value, "1" represents "true" and "0" represents "false". But in turn, when deciding whether a quantity is "true" or "false", the "0" represents "false" and the value of a non "0" as "true". For example, because 5 and 3 are not "0", the value of 5&&3 is "true", that is, 1.
Another example: 5| | The value of 0 is "true", that is, 1.
The general form of logical expression logical expressions is: expression logical operator expressions where the expression can be a logical expression, thus constituting a nested case. For example: (A&AMP;&AMP;B) &&c based on the left binding of logical operators, the upper form can also be written as follows: The value of the a&&b&&c logical expression is the final value of the various logical operations in the formula, and the "1" and "0" respectively represent "true" and "false ”。
void Main () {
Char c= ' k ';
int i=1,j=2,k=3;
float x=3e+5,y=0.85;
printf ("%d,%d\n",!x*!y,!!! x);
printf ("%d,%d\n", x| | I&&j-3,i<j&&x<y);
printf ("%d,%d\n",i==5&&c&& (j=8), x+y| | I+J+K);
}
In this case, the!x and!y respectively are 0,!x*!y 0, so the output value is 0. Since x is not 0, it!!! The logical value of x is 0. to X| | I && j-3, first calculated j-3 value is not 0, and then I && j-3 logical value of 1, so x| | The logical value of the i&&j-3 is 1. For I<j&&x<y, because the value of I&LT;J is 1, and X<y is 0, the value of the expression is 1, 0 phase, and finally 0, the i==5&&c&& (j=8) type, because I==5 is false, The value is 0, and the expression consists of two and operations, so the entire expression has a value of 0. For type x+ y| | I+j+k because the value of X+y is not 0, the value of the whole or expression is 1.

If statement

Use an If statement to form a branching structure. It is judged according to the given conditions to determine the execution of a branch segment. The IF statement in C language has three basic forms.

1. The first form is the basic form if (expression) statement, whose semantics is that if the value of the expression is true, the following statement is executed, otherwise the statement is not executed. The process can be expressed as the following figure
void Main () {
int A,b,max;
printf ("\ n input two numbers:");
scanf ("%d%d", &a,&b);
Max=a;
if (max<b) max=b;
printf ("max=%d", Max);
}
Enter two integers to output the large number of them.
scanf ("%d%d", &a,&b);
Max=a;
if (max<b) max=b;
printf ("max=%d", Max);

In this example program, enter two number a,b. Give a variable max first, then use the IF statement to determine the size of Max and B, if Max is less than B, then assign B to max. So Max is always a large number, and finally the value of Max is output.
2. The second form is if-else form
if (expression)
Statement 1;
Else
Statement 2;
The semantics are: if the value of the expression is true, execute statement 1, or EXECUTE statement 2.
void Main () {
int A, B;
printf ("Input two numbers:");
scanf ("%d%d", &a,&b);
if (a>b)
printf ("max=%d\n", a);
Else
printf ("max=%d\n", b);
}
Enter two integers to output the large number of them. Use the If-else statement to determine the size of the a,b, if a is large, then output a, otherwise output B.

Related Article

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.