14 C # logical operations in programming,

Source: Internet
Author: User

14 C # logical operations in programming,

 

In C # programming, we often need to deal with these situations.

1. When a condition is true, the program will process it like this; When a condition is false, the program will process it like this.

2. When a certain condition is true, the program will always process it like this;

The conditions here are logical operations in C. Next, let's talk about some basic concepts about logical operations in C.

 

Comparison Operators
  1. Comparison operator. The Gu name operator is used for comparison. Comparison operators include >,<,>=, <=, == ,! = Six in total. Note that> = is an operator. Similarly, <= is also an operator.
  2. A comparison operator returns a logical value, which is a Boolean value. When a logical value is reached, I call it a Boolean value, that is, true or false.

    For example

    Bool x = (8> 5 );

    The value of x is true, and the comparison operator is relatively simple.

    > Greater

    <Less

    > = Meaning greater than or equal

    <= Less than or equal

    = This is the same meaning of the two. Do not write an equal sign. An equal sign is the meaning of a value assignment.

    ! = This is not equal;

 

Logical operators
  1. There are three logical operators &, | ,!

    Logical operators are used to operate two logical values, that is, two boolean values.

    Bool x = true;

    Bool y = true;

    X & y indicates that the returned result is true only when both x and y are true; otherwise, the returned result is false;

    X | y indicates x and y. If either of them is true, the returned result is true. If both are false, x | y indicates false;

    ! The operator indicates the inverse operation. It is not an operator that operates two logical values, but a method that operates an operator.

    For example! X, when x is true ,! The value of x is false. When the value of x is false ,! The value of x is true;

 

Here are some examples of actual logical operations. Through the example, you can better understand the logic operations of C.

You create a project. The solution name is Sloution14, the project name is Exercise14, and the project type is console program. Click Program. cs. Add the code to it.

 

Running result

 

Exercise

1. Print the result of x + y. Check whether it is the same as z.

2. Write a logical expression containing two & operator numbers and print the result.

3. x> is the expression y true? Is it true or false? Try to modify the code and run it. After running, check the reason on the Internet? If you cannot find the answer, contact me.

 

 

 

 

 

 

 

 

 

 

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.