Implementing Boolean into if else statements
boolean isTired = true; // setting boolean to true.
if(isTired) { // isTired is the same thing as if(isTired == True)
System.out.println("Go to sleep");
} else {
System.out.println("KeepWorking");
}
if-elseif-elseif-elsif-else statement, 5 or more conditions
int day= 4;
if(day==1){
System.out.println("Sunday");
}
else if(day==2){
System.out.println("Monday");
}
else if(day==3){
System.out.println("Tuesday");
}
else if(day==4){
System.out.println("Wednesday");
}
else if(day==5){
System.out.println("Thursday");
}
else if(day==6){
System.out.println("Friday");
}
else if(day==7){
System.out.println("Saturday");
}
Now let's switch up! Time to make things easier and use switch case
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
int day=3;
switch(day){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
}
DeMorgan’s laws were developed by Augustus De Morgan in the 1800s. They show how to handle the negation of a complex conditional, which is a conditional statement with more than one condition joined by an and (&&) or or (||), such as (x < 3) && (y > 2)
int x = 4, y = 3;
if (!(x < 3 || y > 2))
{
System.out.println("True");
}
else
{
System.out.println("False");
}
int x = 4, y = 3;
if (!(x < 3 && y > 2))
{
System.out.println("True);
}
else
{
System.out.println("False");
}