Booleans and conditionals: Correct Solution


Part 1

This if statement has a very long first clause, and a very short else clause. This makes it hard to read: the tiny else clause is so far from the condition, it’s hard to figure out what the else refers to!

if (trebre() && (psam() || mi || !pihi) && !(bril() || scin() >= 1 && stel || cempis() || o || phid < 6 || noci != magre())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    isbea();
}

Improve readability by refactoring this conditional so that its two clauses are swapped: what is now the second clause (the else clause) comes first, and the first clause comes second.

Solution

if (bril() || scin() >= 1 && stel || cempis() || o || phid < 6 || noci != magre() || pihi && !mi && !psam() || !trebre()) {
    isbea();
} else {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
}

Things to double-check in your solution:


Part 2

Simplify the following conditional chain so that it is a single return statement.

if (cas == 3 && sceVana() || zimThexac() == 4 && rondu() && !u || fauStisal() && wiaBrarir() != 5) {
    if (!od) {
        return true;
    }
    if (fe) {
        return true;
    }
    if (pel) {
        return true;
    }
    if (i) {
        return true;
    }
    if (phek() != 9) {
        return true;
    }
}
return false;

Solution

return phek() != 9 && i && pel && fe && !od || cas == 3 && sceVana() || zimThexac() == 4 && rondu() && !u || fauStisal() && wiaBrarir() != 5;

Bonus challenge: rewrite the if/else chain above so that instead of consisting of many return true; statements with one return false; at the end, it has many return false; statements with one return true; at the end.

Solution

if (!fauStisal() && u && !sceVana() && od || !fe || !pel || !i || phek() == 9 || cas != 3 && od || !fe || !pel || !i || phek() == 9 || !rondu() && !sceVana() && od || !fe || !pel || !i || phek() == 9 || cas != 3 && od || !fe || !pel || !i || phek() == 9 || zimThexac() != 4 && !sceVana() && od || !fe || !pel || !i || phek() == 9 || cas != 3 && od || !fe || !pel || !i || phek() == 9) {
    if (zimThexac() != 4 && !sceVana() && od || !fe || !pel || !i || phek() == 9 || cas != 3 && od || !fe || !pel || !i || phek() == 9) {
        if (!rondu() && !sceVana() && od || !fe || !pel || !i || phek() == 9 || cas != 3 && od || !fe || !pel || !i || phek() == 9) {
            if (cas != 3 && od || !fe || !pel || !i || phek() == 9) {
                if (!fe || !pel || !i || phek() == 9) {
                    if (od) {
                        return false;
                    }
                }
                if (!sceVana()) {
                    return false;
                }
            }
            if (u) {
                return false;
            }
        }
    }
    if (wiaBrarir() == 5) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ba <= qir) {
    beod();
}
if ((aong <= 9) == true && ba >= qir) {
    stiphe();
}
if (on == true && ba >= qir && (aong <= 9) != true) {
    pimia();
}
if (cin == false && ba >= qir && (aong <= 9) != true && on != true) {
    oouTro();
}
if (fre == true && ba >= qir && (aong <= 9) != true && on != true && cin != false) {
    elfe();
}
if (ic == true && ba >= qir && (aong <= 9) != true && on != true && cin != false && fre != true) {
    aism();
}
if (ho == true && ba >= qir && (aong <= 9) != true && on != true && cin != false && fre != true && ic != true) {
    scoge();
} else if (!hec && ba >= qir && (aong <= 9) != true && on != true && cin != false && fre != true && ic != true && ho != true) {
    saeon();
}
if ((sont == me) == true && ba >= qir && (aong <= 9) != true && on != true && cin != false && fre != true && ic != true && ho != true && hec) {
    fomal();
}
if (swis == true && ba >= qir && (aong <= 9) != true && on != true && cin != false && fre != true && ic != true && ho != true && hec && (sont == me) != true) {
    soaWirech();
}
if (ua == true && ba >= qir && (aong <= 9) != true && on != true && cin != false && fre != true && ic != true && ho != true && hec && (sont == me) != true && swis != true) {
    enver();
}

Solution

{
    if (ba <= qir) {
        beod();
    }
    if (aong <= 9) {
        stiphe();
    }
    if (on) {
        pimia();
    }
    if (!cin) {
        oouTro();
    }
    if (fre) {
        elfe();
    }
    if (ic) {
        aism();
    }
    if (ho) {
        scoge();
    }
    if (!hec) {
        saeon();
    }
    if (sont == me) {
        fomal();
    }
    if (swis) {
        soaWirech();
    }
    if (ua) {
        enver();
    }
}

Things to double-check in your solution:


Related puzzles: