I am working with input validation and logical OR. I am trying to make an error message pop up after rejecting input where the first letter in the string is not 0, 1, 2, 3, or 4. However, an error message still pops up even if the first character is 0, 1, 2, 3, or 4. How can I get the program to work as I would like it to?
I see how Logical AND makes sense since the code block would only execute if it did not find any of the numbers. If my understanding is correct, the code block in my current code will always execute since str[0] can only be one value at a time.
Well, the OR gives a true result when either or both the operands are true. For example, if str[0] has the value '0', the first condition str[0] != '0' will be false. However the condition str[0] != '1' will be true (because '0' is not equal to '1' is a true statement). Thus at least one of the operands will always be true, no matter what the input, and so the OR will result in true.