// 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 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");
}
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");
}
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");
}
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;
}
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)
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)");
}