Multiple arguments in the "if" command

This is a discussion on Multiple arguments in the "if" command within the C++ Programming forums, part of the General Programming Boards category; Can you make an "if" command like this? Code: if ( argument1, argument2){runthis}...

  1. #1
    Railgun God |Wiz|'s Avatar
    Join Date
    Sep 2005
    Posts
    23

    Multiple arguments in the "if" command

    Can you make an "if" command like this?

    Code:
    if ( argument1, argument2){runthis}

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  3. #3
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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}
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    whoever is right..the most important thing is that we could answered to his questions in one way or the other to help him.

  7. #7
    Railgun God |Wiz|'s Avatar
    Join Date
    Sep 2005
    Posts
    23
    Thanks, the && was what I needed.
    BUT before you follow your own advice, put the parentheses in

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

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by |Wiz|
    Thanks, the && was what I needed.
    BUT before you follow your own advice, put the parentheses in

    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..


    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

  9. #9
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    yeah compilers dn't get get confuse, they are much intelligent than u can think

    but for readability some people use the parantheses...

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    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){}

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    oops, typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 07:46 AM
  2. Variable Arguments through Command Line
    By zombiezparadize in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 08:35 AM
  3. Tokenizing Command Line Arguments
    By NickNore in forum C Programming
    Replies: 3
    Last Post: 09-24-2007, 01:18 AM
  4. need help with command line arguments
    By cyber_tech in forum C Programming
    Replies: 13
    Last Post: 08-06-2007, 08:45 AM
  5. Command Line Arguments in Unicode
    By JasonD in forum Windows Programming
    Replies: 4
    Last Post: 11-14-2003, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21