Below is syntax with example
var_name =true; // initializing ‘true’ value into variable
var-name= false; //initialize variable again with ‘false’ value
example based on above statement
b=true; //b initialized
b=false; //b initialized again
Boolean data type with if statement
As you all know that we use boolean type for getting required result from condition.There is two possible result .First one is for true and other one is for false.
1. With true value.
syntax
Because b is true it goes inside the if statement and gives required result
2. With false value
syntax
Because b is false it does not go inside if to execute listed statements,it directly goes on statement2.
Let see the example of boolean type.
problem–>>
Execute a java program for performing some conceptual operation with boolean datatype.
code–>>
boolean a;
a=true;
System.out.println(“a is “+a);
a=false;System.out.println(“a become “+a)
;a=true;
if(a)
{
System.out.println(“This statement should be executed”);
}
a=false;
if(a)
{
System.out.println(“This statement should not be executed”);
}
System.out.println(“5>9= ” +(5>9));
System.out.println(“15>9= ” +(15>9));
}
}
output–>>
a become false
This statement should be executed
5>9= false
15>9=true
Logic–>>
First we have declared class named bool.Now we have declared variable a havig boolean data type.The value of a become true after its first initialization.Now we have simply printed that statement about value a.Now we value of a become false after its second initialization and performed same operation as above.Then after third initialization value of a become true,so it goes inside the if statement and prints the required result.Now a become false so it will not perform the next operation and jump to directly the next statement outside the curly braces.That statement directly checks the data (5>9) and give us the result that it is true or not,as you all know it becomes false.Then the last statement prints 15>9= true
Contact us for any difficulty