Can you make an "if" command like this?
Code:if ( argument1, argument2){runthis}
Can you make an "if" command like this?
Code:if ( argument1, argument2){runthis}
Sure. Look up the logical operators:
Code:if (a && b) //read: if a AND b //... if (a || b) //read: if a OR b //...
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
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:
this is wrong!Code:if(x<1,y>x)
but we can combine 2arguments by using logical operators as shown by the example of JaWiB.
BUT with loops u can have something like:
Hope, i cud help a bitCode:for(x=1,y=2;x<5,y>0;x++,y--)
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.
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..Originally Posted by wakish
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}
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
whoever is right..the most important thing is that we could answered to his questions in one way or the other to help him.
Thanks, the && was what I needed.
BUT before you follow your own advice, put the parentheses in
or the && will most likely confuse the compilerCode:if ((argument1) && (argument2)) {run this}
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..Originally Posted by |Wiz|
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
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
yeah compilers dn't get get confuse, they are much intelligent than u can think
but for readability some people use the parantheses...
|| means "or" and && means "and", but can I use them like this:
Code:if(a=1 && b=7 || a=2 && b=77 || a=87){}
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)