If Else Statement Lesson
Lesson to show understanding of if else statements
- If Statement
- If-else Statement
- If-elseif-else Statement
- If-elseif-else statement (5 or more conditions)
- Switch Case
- De Morgan's Law
- Resources used
boolean a = true;
// Set the boolean to "true"
if (a = false) {
System.out.println("This is false");
// Will run if a is false
}
if (a = true) {
System.out.println("This is true");
// Will run if a is true
}
boolean b = false;
// Set the boolean to "false"
if (b = true) {
System.out.println("b is true");
}
else {
System.out.println("b is not true");
}
// The if condition is not met so the else statement is ran
boolean c = true;
// Sets the boolean to "true"
if (c = false) {
System.out.println("The first if statement is true");
// If the if condition is not true then it will not run
}
else if (c = !true) {
System.out.println("This else if statement is true");
}
else if (c = false && true) {
System.out.println("This else if statement is true");
}
else if (c = !(!false || !true)) {
System.out.println("This else if statement is true");
// If the else if condition is not true then then it will not run
}
else {
System.out.println("None of the other conditions were true");
// None of the if statements or else if statements are true so the else statement will run
}
int stars = 3; // The integer "stars" is determined to be 3
if (stars <= 0) {
System.out.println("You got ☆☆☆☆☆");
}
else if (stars <= 1) {
System.out.println("You got ★☆☆☆☆");
}
else if (stars <= 2) {
System.out.println("You got ★★☆☆☆");
}
else if (stars <= 3) {
System.out.println("You got ★★★☆☆");
}
else if (stars <= 4) {
System.out.println("You got ★★★★☆");
}
else {
System.out.println("You got ★★★★★");
}
// The code goes through the if and else if conditions to figure out how many stars to show.
Switch Case
A switch case is able to run one statement after choosing one from multiple statements. This makes it similar to an else-if ladder where it will go through mulitple conditions to see which is true.
When making a switch case code, it is important that you make sure no two cases have the same value and that the same variable is used for all the cases.
int stars = 4; // The integer "stars" is determined to be 3
switch (stars) { // A switch statement that takes in the "stars" integer
case 1: // When there are 0 stars
System.out.println("You got ☆☆☆☆☆");
break;
case 2: // When there is 1 star
System.out.println("You got ★☆☆☆☆");
break;
case 3: // When there are 2 stars
System.out.println("You got ★★☆☆☆");
break;
case 4: // When there are 3 stars
System.out.println("You got ★★★☆☆");
break;
case 5: // When there are 4 stars
System.out.println("You got ★★★★☆");
break;
default: //When none of the other cases are met so there are 5 stars
System.out.println("You got ★★★★★");
}
De Morgan's Law
De Morgan's Law states that the "!" symbol will negate an And statement and an Or statement. This means that if a condition is written as a || b, !(a || b) would really be a && b. This goes for the other way around where !(a && b) is a || b. It will also cause a true statement to be false with !(true) and vice versa.
boolean child = true;
boolean hungry = false;
if (!(child && hungry)) {
System.out.println("You have a hungry child");
}
else {
System.out.println("Who knows what's going on?");
}
boolean fruit = true;
boolean red = true;
if (!(!(fruit) || !(red))) {
System.out.println("It's probably a red apple");
}
else {
System.out.println("You have an unknown food");
}