Reputation: 15
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
Upvotes: 0
Views: 12095
Reputation: 1
Upvotes: 0
Reputation: 6360
A nested if statement
is essentially a series of if statements
within each other like so...
if (<condition>)
{
if (<condition>)
{
...
} else {
...
}
} else {
...
}
If-else if statements
make using multiple conditions cleaner and easier to read, all being on the same level, like so:
if (<condition 1>)
{
...
}
else if (<condition 2>)
{
...
}
else
{
...
}
Upvotes: 2
Reputation: 3064
Nested if/else means a heirachy so..
if($something == true){
if($something_else == true){
} else {
}
} else { // $something == false
if($something_else == true){
} else {
}
}
Upvotes: 2