if-elseif-else statements stop doing comparisons as soon as it finds one that's true. if-if-if does every comparison. The first is more efficient.
Edit: It's been pointed out in comments that you do a return within each if block. In these cases, or in cases where control will leave the method (exceptions), there is no difference between doing multiple if statements and doing if-elseif-else statements.
However, it's best practice to use if-elseif-else anyhow. Suppose you change your code such that you don't do a return in every if block. Then, to remain efficient, you'd also have to change to an if-elseif-else idiom. Having it be if-elseif-else from the beginning saves you edits in the future, and is clearer to people reading your code (witness the misinterpretation I just gave you by doing a skim-over of your code!).
java - Why we use if, else if instead of multiple if block if the body is a return statement - Stack Overflow
java - Are multiple 'if' statements and 'if-else-if' statements the same for mutually exclusive conditions? - Stack Overflow
If else statement in Java - Stack Overflow
if statement - if (boolean condition) in Java - Stack Overflow
Videos
if-elseif-else statements stop doing comparisons as soon as it finds one that's true. if-if-if does every comparison. The first is more efficient.
Edit: It's been pointed out in comments that you do a return within each if block. In these cases, or in cases where control will leave the method (exceptions), there is no difference between doing multiple if statements and doing if-elseif-else statements.
However, it's best practice to use if-elseif-else anyhow. Suppose you change your code such that you don't do a return in every if block. Then, to remain efficient, you'd also have to change to an if-elseif-else idiom. Having it be if-elseif-else from the beginning saves you edits in the future, and is clearer to people reading your code (witness the misinterpretation I just gave you by doing a skim-over of your code!).
What about the case where b1 == b2? (And if a == b1 and a == b2?)
When that happens, generally speaking, the following two chunks of code will very likely have different behavior:
if (a == b1) {
/* do stuff here, and break out of the test */
}
else if (a == b2) {
/* this block is never reached */
}
and:
if (a == b1) {
/* do stuff here */
}
if (a == b2) {
/* do this stuff, as well */
}
If you want to clearly delineate functionality for the different cases, use if-else or switch-case to make one test.
If you want different functionality for multiple cases, then use multiple if blocks as separate tests.
It's not a question of "best practices" so much as defining whether you have one test or multiple tests.
When you write multiple if statements, it's possible that more than one of them will be evaluated to true, since the statements are independent of each other.
When you write a single if else-if else-if ... else statement, only one condition can be evaluated to true (once the first condition that evaluates to true is found, the next else-if conditions are skipped).
You can make multiple if statements behave like a single if else-if .. else statement if each of the condition blocks breaks out of the block that contains the if statements (for example, by returning from the method or breaking from a loop).
For example :
Copypublic void foo (int x)
{
if (x>7) {
...
return;
}
if (x>5) {
...
return;
}
}
Will have the same behavior as :
Copypublic void foo (int x)
{
if (x>7) {
...
}
else if (x>5) {
...
}
}
But without the return statements it will have different behavior when x>5 and x>7 are both true.
Yes, it makes a difference: see The if-then and if-then-else Statements.
Furthermore, you can easily test it.
Code #1:
Copy int someValue = 10;
if(someValue > 0){
System.out.println("someValue > 0");
}
if(someValue > 5){
System.out.println("someValue > 5");
}
Will output:
CopysomeValue > 0
someValue > 5
While code #2:
Copy int someValue = 10;
if(someValue > 0){
System.out.println("someValue > 0");
}else if(someValue > 5){
System.out.println("someValue > 5");
}
Will only output:
CopysomeValue > 0
As you can see, code #2 never goes to the second block, as the first statement (someValue > 0) evaluates to true.
Okay, so..
// As you already stated, you know that a boolean defaults to false.
boolean turnedOn;
if(turnedOn) // Here, you are saying "if turnedOn (is true, that's implicit)
{
//then do this
}
else // if it is NOT true (it is false)
{
//do this
}
Does it make more sense now?
The if statement will evaluate whatever code you put in it that returns a boolean value, and if the evaluation returns true, you enter the first block. Else (if the value is not true, it will be false, because a boolean can either be true or false) it will enter the - yep, you guessed it - the else {} block.
A more verbose example.
If I am asked "are you hungry?", the simple answer is yes (true). or no (false).
boolean isHungry = true; // I am always hungry dammit.
if(isHungry) { // Yes, I am hungry.
// Well, you should go grab a bite to eat then!
} else { // No, not really.
// Ah, good for you. More food for me!
// As if this would ever happen - bad example, sorry. ;)
}
In your example, the IF statement will run when it is state = true meaning the else part will run when state = false.
if(turnedOn == true) is the same as if(turnedOn)
if(turnedOn == false) is the same as if(!turnedOn)
If you have:
boolean turnedOn = false;
Or
boolean turnedOn;
Then
if(turnedOn)
{
}
else
{
// This would run!
}
Prompt: The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in.
sleepIn(false, false) โ true sleepIn(true, false) โ false sleepIn(false, true) โ true
my code:
public boolean sleepIn(boolean weekday, boolean vacation) { if (weekday == false) { return true; } if (vacation = true) { return true; } else { return false; } }
output: Expected Run sleepIn(false, false) โ true true OK
sleepIn(true, false) โ false true X
sleepIn(false, true) โ true true OK
sleepIn(true, true) โ true true OK
Shouldn't (true, false) produce a false output?
You have used = (assignment) instead of == (comparison).
Note the 'vacation = true'.
What this means is 'set vacation to true'. It does not mean 'check if vacation is equal to true'.
This means you have to make it 'if(vacation == true)'
Slightly relevant story about this mistake.
If you want to convert something like:
if(A) {
return X;
}
else if(B) {
return Y;
}
else {
return Z;
}
You can write this as:
A ? X : (B ? Y : Z);
You thus write the else if as a condition in the else-part (after :) of the upper expression.
However, I would strongly advice against too much cascading. The code becomes extremely unreadable and the ? : code structure was never designed for this.
You can extend this to any number of clauses, in perfect analogy to the if-else construct.
return a == b? "b"
: a == c? "c"
: a == d? "d"
: "x";
In this form it quite closely resembles Lisp's cond, both in shape and in semantics.
But, do note that this is not a "shorthand for if/else" because it is an expression whereas if/else is a statement. It would be quite bad abuse of the ternary operator if the expressions had any side effects.