if statement not working right?

前端 未结 5 1650
执念已碎
执念已碎 2020-12-02 03:19

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 != \'

5条回答
  •  执笔经年
    2020-12-02 03:45

    Take the conditional yesno != 'Y' || 'N' apart. It has two clauses:

    1. yesno != 'Y'. This is probably a comparison you intended; it checks to see if the char yesno is inequal to the value 'Y'.
    2. 'N'. This clause evaluates simply to the value 'N'. As a bool, 'N', which is not zero, is considered "true."

    So you have "(stuff) || (always true)". Here "stuff" is yesno != 'Y' and "always true" is 'N'. The result will always be true.

提交回复
热议问题