The following will work in C / C ++, because it does not have support for boolean classes in the first class, it processes each expression using the "on" bit on them as true, otherwise false. Actually the following code will not work in C # or Java if x and y are of numeric types.
if (x | y)
Thus, the explicit version of the code above:
if ( (x | y) != 0)
In C, any expression that has the โonโ bit on them leads to true
int i = 8;
if (i) // valid in C, leads to true
int joy = -10;
if (joy) // vaild in C, leads to true
Now back to C #
If x and y are of numeric type, your code is: if (x | y) will not work. Did you try to compile it? This will not work
But for your code, which I could read x and y, it has boolean types, so it will work, so the difference between | and || for boolean types || squirrel cage, | is not. The conclusion is as follows:
static void Main() { if (x | y) Console.WriteLine("Get"); Console.WriteLine("Yes"); if (x || y) Console.WriteLine("Back"); Console.ReadLine(); } static bool x { get { Console.Write("Hey"); return true; } } static bool y { get { Console.Write("Jude"); return false; } }
is an:
HeyJudeGet Yes HeyBack
Jude will not be printed twice, || is a Boolean operator, many C-derivative logical operators are short-circuited , Boolean expressions are more efficient if they are shorted.
As for layman terms, when you say a short circuit, for example in || (or operator), if the first expression is already true, there is no need to evaluate the second expression. Example: if (answer == 'y' || answer == 'Y'), if the user presses small y, the program does not need to evaluate the second expression (answer == 'Y'). This is a short circuit.
In my code example above, X is true, so Y on || the operator will not be evaluated further, therefore there is no second exit "Jude".
Do not use this code in C #, even if X and Y are of boolean types: if (x | y) . Does not work.