1

If I have IF condition like:

if(x == 0 || y == 4)
{
// ...
}

then if x == 0 is true does c# checks if y == 4 while we already know the whole expression is true?

extra2
  • 43
  • 1
  • 6
  • 3
    This can be answered by the [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator). (_"If the first operand evaluates to true, the second operand isn't evaluated"_) – Tim Schmelter Sep 05 '18 at 07:48
  • Would it make a change if c# does it or not since you would go into the statement anyways? – Cataklysim Sep 05 '18 at 07:49
  • 2
    @cataklysim yes, the code on the right could easily contain side effects. It could be a function call – Buh Buh Sep 05 '18 at 07:50
  • Or you can check it by yourself, put `return 4` inside some method, compare `y` with that method, add breakpoint inside method and check if that hits. – SᴇM Sep 05 '18 at 07:51
  • @Cataklysim in my case I had to know – extra2 Sep 05 '18 at 07:52
  • @SeM yee that's also a good idea thanks – extra2 Sep 05 '18 at 07:52

2 Answers2

1

The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator

fubo
  • 44,811
  • 17
  • 103
  • 137
-1

Look at MSDN. So the answer is NO, it doesn't check

The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false.

Antoine V
  • 6,998
  • 2
  • 11
  • 34