# Multiple arguments in the "if" command

• 09-27-2005
|Wiz|
Multiple arguments in the "if" command
Can you make an "if" command like this?

Code:

`if ( argument1, argument2){runthis}`
• 09-27-2005
JaWiB
Sure. Look up the logical operators:
Code:

```if (a && b) //read: if a AND b //... if (a || b) //read: if a OR b //...```
• 09-27-2005
wakish
i think |Wiz| asked if we cud use 2arguments separated by a comma, just look at his example given!

well,from my point of view:
Code:

`if(x<1,y>x)`
this is wrong!

but we can combine 2arguments by using logical operators as shown by the example of JaWiB.

BUT with loops u can have something like:
Code:

`for(x=1,y=2;x<5,y>0;x++,y--)`
Hope, i cud help a bit ;)
• 09-27-2005
Daved
You can use the comma operator in the if statement, although it rarely makes sense. It will evaluate each expression and the result of the whole thing will be the result of the last expression. So if (a, b) will evaluate a and b, and will be true if b evaluates to true and false if b evaluates to false. I think JaWiB's version is what you are looking for.
• 09-27-2005
Dae
Quote:

Originally Posted by wakish
i think |Wiz| asked if we cud use 2arguments separated by a comma, just look at his example given!

That was just his example to show what he meant (probably how its written in different language, or he guessed it). He meant he would like to have an if statement that would only run if two different arguments returned true. In which case, JaWiB's reply is right..

Use the && operator, not a comma, to have an if statement that only runs when both are true. You could also use the || operator that means it will run if either are true.

Code:

`if ( argument1  && argument2) {runthis}`
• 09-27-2005
wakish
whoever is right..the most important thing is that we could answered to his questions in one way or the other to help him.
• 09-27-2005
|Wiz|
Thanks, the && was what I needed.

Code:

`if ((argument1) && (argument2)) {run this}`
or the && will most likely confuse the compiler
• 09-27-2005
Dae
Quote:

Originally Posted by |Wiz|
Thanks, the && was what I needed.

Code:

`if ((argument1) && (argument2)) {run this}`
or the && will most likely confuse the compiler

This will not confuse the compiler: something != something 2 && something == something3. Generally most wont confuse your compiler, but the person reading it. Some might, you're right, order of operations..

Quote:

Operators Associativity
( [ - . Left to right
! - ++ -{- + * & (type-cast) sizeof Right to left
(in the above line, +, - and * are the unary forms)
* / % Left to right
+ - Left to right
<< >> Left to right
< <= > >= Left to right
== != Left to right
& Left to right
^ Left to right
| Left to right
&& Left to right
|| Left to right
?: Left to right
= += -= *= /= %= &= ^= |= <<= >>= Right to left
, Left to right
• 09-28-2005
wakish
yeah compilers dn't get get confuse, they are much intelligent than u can think ;)

but for readability some people use the parantheses...
• 09-28-2005
maxorator
If statements
|| means "or" and && means "and", but can I use them like this:
Code:

`if(a=1 && b=7 || a=2 && b=77 || a=87){}`
• 09-28-2005
laserlight
Well, yes, except that now it appears that you're confusing assignment ("=") with comparing for equality ("==").

If I remember correctly || has a lower precedence than &&, so your expression (once corrected) should work as expected, but grouping using parentheses would help prevent misinterpretation.
• 09-28-2005
maxorator
oops, typo