Thread: Number of Condition inside if statement

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    28

    Number of Condition inside if statement

    I have been assigned to review the below code and update for the changes.
    Code:
       if(condition1>val1 && condition2<=val2 && cond3>val3 &&  cond4<val4 && cond5==val5 && cond6==val6 && cond7==val7 && cond8<val8  && cond9==val9 && cond10==val10 && cond11==val11  && cond12==val12)
    {
    }
    What is the best method to update the above code, i see lot of conditional checks, i am sure it is not the best practice. It is not only one place.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,416
    Generally, when you start numbering variables like that, it becomes worthwhile to ask if arrays should be used instead. If arrays are used, then a loop becomes a natural option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,047
    ISO 9899 5.2.4.1 1 suggests some limits, but not for the number of expressions inside a conditional block of an if. I think 127 (as in "127 parameters in one function call") is a good limit.

    BTW: I've seen this limit imposed on MSVC. This expression, separated by &&, can be interpreted as nested ifs, like:
    Code:
    if ( condition1 > val1 )
      if ( condition2 <= val2 )
        if ( ... )
          ...
    And more than 127 nested ifs (on MSVC) will cause a compilation error.

    Try to simplify the expression or to shrink the number of comparisons... There are multiple ways to do it and to choose one depends on what you are tring to do. For example: You could notice that the first 4 tests are RANGE tests, and the last 6 are equality tests, so:

    Code:
    _Bool found = 0;
    
    // range tests.
    if ( cond1 > val1 && cond2 <= val2 && cond3 > val3 && cond4 <= val4 )
    {
      // ISO 9899 suggests a limit of 4095 items in that block!
      struct {
        int cond, val;
      } tests[] = { { cond5, val5 }, { cond6, val6 }, { cond7, val7 }, { cond8, val8 },
                    { cond9, val9 }, { cond10, val10 }, { cond11, val11 } };
    
      found = 1;
    
      // "Non" equality tests.
      for ( int i = 0; found && i < sizeof tests / sizeof tests[0]; i++ )
        if ( tests[i].cond != tests[i].val )
          found = 0;
    }
    
    if ( found )
    { ... }
    This approach has the problem that a copy of the pairs (cond,val) are made to the stack, but avoid the multiple nesting levels.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    You say it appears in more than one place. Show a couple of the others.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,434
    Some formatting would help, so you're not scrolling sideways.
    Code:
    if(condition1>val1  && condition2<=val2 && cond3>val3 &&  
       cond4<val4       && cond5==val5      && cond6==val6 && 
       cond7==val7      && cond8<val8       && cond9==val9 && 
       cond10==val10    && cond11==val11    && cond12==val12)
    {
    }
    It might help you figure out what the underlying pattern(s) are.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-15-2017, 10:31 AM
  2. If statement wont check the second condition
    By BIGDENIRO in forum C Programming
    Replies: 9
    Last Post: 09-25-2013, 05:41 AM
  3. using two GetAsyncKeyState a while statement with an or condition
    By Rob Fisher in forum Windows Programming
    Replies: 3
    Last Post: 01-11-2013, 04:59 AM
  4. Can I call a function inside condition of if statement?
    By Meerul264 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2012, 11:06 PM
  5. if-else statement condition
    By YouMe in forum C Programming
    Replies: 16
    Last Post: 07-04-2012, 09:48 AM

Tags for this Thread