Lecture 24 question

Does it matter if it is written (Response == ‘y’) || (Response == ‘Y’) vs. (Response == ‘y’ || ‘Y’) ? I thought it had to be written the first way but I checked the second way and it worked too. If left the second way will it mess up the code in the future. Thank you.

== and || are so-called operators

Every programming language has an order of precedence/ order of execution for operators,
just like in math, where you calculate * before +.

== gets evaluated before ||, therefore your

first returns true for

Response == 'y'

and then checks

true || 'Y'

which again returns true.

If the first statement is false, it’ll try to evaluate

false || 'Y'

and i have no idea what the compiler is taking ‘Y’ for… maybe true because it is not NULL.
It may be a case of unpredicted behaviour .

1 Like

Thanks for the reply. Yea the second shorter way looks a little wonky for the compiler which is why I was surprised it worked but then was thinking if MS had upgrade VS to read; If this EQUALS this OR this then true since we would read it that way but either way I left it the longer way that we all know it should be just in case it comes back and bites me.

I am using VS 2017 and the above statement ((Response == ‘y’ || ‘Y’)) does not resolve predictably.
Perhaps you should try running it again a few times, giving various input, and see what comes out.

Privacy & Terms