Logical operator behavior for and in C # (% with%) is the same as the &&
operator you mentioned. Same for the or logical operator ( AndAlso
) and ||
.
The OrElse
and And
operators of Visual Basic are equivalent to the Or
and &
operators, respectively.
I think we can all agree that this is not at all obvious.
Following the bigown answer, I'll try to explain the difference here.
The operators |
/ AndAlso
and &&
/ OrElse
work only with boolean expressions and are short-circuited.
The ||
/ And
and &
/ Or
operators operate as bit filters and can be used with expressions of several different types - this is a bit more complex. But the important thing here is that they are not short-circuited.
Bit filtering occurs as follows: you align two values of the same type in binary and compare the bits in pairs, always comparing the nth bit of a value with the nth bit of another value. The result of the operation is a third binary value of the same size, filled in according to the following rules:
- If the operation is "and" , each result position will contain 1 only if both inputs have 1 in that position, and 0 otherwise;
- If the operation is "or" , each result position will contain 0 only if both inputs have 0 in that position, and 1 otherwise.
To verify, make a program that performs the following operations and displays the results in a console or message box:
1 & 3:
1: 0000 0000 0000 0000 0000 0000 0000 0001
3: 0000 0000 0000 0000 0000 0000 0000 0011
resultado: 0000 0000 0000 0000 0000 0000 0000 0001 (1)
1 | 3:
1: 0000 0000 0000 0000 0000 0000 0000 0001
3: 0000 0000 0000 0000 0000 0000 0000 0011
resultado: 0000 0000 0000 0000 0000 0000 0000 0011 (3)
3 & 5:
3: 0000 0000 0000 0000 0000 0000 0000 0011
5: 0000 0000 0000 0000 0000 0000 0000 0101
resultado: 0000 0000 0000 0000 0000 0000 0000 0001 (1)
3 | 5:
3: 0000 0000 0000 0000 0000 0000 0000 0011
5: 0000 0000 0000 0000 0000 0000 0000 0101
resultado: 0000 0000 0000 0000 0000 0000 0000 0111 (7)
Now the coolest: Booleans in .NET are represented by a byte. The two possible values are:
true: 0000 0001
false: 0000 0000
Then, |
results in true & false
, while false
results in true | false
(regardless of the order of variables).
Short circuit ... This is a characteristic of most languages derived from C. The evaluation of logical expressions has two stopping conditions. It stops immediately when it encounters a false value for operations of type "and" ( true
/ AndAlso
) or true for expressions of type "or" ( &&
/% with%). This feature is not used for operators OrElse
/ ||
and And
/ &
.
If you use several logical evaluations in a single expression, the program will assemble an expression tree - I leave it to your discretion to search how the programs do this. The important thing is that each operator becomes a node with two children, and each child is an expression. The program then resolves the tree by substituting each operator for the result of its evaluation with its child nodes. If the operators are short-circuited, the tree may have paths removed prematurely, and those paths will never be evaluated. But if the tree contains only non-short-circuiting operators, it will necessarily evaluate every path you have.