We all know these four logical operators, but sometimes they will be messy. Here we will use the program to explain them so as not to forget them.
1. Logic and
& And & are translated into Chinese, which means "and". They are executed when both conditions are set at the same time. In this case, why are the two conditions different from each other, directly run the Code:
Public void fun () {int C = 0; int D = 0; If (C = 10) <D & (D = 10) <= C) // assign a value to the variable in the Condition Statement to check whether the condition is executed. Do not confuse it. In fact, assign a value first and then judge {console. writeline ("& C = {0}, D = {1}", c, d);} console. writeline ("& C = {0}, D = {1}", c, d); console. writeline ("-------------------------------"); int e = 0; int f = 0; If (E = 10) <F & (F = 10) <= e) {console. writeline ("& E = {0}, F = {1}", e, f);} console. writeline ("& E = {0}, F = {1}", e, f );}
What is the result? See the figure below:
The conclusion is: "&" when the condition from left to right is false, the IF statement is directly jumped out and no further judgment is made. Therefore, the d in the program will not be assigned a value; "&" indicates that all judgment conditions are executed no matter whether the condition from left to right is true or not. Therefore, F in the program is assigned 10 values.
2. Logic or
| And | translated into Chinese means "or", both of which are executed when at least one of the two conditions is established. Let's take a look at the differences between them and go directly to the Code:
private void fun(){ int x = 0; int y = 0; if ((x = 10) > y || (y = 10) < x) { Console.WriteLine("|| x={0},y={1}", x, y); } Console.WriteLine("---------------------------------"); int a = 0; int b = 0; if ((a = 10) > b | (b = 10) < a) { Console.WriteLine("| a={0},b={1}", a, b); } }
View the result chart directly:
The conclusion is: "|" when a condition is set, the IF content is directly executed without executing the judgment condition. Therefore, Y in the program is not assigned a value of 10; "|" executes all judgment statements from left to right, regardless of whether the conditions are true.
The complete code of this instance is attached to create a console application, copy and paste it, and use it directly:
static void Main(string[] args){ int x = 0; int y = 0; if ((x = 10) > y || (y = 10) < x) { Console.WriteLine("|| x={0},y={1}", x, y); } Console.WriteLine("---------------------------------"); int a = 0; int b = 0; if ((a = 10) > b | (b = 10) < a) { Console.WriteLine("| a={0},b={1}", a, b); } Console.WriteLine("---------------------------------"); int c = 0; int d = 0; if ((c = 10) < d && (d = 10) <= c) { Console.WriteLine("&& c={0},d={1}", c, d); } Console.WriteLine("&& c={0},d={1}", c, d); Console.WriteLine("---------------------------------"); int e = 0; int f = 0; if ((e = 10) < f & (f = 10) <= e) { Console.WriteLine("& e={0},f={1}", e, f); } Console.WriteLine("& e={0},f={1}", e, f); Console.ReadKey(); }
Summary: In one sentence, when two operators (&, |) are used, when the first condition is true (|) or violates (&), the condition after the condition is no longer judged, therefore, it is more efficient. When it is an operator (&, |), the remaining judgment statements will be executed no matter whether the first condition is true (|) or illegal, therefore, the efficiency is lower.
Logic and (&, &) and logic or (|, |)