Give an example for boolean data type.
Boolean is the type required by the conditional expressions used in control statements such as if and for.
Answers
Hi
Boolean data type can have either true or false as value
All conditional operatorators like
> greater than,
< less than,
>= greater than equal to,
<= less than equal to,
== equal to,
!= not equal to,
will return boolean value either true or false on the basis of given values
for example
if (10 > 5) {
System.out.println("10 is greater than 5");
} else {
System.out.println("10 is NOT greater than 5");
}
I want to mention that "if" block executes only if given condition returns true, in our case it is returning true becaue 10 is greater than 5.
so after doing logical operations on 10 > 5 computer knows that this condition is true
so now you can make your mental model about this "if" block like below
if (true) {
System.out.println("10 is greater than 5");
} else {
System.out.println("10 is NOT greater than 5");
}
"10 is greater than 5" will get printed on the screen because 10 is greater than 5 and it's true.
another example:
if (10 == 5) {
System.out.println("10 is equal to 5");
} else {
System.out.println("10 is NOT equal to 5");
}
10 == 5 condition will return false because 10 is not equal to 5 right.
so now you can mentally think of this "if" block like below
if (false) {
System.out.println("10 is equal to 5");
} else {
System.out.println("10 is NOT equal to 5");
}
so now we know anything between that "if" block executes only if condition is true, but in our case it is false because 10 is not equals to 5
so "else" block will get executed, so the output will be
"10 is NOT equal to 5"
Hope you understood.
public static void main(String args[]) {
boolean b1,b2,b3;
b1 = true; // Assigning Value
b2 = false; // Assigning Value
b3 = b2; // Assigning Variable
System.out.println(b1); // Printing Value
System.out.println(b2); // Printing Value
System.out.println(b3); // Printing Value
}
Output :
true
false
false
The above one is an example in JAVA. Boolean data type is used for logical values. This data type can have two possible values : true or false.This datatype is the type returned by all relational operators
Boolean is the type required by the conditional expressions used in control statements such as if and for.
boolean b1,b2,b3;
b1 = true; // Assigning Value
b2 = false; // Assigning Value
b3 = b2; // Assigning Variable
System.out.println(b1); // Printing Value
System.out.println(b2); // Printing Value
System.out.println(b3); // Printing Value
}
Output :
true
false
false