Asp.net logical operators and (& amp;), or (|), non (!), Asp.net logical operators

Source: Internet
Author: User

Asp.net logical operators and (&), or (|), non (!), Asp.net logical operators

Logical operators and (&), or (|), not (!) Returns a Boolean value based on the relationship between parameters.

Public class bool {

Public static void main (string [] args ){

Random rand = new random (47 );

Int I = rand. nextint (100 );

Int j = rand. nextint (100 );

System. out. println ("I =" + I );

System. out. println ("j =" + j );

System. out. println ("I> j is" + (I> j ));

System. out. println ("I <j is" + (I <j ));

System. out. println ("I> = j is" + (I> = j ));

System. out. println ("I <= j is" + (I <= j ));

System. out. println ("I = j is" + (I = j ));

System. out. println ("I! = J is "+ (I! = J ));

System. out. println ("(I <10) & (j <10) is" + (I <10) & (j <10 )));

System. out. println ("(I <10) | (j <10) is" + (I <10) | (j <10 )));

}

}

Output

I = 58

J = 55

I> j is true

I <j is false

I> = j is true

I <= j is false

I = j is false

I! = J is true

(I <10) & (j <10) is false

(I <10) | (j <10) isfalse

And or non-operations can only be applied to boolean values. If boolean is used where it should be a string value, the boolean value is automatically converted to an appropriate form.

It should be noted that the program comparison of floating point numbers is very strict.

Conditional Operators

Operator
Use
Description

&&
Op1 & op2
If op1 and op2 are both true, true is returned. If op1 is false, the right operand is not calculated.

|
Op1 | op2
If one of op1 and op2 is true, true is returned. If op1 is true, the right operand is not calculated.

!
! Op
If op is false, true is returned. If op is true, false is returned.

&
Op (www.111cn.net) 1 & op2
Operation op1 and op2; If op1 and op2 are both boolean values and both are true, true is returned; otherwise, false is returned. If op1 and op2 are both numbers, bit and Operation

|
Op1 | op2
Operation op1 and op2; If op1 and op2 are both boolean values and one is equal to true, true is returned; otherwise, false is returned. If op1 and op2 are both numbers, the bitwise OR operation is performed.

^
Op1 ^ op2
Operation op1 and op2; If op1 and op2 are different, that is, if one is true and the other is not, true is returned; otherwise, false is returned. If op1 and op2 are both numbers, the bitwise XOR operation is performed.

Short Circuit
When using logical operators, a short circuit is encountered. Once the value of the entire expression is clearly determined, the remaining part of the expression is no longer calculated. Therefore, the back part of the entire logical expression may not be computed. The following example shows the short circuit.

Public class shortcircuit {

Static boolean test1 (int val ){

System. out. println ("test1 (" + val + ")");

System. out. println ("result:" + (val <1 ));

Return val <1

}

Static boolean test2 (int val ){

System. out. println ("test1 (" + val + ")");

System. out. println ("result:" + (val <2 ));

Return val <2

}

Static boolean test3 (int val ){

System. out. println ("test1 (" + val + ")");

System. out. println ("result:" + (val <3 ));

Return val <3

}

Public static void main (string [] args ){

Boolean B = test1 (0) & test2 (2) & test3 (2 );

System. out. println ("expression is" + B );

}

}

Output

Test1 (0)

Result: true

Test (2)

Result: false

Expression is false

Because you call three methods, you will naturally feel that all three methods should be run, but the output is not actually like this, because a false result is generated in the second test. This means that the entire expression must be false, so there is no need to continue to calculate the remaining expressions, which is just a waste. This is the reason for a "Short Circuit. In fact, all logical expressions do not have to be computed, which improves performance.
From: http://www.111cn.net/net/net/37363.htm

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.