If-Else Notebook
Definitions:
Conditional Statement:
- A statement that only executes when a condition is true
Boolean:
- A primitive data type that can be either true or false
If Statement:
- The code in the body of an if statement will only execute if the code is true
If-Else Statement:
- Same as an if statement, but if the condition is false the code in the body of the else statement will execute
(==) : equal to
(!=) : not equal to
(<=) : less than or equal to
(>=) : greater than or equal to
(&&) : and
(||) : or
Complex 5 layer While, If, and Else statement program:
import java.util.Scanner;
// this is a program that would display if a score in a game is enough to advance to the next level
public class Score {
public static void main(String[] args) {
// where the user inputs their score
Scanner input = new Scanner(System.in);
int score = 0;
// makes it so the program runs until the user decides to quit the program
while (score != -100) {
System.out.print("Enter the student's score (or -100 to quit): ");
score = input.nextInt();
// if the score is less than 30, the user cannot advance
if (score >= 0 && score <= 29) {
System.out.println("\nScore: {" + score + "} | is not enough to advance to the next level.");
}
// if the score is then less than 80, they can go on, but haven't beaten the high score of 80
else {
if (score >= 30 && score <= 79) {
System.out.println("\nScore: {" + score + "} | is enough to advance, but is less than the high score.");
}
// if they got higher than 80, they've gotten the high score
else {
if (score >= 80 && score <= 100) {
System.out.println("\nScore: {" + score + "} | is enough to advance, and greater than the high score!");
}
// this is what runs if they decide to quit the program
else {
if (score == -100) {
System.out.println("Goodbye!");
}
// this is if the user types in an invalid score
else {
System.out.println("The score entered is invalid. Please try again.");
}
}
}
}
}
}
}
Score.main(null);
De Morgan's Laws:
- De Morgan's Law of Union: The complement of the union of the two sets A and B will be equal to the intersection of A' (complement of A) and B' (complement of B). This is also known as De Morgan's Law of Union. It can be represented as (A ∪ B)’ = A’ ∩ B’.
- De Morgan's Law of Intersection: The complement of the intersection of A and B will be equal to the union of A' and B'. This condition is called De Morgan's law of Intersection. It can be given by (A ∩ B)’ = A’ ∪ B’.
Simplified:
- not(A and B) --> notA or notB
- not(A or B) --> notA and notB
- not(A and B) =/= notA and notB