Search code examples
if-statementlanguage-agnosticnested

Nested if-else vs if-else?


I'm confused on what is meant by "nested if-else statements", compared to if-else if statements.

The following question asks to use it instead of if-else statements but I can't figure out how.

if (playerThrow == ROCK && computerThrow == ROCK) {
    System.out.println("Its a draw!");
} else if (playerThrow == ROCK && computerThrow == PAPER) {
    System.out.println("Computer wins!");
} else if (playerThrow == ROCK && computerThrow == SCISSORS) {
    System.out.println("Player wins!");
} /* ... */

Code is from my textbook (I messed up), else I would have posted mine, sorry


Solution

  • Nested if/else means a heirachy so..

    if($something == true){
        if($something_else == true){
        } else {
        }
    } else { // $something == false
        if($something_else == true){
        } else {
        }
    }