Using || inside comparison

Hi, so I tried this first

return (Response[0] == ‘y’ || ‘Y’);
but it wouldn’t work, incorrect answers were being returned as true.

return (Response[0] == ‘y’) || (Response[0] == ‘Y’);
this seems to work fine though, can anyone explain why my first option wouldn’t work?

1 Like

This is because each statement is independant of each other, you could write

return (Response[0] == 'y' || 'Y');


return ('Y' || Response[0] == 'y');

and get the same results. ‘Y’ is going to be converted to a boolean value, and all non-zero values valuate as true so that entire statement will always be true.

Privacy & Terms