Search code examples
javanestedternary

How to convert this example of nested ternary if-else to if-else loop


How to convert this example of nested ternary constructor to if-else loop. I am an begginer and is complicated form me.

    int bill = 2000;
    int qty = 10;
    int days = 10;
    int discount = (bill > 1000)? (qty > 11)? 10 : days > 9? 20 : 30 : 5;
    System.out.println(discount);

Solution

  • TL;DR: See last code block for answer to question.


    First, an if-else statement is not a "loop".
    for, while, and do-while are loops.
    if is a conditional statement.

    The operator precedence of the ? : ternary conditional operator is so low you rarely need to use parentheses for the 3 parts, unless you have embedded assignment operators. E.g. (bill > 1000) ? 30 : 5 is unnecessary, bill > 1000 ? 30 : 5 means the same thing.

    Also, for readability, always use spaces around operators.
    A? B : C is wrong. It should be A ? B : C.

    Now, since the ternary operator is right-associative, you don't need parenthesis when nesting, i.e.
    A ? B ? C : D : E means A ? (B ? C : D) : E
    A ? B : C ? D : E means A ? B : (C ? D : E)
    The nested ? : operator stays intact.

    There is no need for the parenthesis, however keep this in mind. A ? B : (C ? D : (E ? F : G)) is like an if-elseif-elseif-else statement, so the following are equivalent:

    // single line
    x = A ? B : C ? D : E ? F : G;
    
    // wrapped for readability, to show structure
    x = A ? B :
        C ? D :
        E ? F : G;
    
    // using if statement
    if (A) {
        x = B
    } else if (C) {
        x = D
    } else if (E) {
        x = F
    } else {
        x = G
    }
    

    No parenthesis are needed to clarify the ternary operator if adequate wrapping/indentation is used.

    In contrast, A ? (B ? C : D) : E is a nested if statement, so should have parenthesis to clarify that:

    // single line
    x = A ? B ? C : D : E;
    
    // wrapped for readability, to show structure
    x = A ? (B ? C
               : D)
          : E;
    
    // using if statement
    if (A) {
        if (B) {
            x = C
        } else {
            x = D
        }
    } else
        x = E
    }
    

    Ok, end of long story. Let's look at the question code:

    // from question
    int discount = (bill > 1000)? (qty > 11)? 10 : days > 9? 20 : 30 : 5;
    
    // normalized to have parenthesis only around nested operators
    int discount = bill > 1000 ? (qty > 11 ? 10 : (days > 9 ? 20 : 30)) : 5;
    
    // refactored for better structure (using only tail-nesting)
    int discount = bill <= 1000 ? 5 : (qty > 11 ? 10 : (days > 9 ? 20 : 30));
    
    // wrapped for readability
    int discount = bill <= 1000 ? 5 :
                   qty > 11 ? 10 :
                   days > 9 ? 20 : 30;
    
    // using if statement
    int discount;
    if (bill <= 1000) {
        discount = 5;
    } else if (qty > 11) {
        discount = 10;
    } else if (days > 9) {
        discount = 20;
    } else {
        discount = 30;
    }