C Language Foundation Lesson two-----Boolean types, relational operators, logical operators,

Source: Internet
Author: User
Tags arithmetic operators

/*

Boolean type:

1.c89 does not have a Boolean type, C99 has a Boolean type. C Engineering support C89,OC Engineering C99.

2. Two types of values:

BOOL True (TRUE) False (false) C language

BOOL YES (True) NO (false) OC language

3. The expression of true and false

C language is not 0 is true 0 is false

OC language Yes is defined as 1 no defined as 0

4. The output conversion character is%d

5. Function: Used for branching structure and cycle judgment.

*/

BOOL A1 = YES;

BOOL A2 = NO;

printf ("a1=%d", A1);

printf ("a2=%d", A2);

//

BOOL a3 = YES;

BOOL A4 = NO;

printf ("a3=%d", A3);

printf ("a4=%d", A4);

BOOL A5 = YES;

BOOL a6 = NO;

printf ("a5=%d a6=%d", A5,A6);

#pragma mark---relational operators----

/*

Relational operators

1. For a comparison of two numbers

2. Relational operators result in two types of operations: TRUE, false. Use a Boolean type variable to receive the result

3. Relational expressions: Constants, variables, and relational operators;

*/

Greater than less than

BOOL a1 = 5 > 3;//result is True YES

printf ("a1=%d\n", A1);

BOOL A2 = < 30;//result is false NO

printf ("a2=%d\n", A2);

//

Define the age of two people, then compare and then define a bool type variable to receive the result;

int age1 = 30;

int age2 = 20;

BOOL age= age1 > age2;

printf ("age=%d\n", age);

Define three variables, compare three variables, and receive results when defining a bool type variable;

int a = 0;

int b = 0;

int c = 0;

printf ("Please enter three variables:");

scanf ("%d%d%d", &a,&b,&c);

BOOL d = a > B > C;

printf ("D =%d\n", d);

Greater than or equal to less than equals

BOOL r1 = 5 >= 5;//result is true

printf ("r1=%d\n", R1);

//

BOOL r2 = 6 <= 1;//result is False

printf ("r2=%d\n", r2);

//

equals = = Not equal to! =

BOOL R3 = 6 = = 5;//result is False

printf ("r3=%d\n", R3);

//

BOOL R4 = 4! = 4;//result is False

printf ("r4=%d\n", R4);

Note: = = (double equals to determine whether the values on both sides are equal), the left and right sides can be constants or variables;

= (an equal sign is a variable that assigns the right value to the left) and the left side must be a variable;

#pragma mark---logical operators---

/*

logical operators

1. Logical AND logical or logical non-

2. There are two kinds of logical operations, true, false. Use the BOOL type variable to receive

3. Logical Expressions: Constants, variables, and logical operators;

*/

/* Logic and &&

1. Logic and expression: expression 1 && expression 2

2. Operational rules:

(1) First execute expression 1, in the execution of expression 2;

(2) expression 1 and expression 2 The result is true at the same time, is true, a false is false;

(3) Short-circuit phenomenon: The result of expression 1 is false, it is not executed expression 2;

*/

Define two variables, then compare the two variables, and use the logical and. Variable with type bool to receive the result;

int a = 10;

int B = 20;

BOOL R1 = a > B && a++;

printf ("r1 =%d\n", R1);

BOOL R2 = a < b && b++;

printf ("r2 =%d\n", r2);

BOOL R3 = a > B && a < b;

printf ("R3 =%d\n", R3);

/*

Logical OR operator: | |

1. Logic or expression: expression 1 | | Expression 2

2. Operational rules:

(1). Execute expression 1 First, execute expression 2;

(2). The result of expression 1 and expression 2 is false at the same time. It's true.

3. Short-circuit phenomenon: expression 1 is true, it is not in the execution of expression 2;

*/

int a = 10;

int B = 20;

BOOL R1 = (A < b) | | (b = = 3);

printf ("r1 =%d\n", R1);

BOOL r2 = (A = = 3) | | (b > a);

printf ("r2 =%d\n", r2);

BOOL R3 = (A > b) | | (b > a);

printf ("R3 =%d\n", R3);

//

Short Circuit phenomenon

BOOL R4 = (A < 5) | | (a++);

printf ("R4 =%d\n", r4);

BOOL R5 = (b > 5) | | (b++);

printf ("R5 =%d\n", R5);

//

/*

Logical Non!

1. Non-logical: negation operation

2. Operational rules: Reverse the true and false

*/

BOOL a = YES;

BOOL B =!a;

printf ("A =%d\n", a);

printf ("B =%d\n", b);

BOOL C = NO;

BOOL d =!c;

printf ("C =%d", c);

printf ("D =%d", d);

/*

Precedence of Operators

Arithmetic operators > Relational operators > Logical operators > Assignment operators

*/

int a = 3, B = 4, c = 5;

BOOL R1 = a + B > c && b = = C;

printf ("%d\n", R1);

BOOL r2 = a | | (b + C && b-c);

printf ("%d\n", r2);

//

int a = 10;

int B = 20;

BOOL R1 = a > B && b++;

printf ("r1 =%d\n", R1);

C Language Foundation Lesson two-----Boolean types, relational operators, logical operators,

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.