CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2009
    Posts
    24

    Is this possible?

    [code\]
    int a=5;
    if (a==7||6||5||4||3||2)

    instead of doing

    int a=5;
    if (a==7||a==6||a==5||a==4||a==3||a==2)
    [code/]
    is this possible and if not how can you make it possible?

  2. #2
    Join Date
    May 2007
    Posts
    13

    Re: Is this possible?

    Code:
    int a=5;
    if (a==7||6||5||4||3||2)
    is not going to do what you want it to (the condition will always be true). Instead do

    Code:
    if( a >= 2 && a <=7 )
       // Do something
    or use a switch statement

    Code:
    switch( i )
        {
        case 7:
        case 6:
        case 5:
        case 4:
        case 3:
        case 2:
          //Do something
          break;
        default:
          break;
       };

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Is this possible?

    Quote Originally Posted by E-man96 View Post
    [code\]
    int a=5;
    if (a==7||6||5||4||3||2)

    instead of doing

    int a=5;
    if (a==7||a==6||a==5||a==4||a==3||a==2)
    [code/]
    is this possible and if not how can you make it possible?
    No it's not.

    What do you have against white space?

  4. #4
    Join Date
    Oct 2002
    Location
    UK
    Posts
    200

    Re: Is this possible?

    You may overload operator "||". But it is not recommanded.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is this possible?

    Quote Originally Posted by E-man96 View Post
    [code\]
    int a=5;
    if (a==7||6||5||4||3||2)

    instead of doing

    int a=5;
    if (a==7||a==6||a==5||a==4||a==3||a==2)
    [code/]
    is this possible
    No it's not possible. C++ does not understand colloquial English. Just because English speakers shorten their "or's" by combining everything into one sentence doesn't mean it works for C++.

    Every comparison must be explicitly stated, one at a time.
    and if not how can you make it possible?
    Didn't you do that already?
    Code:
    int a=5;
    if (a==7||a==6||a==5||a==4||a==3||a==2)
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 13th, 2009 at 12:24 PM.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Is this possible?

    Quote Originally Posted by Paul McKenzie View Post
    C++ does not understand colloquial English. Just because English speakers shorten their "or's" by combing everything into one sentence doesn't mean it works for C++.
    There are languages that do work that way though. C++ Just doesn't happen to be one of them.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is this possible?

    Quote Originally Posted by GCDEF View Post
    There are languages that do work that way though. C++ Just doesn't happen to be one of them.
    Maybe, but most high-level (and low-level) languages work only by specifying in "long hand" all of the conditions.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured