Why is this a good question?

I was taking in one of Rob Miles' video blogs and one of the questions on his quiz he said he was particularly proud of and spent some time explaining it. The question was as follows:

The C# code below is a part of a validation in a program that is supposed to reject age values which is less than 3 or greater than 100.

 if (age < 3 && age > 100) Console.WriteLine("Age invalid"); 

The code has been tested with the ages of 1, 50 and 100. State which input values would cause the program to fail and identify the error on line 1.

I perhaps fretted over this a minute longer than I should have, looking for the trick only to realize there wasn't one. The && is the AND operator and a number (or really anything) cannot be two things or values at once. So quite clearly the message "Age invalid" will never print. Clearly what was intended was the OR operator: || but he never states that in his commentary.

The video can be found here: http://ift.tt/1GzGct9 and the question/answer starts at 4:00 regarding 1(iii).

by Zamdrist via /r/csharp

Leave a Reply