Boolean Expressions and If Statements
Here, I explain if, if else statements, switch-case, De Morgan's law, and my overall learnings
- If Statements
- If-else Statements
- If-else If-else Statement
- If-else 5 conditions example
- Switch Case
- De Morgan's Law
- Resources I used aside from the given
if (score >= 90) {// the condition here is the score received on the test
grade = 'A'; // if the score is greater than 90, the action executed will be telling the user they got an A grade; if it's not true, the rest of the code will be executed and ignore what is between the brackets
}
if (score >= 90) {// the condition here is the score received on the test
grade = 'A'; // if the score is greater than 90, the action executed will be telling the user they got an A grade; if it's not true, the rest of the code will be executed and ignore what is between the brackets
}
else {
grade = 'Lower than an A'; // if the score is less than a 90, then this alternate code will run
}
if (time < 10) { // the condition here is the time
greeting = "Good morning"; // if the time is less than 10, the code will be executed
} else if (time < 20) { // the second condition is here if the number is not less than 10; it must be less than 20 for the code to run
greeting = "Good day"; // if the time is less than 20 but greater than 10, the code will be executed
} else { // if the number is greater than 20, then this alternate code will run since the first two statements were false
greeting = "Good evening"; // this code will be executed if the number is greater than 20
}
import java.util.Scanner;
public class IfElseStatements {
public void go() {
Scanner scan = new Scanner(System.in);
int score = scan.nextInt();
if (score <= 60) {
System.out.println("You got an F");
} else if (score <= 70) {
System.out.println("You got a D");
} else if (score <= 80) {
System.out.println("You got a C");
} else if (score <= 90) {
System.out.println("You got a B");
} else if (score <= 100) {
System.out.println("You got an A");
} else {
System.out.println("You didn't even get a grade!");
}
}
public static void main(String[] args) {
IfElseStatements cond = new IfElseStatements();
cond.go();
}
}
IfElseStatements.main(null);
Switch Case
A switch case is an if-else ladder that checks multiple conditions at once. The value that is inputted is checked with each given case until a match is found. Things to remember: two cases can't have the same value, data variable needs to be the same in all cases, and the value needs to be literal or constant not a variable.
import java.util.Scanner;
public class SwitchExample { // sets up class and objects for scanner
public void go() {
Scanner scan = new Scanner(System.in);
int score = scan.nextInt();
switch (score){ // switch statement with condition that goes through different cases to find a match
case 1: // for the score of less than 60
System.out.println("You got an F");
break;
case 2: // for the score between 60 and 70
System.out.println("You got a D");
break;
case 3: // for a score between 70 and 80
System.out.println("You got a C");
break;
case 4: // for a score between an 80 and a 90
System.out.println("You got a B");
break;
case 5: // for a score between a 90 and 100
System.out.println("You got an A");
break;
default: // if none of the cases are a match, this is what will be the code automatically executed
System.out.println("You didn't even get a grade");
}
}
public static void main(String[] args) {
SwitchExample cond = new SwitchExample();
cond.go();
}
}
SwitchExample.main(null);
De Morgan's Law
Allows for specific Boolean statements to be written in different ways to have the same effect. It's similar to probability in statistics. The || means "or" and the && means "and"
Written as: not (A or B) = (not A) and (not B)
not (A and B) = (not A) or (not B)
In code it's written as:
!(A && B) == !A || !B !(A && B) == !A && !B
boolean dog = false;
boolean labrador = true;
if (!(dog && windy)) { // using the "and"
System.out.println("You have a dog that's a labrador");
}
else {
System.out.println ("Your dog is another breed");
}
// checks if both boolean expressions are true and then inverses the results
boolean dog = false;
boolean labrador = true;
if (!dog || !labrador) { // using the "or"
System.out.println("You have a dog that's a labrador");
}
else {
System.out.println ("Your dog is another breed!");
}
// checks if dog is false or labrador is false