If-Else
- If statements are condiitional statements and use booleans to enter the statement
int temperature = 100;
if (temperature >= 90)
{
System.out.println("It is hot");
}
- If-else statments execute a block of code if the condition is true, but another if the condition is false
int temperature = 86;
if (temperature >= 90)
{
System.out.println("It is hot");
}
else
{
System.out.println("It is cold");
}
- If-elseif-else statements provide multiple conditions of else if statements in addition to the else statement
int temperature = 65;
if (temperature >= 90)
{
System.out.println("It is hot");
}
else if ((60 < temperature) && (temperature < 90))
{
System.out.println("It is warm");
}
else
{
System.out.println("It is cold");
}
double drivingAge = 15;
double permitAge = 15.5;
if (drivingAge >= 16)
{
System.out.println("You can drive");
}
else if (drivingAge >= 15.5)
{
System.out.println("You can get your permit");
}
else if ((drivingAge < 16) && (permitAge < 15.5))
{
System.out.println("You are too young to drive");
}
else if ((drivingAge < 16) && (permitAge >= 15.5))
{
System.out.println("You can drive with a parent");
}
else
{
System.out.println("Stay off the streets");
}
int drivingAge = 16;
switch(drivingAge)
{
case 0:
System.out.println("You were just born");
break;
case 15:
System.out.println("You can get your permit");
break;
case 16:
System.out.println("You can get your license");
break;
case 18:
System.out.println("You can get a vertical license");
break;
case 100:
System.out.println("This seems a little dangerous to be driving");
break;
default:
System.out.println("I'm not sure");
break;
}
- De Morgans law lits multiple conditions that need to be met to enter the code block
- Uses the operands AND, OR and NOT and two input variables, A and B
boolean a = true;
boolean b = false;
if (a) {
System.out.println("True code block");
}
if (a && !b) {
System.out.println("True code block");
}
if (a || b) {
System.out.println("True or False code block");
}
if ((a && !b) && (a || a)) {
System.out.println("true or false");
}
if (!a && b ) {
System.out.println("False code block");
}