When do we skip further conditions, when we have multiple conditions in a while loop?

207 views Asked by At

For example, when we have three conditions in a while loop:

while (true || false || false)
{
  //Do something...
}

I am talking of conditions separated by the || operator only. I believe when we encounter this while loop, as soon as the first true statement is encountered, we skip the other conditions. I know this is true if we have two conditions in the while loop separated by the || operator. But, if we have more than two conditions, do we skip the further conditions, further down the while loop - when the first true condition is encountered? This is because, the while loop does not know, how many more conditions we have in the while loop. And would go further. For two conditions, we would skip evaluating the conditions further, when the first true condition is encountered. But, when there are multiple conditions, separated by the || operator, would we still skip evaluating the other conditions, when the first true condition is encountered? It would be helpful if you can tell me, what happens at Compile time and Run time, in this case (when we have multiple conditions in the while loop, separated by the || operator). How do we know when to skip the further conditions? Thank you for all your help!

2

There are 2 answers

2
StriplingWarrior On BEST ANSWER

It would be helpful if you can tell me, what happens at Compile time and Run time, in this case (when we have multiple conditions in the while loop, separated by the || operator).

If you literally have while(true || false || false), or equivalent compile-time constants that the compiler can deduce at compile time, then this will compile to the same code as while(true), because true || false || false can be calculated at compile-time to be true. The compiler would also omit any code following the while loop, unless it could determine you're using a break; or some other construct that would allow the while loop to be exited.

Likewise, if you had something like while(true && false), the entire while loop would be omitted from the compile-time code, because it would be deemed unreachable.

If you are using expressions with variables whose values cannot be determined at compile-time, such as while(node == null || node.Value != value), then at run-time the first expression (node == null) will be evaluated, and if it's found to be true then the remainder of the expression will be short-circuited as specified by the docs that Guru Stron noted in his answer. (In the example I just gave, that would prevent node.Value from throwing a null reference exception.)

2
Guru Stron On

Assuming that actual conditions are not constants (otherwise it does not make much sense, because they will be optimized by compiler to just true) - this is called "short-circuiting" and is explained in the docs for && and || operators:

Conditional logical AND operator &&

The conditional logical AND operator &&, also known as the "short-circuiting" logical AND operator, computes the logical AND of its operands. The result of x && y is true if both x and y evaluate to true. Otherwise, the result is false. If x evaluates to false, y isn't evaluated.

Conditional logical OR operator ||

The conditional logical OR operator ||, also known as the "short-circuiting" logical OR operator, computes the logical OR of its operands. The result of x || y is true if either x or y evaluates to true. Otherwise, the result is false. If x evaluates to true, y isn't evaluated.

You can try using | operator:

Logical OR operator |

The | operator computes the logical OR of its operands. The result of x | y is true if either x or y evaluates to true. Otherwise, the result is false. The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand.

bool Get(int i)
{
    Console.WriteLine(i);
    return i == 1;
}

// prints 1 2 InWhile
while (Get(1) | Get(2))
{
    Console.WriteLine("InWhile");
    break;
}

Demo @sharplab.io