This is always very confusing to me. Can someone please explain it? The confusion I have is - boolean default to false. So in the below example, does it enter the if
loop when state is not turned on, i.e., it is TURNED OFF if (condition is false)
OR does it enter the if
loop when state is TURNED ON, in other words if (condition is true)
?
boolean turnedOn;
if (turnedOn) {
//do stuff when the condition is false or true?
} else {
//do else of if
}
I know this is a very basic question, but if you could explain the answer in very basic language, that would be great. :) Feel free to point me to duplicate posts that have a very good explanation (I did not find one where I could clearly get it). Also feel free to change the subject of the post if you'd like to make it more generic.
Suppose you want to check a boolean. If true, do something. Else, do something else. You can write:
if(condition==true){
}
else{ //else means this checks for the opposite of what you checked at if
}
instead of that, you can do it simply like:
if(condition){ //this will check if condition is true
}
else{
}
Inversely. If you were to do something if condition was false and do something else if condition was true. Then you would write:
if(condition!=true){ //if(condition=false)
}
else{
}
But following the simple path. We do:
if(!condition){ //it reads out as: if condition is not true. Which means if condition is false right?
}
else{
}
Think about it. You'll get it in no time.
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!
}
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. ;)
}
ABoolean (with a uppercase 'B') is a Boolean object, which if not assigned a value, will default to null. boolean (with a lowercase 'b') is a boolean primitive, which if not assigned a value, will default to false.
Boolean objectBoolean;
boolean primitiveBoolean;
System.out.println(objectBoolean); // will print 'null'
System.out.println(primitiveBoolean); // will print 'false'
Citation
so in your code because boolean with small 'b' is declared it will set to false hence
boolean turnedOn;
if(turnedOn) **meaning true**
{
//do stuff when the condition is false or true?
}
else
{
//do else of if ** itwill do this part bechae it is false
}
the if(turnedon) tests a value if true, you didnt assign a value for turned on making it false, making it do the else statement :)
boolean turnedOn;
if(turnedOn)
{
//do stuff when the condition is true - i.e, turnedOn is true
}
else
{
//do stuff when the condition is false - i.e, turnedOn is false
}
boolean state = "TURNED ON";
is not a Java valid code. boolean can receive only boolean values (true or false) and "TURNED ON"
is a String.
EDIT:
now you are talking about a loop and your code does not contain any. your var state
is false because the boolean default value and you execute the else clause.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With