# Multi-conditional if statements.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-23-2005
SlyMaelstrom
Multi-conditional if statements.
Is there a way to do them?

For example:

Code:

```if (x=1 or y=2 or z=3) { do something } else { return=0; }```
• 03-23-2005
Shakti
Code:

```if (x==1 || y==2 ||  z==3) { do something } else { return 0; }```
You use || as or, && is and.
• 03-23-2005
major_small
also, know that && (AND) is figured before || (OR)

for example: if(true && false || true) evaluates to true

there are also other operators, like the ! (NOT) operator. it simply negates the value. for example, if(!true) evaluates to false.

you can also use parenthesis. I assume you know how parenthesis mess with the order of operations in arithmetic, so all I'm going to say is it's essentially the same for logic.
• 03-23-2005
misplaced
be careful though

c++ uses short circuit evaluation

take for example
Code:

```bool thisIsTrue = true; if(thisIsTrue || ImportantFunction()) {     ......... }```

when it sees the || it knows that either the left operand (thisIsTrue) or the right operand (the return value of ImportantFunction()), must be true in order to execute the code within the if{} block. since "thisIsTrue" is in fact true, there is no need to evaluate the rest of the expression and does not call "ImportantFunction()".

don't get cute with that piece of information either and do
Code:

```//bad if(x || func())```
in place of
Code:

```//good if(!x)   func();```
• 03-23-2005
SlyMaelstrom
Thanks for this info guys.

I saw info exactly like this on the internet, except it was refering to PHP. The statements looked exactly like C++, but I wanted to ask just to be sure.
• 03-23-2005
SlyMaelstrom
Also... since I'm thinking about things I've seen in other languages.

In strings, can you make an if statement that looks to see if the user's input is contained in your criteria but isn't exactly your criteria?

For instance, if my statement is looking for the user to say "North" but they choose to say "Nor" or "N" it would accept that so long as it doesn't interfere with other criteria.
• 03-23-2005
skiingwiz
Quote:

Originally Posted by major_small
also, know that && (AND) is figured before || (OR)

for example: if(true && false || true) evaluates to true

That example evaluates to true regardless of which operator has precedence.

( (true && false) || true ) == true
( true && (false || true) ) == true
• 03-23-2005
Ezzetabi
If you use -Wall (as you really should) when compiling the compiler will warn you if you forgot the second = in the confrontation.
Also, if you are afraid to make mistakes, just use the constant in front:
if (0 == p)
so, if you forgot a equal the compiler will tell that the assignement is not valid.
• 03-23-2005
Salem
> if (0 == p)
Oddly, it seems easier to remember to use == than remember to swap the variables round.

Not only is p == 0 the natural way of reading it, because you've remembered that it should be == and not =, you DON'T have a problem when you try and do something like
if ( p == q )

Rearranging it to
if ( q == p )
doesn't help you because both can be assigned and your syntactic crutch to stop you falling over no longer works as it should.
• 03-23-2005
Hunter2
Quote:

In strings, can you make an if statement that looks to see if the user's input is contained in your criteria but isn't exactly your criteria?
Assuming you're talking about std::string and not char[]:
Code:

```if(str.find("No") != std::string::npos)   //it's contained.```
• 03-23-2005
Thantos
Quote:

also, know that && (AND) is figured before || (OR)
NOT true. The order of evaluation is always left to right in a boolean statement. && binds the results tighter then ||.

So if you have:
Code:

`if ( x==5 || y==2 && z==7)`
the x==5 is evaluated before the rest of it.
• 03-24-2005
major_small
Quote:

Originally Posted by Thantos
NOT true. The order of evaluation is always left to right in a boolean statement. && binds the results tighter then ||.

So if you have:
Code:

`if ( x==5 || y==2 && z==7)`
the x==5 is evaluated before the rest of it.

I think you misunderstood what I meant. I meant that && has a higher precedence than ||. bad wording on my part. and a bad example as well. (thanks to skiingwiz for pointing that out). with that in mind, here's a better example:

if(false && true || true) evalutates to true
if((false && true) || true) evaluates to true
if(false && (true || false)) evaluates to false

C operator precedence:
http://msdn.microsoft.com/library/de...m/expre_13.asp
• 03-24-2005
Hunter2
Still a bad example ;) This also demonstrates the left-to-right evaluation order (evaluates the && first, because it is the leftmost operator).

**EDIT**
Hmm, I can't seem to come up with an example that will make a distinction between the two. Could it be that the two will always just give you the same result? In which case, the only real way to find out the order of evaluation would be to create bool functions that give output when they are called.
• 03-24-2005
major_small
Quote:

Originally Posted by Hunter2
Still a bad example ;) This also demonstrates the left-to-right evaluation order (evaluates the && first, because it is the leftmost operator).

fine, how's this:

if(!(!A && B || A && !B))

jeebus... and 10 points to anybody whow knows what that gate's called.

edit: and another 20 points to the person who brings me the head of the person that gives me bad rep for trying to be helpful.
• 03-24-2005
```int *p; /* code */ if(NULL != p && 1 == *p) { }```