Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've looked and looked with the debugger and cannot seem to figure out why the IF statement always prints the message.

The IF statement checks to see if yesno != 'Y' ||(or) 'N' but regardless if i type y or Y or n N or H B it will show... . Im not sure what move to make anymore! I cant seem to find where its going wrong?

if(yesno != 'Y' || 'N') { ...

Thanks guys.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
378 views
Welcome To Ask or Share your Answers For Others

1 Answer

The || doesn't quite mean what you think it means. The correct approach is:

if (yesno != 'Y' && yesno != 'N') { ...

This evaluates each side of the && independently, and the result is true if both sides are true.

Note that

if (yesno != 'Y' || yesno != 'N') { ...

will always be true because any given character is either not Y or it is not N. This is probably not what you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...