1

I've been using the following code block to ask user for input and checking its validity in a console application.

do
{
    Console.Write("Enter X value:");    // prompt
} while (!(int.TryParse(Console.ReadLine(),out temp) && (temp<=10) && (temp>=0))); // repeat if input couldn't be parsed as an integer or out of range

Is it a documented feature that the "&&" (and) expression evaluating is lazy? ie: if first operand is false then it wouldn't parse the second operand? Can I rely on it in production builds? Can I expect that it would behave the same in other compilers?
This is something I picked up in PPCG.SE

Additionally, can the block be made simpler to read or simplified into a oneliner?

workoverflow
  • 578
  • 5
  • 16
  • It's amazing what one can find, if one is simply willing to [read the documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator): _"The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary"_ – Peter Duniho Jan 06 '18 at 21:29

2 Answers2

1

Is it a documented feature that the "&&" (and) expression evaluating is lazy?

Yes as mentioned on the C# reference page:

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary. In other words, it's a short-circuiting operator.

Can I rely on it in production builds? Can I expect that it would behave the same in other compilers?

Yes, it should always behave the same.

Additionally, can the block be made simpler to read or simplified into a oneliner?

Apart from removing some of the redundant parameters, you cannot simplify this with only one line.

You can, however, make it a bit more readable by hiding the logic of (temp<=10) && (temp>=0) into some method e.g.:

public static bool IsValidRange(int temp) =>  temp >= 0 && temp <= 10;

then your while condition becomes:

while (!(int.TryParse(Console.ReadLine(), out temp) && IsValidRange(temp)));

now the name of the method reads as the problem statement IsValidRange.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • As an added note, `&&` and `||` are generally referred to as "short-circuit operators" because of this behavior. Most major programming languages treat them this way. – Powerlord Jan 06 '18 at 10:04
0

Here is the relevant section in the C# Language Specification that talks about && and ||:

Section 7.12 Conditional Logical Operators

The && and || operators are conditional versions of the & and | operators:

  • The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is not false.
  • The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.

Since it's in the spec, it should behave the same no matter what compiler you use. If it doesn't, then the compiler you are using is not considered a "C#" compiler. :)

Sweeper
  • 213,210
  • 22
  • 193
  • 313