If Statements

If the conditional is true, run the code block

// 1+1 does equal 2, therefore the code block is ran and the system prints out "1+1=2"
if (1+1 == 2) {
    System.out.println("1+1=2");
}
// 1+1 does not equal 3, therefore the code block will not run and the system does not print out "1+1=3"
if (1+1 == 3) {
    System.out.println("1+1=3");
}
1+1=2

If-else Statements

If the conditional is true, run the code block inside if If the conditional is false, run the code block inside else

// 1+1 does equal 2, therefore the code block inside if is ran and the system prints out "1+1=2"
if (1+1 == 2) {
    System.out.println("1+1=2");
} else {
    System.out.println("1+1!=2");
}
// 1+1 does not equal 3, therefore the code block inside else is ran and the system prints out "1+1!=2"
if (1+1 == 3) {
    System.out.println("1+1=3");
} else {
    System.out.println("1+1!=3");
}
1+1=2
1+1!=3

If-elseif-else Statements

Goes through each if and elseif conditionals to see if it is true, if a conditional is true, the code block inside it will run If none of the conditionals are true, code block inside else will run

if (1+1 == 0) {
    // Because 1+1 does not equal 0, this does not run
    System.out.println("1+1=0");
} else if (1+1 == 1) {
    // Because 1+1 does not equal 1, this does not run
    System.out.println("1+1=1");
} else if (1+1 == 2) {
    // Because 1+1 does equal 2, this does run
    System.out.println("1+1=2");
} else if (1+1 == 3) {
    // skips this conditional because previous conditional already evaluated to be true
    System.out.println("1+1=3");
} else {
    // skips this because 1+1 == 2 conditional already evaluated to be true
    System.out.println("1+1 does not equal 0, 1, 2, or 3");
}
1+1=2

Letter Grade Calculator with If-elseif-else statements

double Grade = 93.9;
if (Grade < 60) {
    // If Grade is less than 60
    System.out.print("F");
} else if (Grade < 70) {
    //If Grade is less than 70 (60 or greater because previous conditional cleared to be false)
    System.out.print("D");
} else if (Grade < 80) {
    //If Grade is less than 80 (70 or greater because previous conditionals cleared to be false)
    System.out.print("C");
} else if (Grade < 90) {
    //If Grade is less than 90 (80 or greater because previous conditionals cleared to be false)
    System.out.print("B");
} else {
    //If Grade is 90 or greater (because previous conditionals cleared to be false)
    System.out.print("A");
}
A

Letter Grade Calculator with switch-case statements

import java.lang.Math;
double Grade = 93.9;
int floor = (int) Math.floor(Grade / 10) * 10;
switch(floor) {
    case 80: // If Grade is 80 up to but not including 90
        System.out.println("B");
        break;
    case 70: // If Grade is 70 up to but not including 80
        System.out.println("C");
        break;
    case 60: // If Grade is 60 up to but not including 70
        System.out.println("D");
        break;
    case 50: // If Grade is 50 up to but not including 60
        System.out.println("F");
        break;
    case 40: // If Grade is 40 up to but not including 50
        System.out.println("F");
        break;
    case 30: // If Grade is 30 up to but not including 40
        System.out.println("F");
        break;
    case 20: // If Grade is 20 up to but not including 30
        System.out.println("F");
        break;
    case 10: // If Grade is 10 up to but not including 20
        System.out.println("F");
        break;
    case 0: // If Grade is 0 up to but not including 10
        System.out.println("F");
        break;
    default: // anything else so f Grade is 90 or more or less than 0
        System.out.println("you either have negative grade or an A");
        break;
}
you either have negative grade or an A

De Morgan's law

From Wikipedia:

  • The negation of a disjunction is the conjunction of the negations
  • The negation of a conjunction is the disjunction of the negations or
  • The complement of the union of two sets is the same as the intersection of their complements
  • The complement of the intersection of two sets is the same as the union of their complements

NOT(A or B) is the same as NOT(A) and NOT(B) NOT(A and B) is the same as NOT(A) or NOT(B)

De Morgan's Law

boolean a = true;
boolean b = false;
System.out.println("A is " + a);
System.out.println("B is " + b);
System.out.println("NOT(A or B): " + Boolean.toString(!(a||b)));
System.out.println("NOT(A) and NOT(B): " + Boolean.toString((!a)&&(!b)));
System.out.println("NOT(A and B): " + Boolean.toString(!(a&&b)));
System.out.println("NOT(A) or NOT(B): " + Boolean.toString((!a)||(!b)));
if ((!(a||b)) == ((!a)&&(!b))) {
    System.out.print("NOT(A or B) is the same as NOT(A) and NOT(B)\n");
}
if ((!(a&&b)) == ((!a)||(!b))) {
    System.out.print("NOT(A and B) is the same as NOT(A) or NOT(B)");
}
A is true
B is false
NOT(A or B): false
NOT(A) and NOT(B): false
NOT(A and B): true
NOT(A) or NOT(B): true
NOT(A or B) is the same as NOT(A) and NOT(B)
NOT(A and B) is the same as NOT(A) or NOT(B)