If I have an if statement separated by ||
returns true, will it continue the statement?
for example: if(true || random())
, will random()
be executed? because there is no reason for that.
If I have an if statement separated by ||
returns true, will it continue the statement?
for example: if(true || random())
, will random()
be executed? because there is no reason for that.
random()
will not be executed:
The conditional-OR operator (
||
) performs a logical-OR of itsbool
operands. If the first operand evaluates totrue
, the second operand isn't evaluated. If the first operand evaluates tofalse
, the second operator determines whether the OR expression as a whole evaluates totrue
orfalse
.
See || Operator (C# Reference) and this answer.
No, random()
will not be executed if you put ||
and your first condition is true.
However, with |
both conditions get checked no matter what the first result is.