How do booleans work?
Booleans work on a true or false basis. In that there are two values that the variable can be represented by, "true" or "false". This Boolean logic can be used in conditional statements.
And
And logic gates require two or more conditions to be true for an overall condition to be true. For example...
int integerOne = 5;
int integerTwo = 7;
if(integerTwo > integerOne && integerTwo == 7){
//both of these statements are true
//the code here will execute
}
Or
Or statements require that at least one out of many conditions are true for an overall condition to be true. For example…
int integerOne = 5;
int integerTwo = 7;
if(integerTwo < integerOne || integerTwo == 7){
//only one of these statements are true
//since the condition is or it still returns true
//the code will execute
//the code here will execute
}
Other Boolean Logic Gates
Not: The condition is true if a data type is not a certain value. For Example…
int integerOne = 5;
int integerTwo = 7;
if(!(integerTwo > integerOne)){
//the statement is true
//since the condition is a 'not' it returns false
//the code will not carry out
//the code goes here
}
These can also be used with strings...
String hello = "Hello";
if(!hello.equals("world")){
//the statement is not true
//since the condition is a 'not' it returns true
//this is because it is not not true
//the code will carry out
//the code goes here
}
XOR(Exclusive or): The condition is true if the two given condition results differ. For Example…
int integerOne = 5;
int integerTwo = 7;
if(integerTwo > integerOne ^ integerTwo == 5){
/*since the first statement is true and the second is false...
the statement will return true*/
//this is because the result of the two statements are different
//the code will carry out
//the code goes here
}
NOR: The overall condition is false if one condition is true. For example...
int integerOne = 5;
int integerTwo = 7;
if(!(integerTwo > integerOne)&& !(integerTwo == 5)){
/*since one of the statements is true
and this is a nor, the overall condition is false*/
//a nor require all conditions to be false to return true
//the code will not carry out
//the code goes here
}
NAND: The overall condition is false if all conditions are met. For Example...
int integerOne = 5;
int integerTwo = 7;
if(!(integerTwo > integerOne&& integerTwo == 7)){
/*since both of the conditions are true
and this is a nand, the overall condition is false*/
//a nand require all conditions to be true to return false
//every other option would result in a true
//the code will not carry out
//the code goes here
}