Logical Operators

Jun 1, 2009 at 3:24pm
Why do statements like:

if(varName=='y'||'Y')

not work so you're forced to use something like

if(varName=='y'||varName=='Y') ?
Jun 1, 2009 at 3:49pm
Because varName=='y'||'Y' could be varName == func1() || func2() which may be interpreted as
( varName==func1() ) || func2() which is usually what the programmer wants
Last edited on Jun 1, 2009 at 3:50pm
Jun 1, 2009 at 4:06pm
Im not sure, but i usually do this so i dont have to worry about case
sensenitve input
1
2
3
4
5
6
7
8
9
#include <cctype>

char varName;
std::cin>>varName;
varName = std::toupper(varName);
if(varName = 'Y')
{
//do something
}


Jun 1, 2009 at 4:11pm
1
2
3
4
if(varName = 'Y')
{
//do something
}

You mean you overwrite whatever the user inputted and "do something" regardless? :-)

To answer OP's question more thoroughly, 'Y' is an integer, and therefore an expression in itself. All (binary) operators take two expressions as operands and evaluate to another expression. mcleano's confusion arises from the meaning of the operand OR and the word "or". OR doesn't "evaluate to either of its operands", which is more or less the meaning of "or". OR evaluates to non-zero if either of its operands are non-zero.
If we were to change the operator precedence so that (a=='Y' || 'y')==(a==('Y' || 'y')), it still wouldn't work because OR doesn't return either of its operands as the situation demands. All OR does is the boolean operation OR, which follows the truth table:
For a || b == f
a b f
0 0 0
0 1 1
1 0 1
1 1 1

So, a==('Y' || 'y') would in reality be equivalent to a==!0.

Note that it's not impossible to make a language that follows the more intuitive (from the natural language point of view) rule, but it's just not practical and could potentially have ambiguous syntax.
Last edited on Jun 1, 2009 at 10:37pm
Jun 1, 2009 at 9:51pm
I think I'll re-read this again in the morning lol, thanks for the replies though!
Jun 2, 2009 at 4:38am
jloundy: line 6: you accidentally used the assignment operator...
Jun 2, 2009 at 7:25am
I'm not sure about the if statement, but if you try a switch statement:
1
2
3
4
5
 switch (varName) 
case 'Y':
// Code to execute if Y
default:
// Code to execute if not Y or any other case defined 

That is not case sensitive.
Jun 2, 2009 at 7:27am
closed account (4j8AC542)
Because logical operators apply to conditions, so they work something like "is the cat black or is the cat white" and not "is the cat black or white". This allows you to perform checks like "is the cat black or is the cat fluffy" which you wouldn't be able to do if the first way you described worked.

If you want to do something like "is the variable y or Y" and your variable is a std::string then you could do something like:

if (std::string::npos != varName.find_first_of("yY")) { ... }
Jun 2, 2009 at 7:51am
@chrisname
'y' and 'Y' have different values so even the switch is 'case sensitive' but with that you can do this:
1
2
3
4
5
6
switch (varName)
{
    case 'Y': 
    case 'y':
        //varName is equal to 'y' or 'Y'
}
Topic archived. No new replies allowed.