How many conditions can be used inside an if statement?

edited March 2017 in Programming Questions

Hello!

I'm currently developing an interactive data visualization and I'm attempting to use an if statement with 8 conditions. After I added the last condition, the program didn't carry it out so I decided to println the variable that defined the condition. Upon adding this prinln() to the code, the program started crashing processing every time I run it. Do if statements have a limit on the amount of conditions they can carry out? Can I use nested if statements to solve this or will that not work either?

Thanks!!

Tagged:

Answers

  • println can cause crashes

    you can use multiple ifs though

  • edited March 2017

    @Nasif --

    The problem is more likely that some aspect of how a specific conditional term is being evaluated needs to be debugged. Consider this 14-condition if statement sketch, which works fine:

    if (false || false || false || false || false || false || false || false || false || false || false || false || false || true){
      background(0,255,0);
    } else {
      background(255,0,0);
    }
    

    See related discussion of multiple conditions in Java and short circuiting:

  • Anything that evaluates to only true or false can be used.

  • Breaking the if statement into multiples is possible and may help.

    Might also be clearer to assign the individual tests to boolean local variables and use those in the if()

    Post the condition

  • edited March 2017 Answer ✓

    yeah post your code. Maybe a typo

    Use a function

    I often just program a function with return type boolean to take care of it. This function must have a good name such as boolean allAsteroidsAreDestroyed().

    Then a complicate if just becomes

    if(allAsteroidsAreDestroyed()) {
    
    }
    

    In this function you are more free to work with multiple lines and tests:

    boolean allAsteroidsAreDestroyed() {
    
        if(....) return false;  // leaving function
        if(....) return false;  // leaving function
    
        if(....||.....) return true;  // leaving function
        if(....) return false;  // leaving function
    
        return true;  // leaving function
    
    }
    

    Work with a buffer

    To accumulate tests you can use a buffer. This allows you to split ifs up:

    boolean bufferIllegal = true; // default
    
    if(.....) bufferIllegal = false;
    if(.....) bufferIllegal = false;
    
    // bufferIllegal now knows the result
    

    Another approach

    I was once thinking about an idea but only theoretically.

    Let's say you have a situation where you have many if-clauses and are confused. But you know that you have let's say 6 possible results of your tests like in a game of chess e.g.: moveWasIllegal, moveWasCastling, moveWasGivingCheck, moveWasGivingCheckMate, moveWasEnPassant, moveWasStandard ....

    You could approach this problem from the results and enumerate the possible results and for loop over them and then test each case separately:

    int testIncomingChessMove() {
    
        final int moveWasIllegal = 0;
        final int moveWasCastling = 1; 
        final int moveWasGivingCheck = 2;
        final int moveWasGivingCheckMate = 3;
        final int moveWasEnPassant = 4;
        final int moveWasStandard   = 5; 
    
        for (resultTester=moveWasIllegal; resultTester<=moveWasStandard;resultTester++) {    
    
            switch(resultTester) {
    
                case moveWasIllegal:
                    // test of illegal move
                    boolean bufferIllegal
                    //.......
                    //.......
                    //in case it was illegal 
                    return moveWasIllegal; // leaving function
                    break; 
    
                case moveWasCastling :
                    // test of Castling move
                    boolean bufferCastling
                    //.......
                    //.......
                    //in case it was Castling
                    return moveWasCastling ; // leaving function
                    break; 
    
                 // case..................... 
    
            }//switch
        }//for
    
        return moveWasStandard;
    
    }//func
    

    (The final ints must probably have global scope and are not only visible in the function.)

    Chrisir ;-)

  • (@Chrisir It is "Check" and "Checkmate")

  • (Thanks. I corrected this)

  • Thank you all very much! Upon troubleshooting it turns out the println() was the one giving me problems, once I moved it elsewhere it all worked again. Thanks for all the possibilities on using conditionals I really did learn a lot :)

Sign In or Register to comment.