Ifs and Expressions Lesson
int n = 5;
if (n>0){
System.out.println("Greater than 0!"); //since 5>0, it is printing that.
}
int n = 10;
if (n>15){
System.out.println("Greater than 15!");
}
else{
System.out.println("Less than (or equal to) 15!"); //since n=10, it is less than 15
}
int n = 12;
if (n>15){
System.out.println("Greater than 15!");
}
else if (n>10){
System.out.println("Greater than 10!"); //Since 12 is not greater than 15 but is greater than 10, it is printed that it is greater than 10.
}
else{
System.out.println("Less than or equal to 10!");
}
int n = 12;
if (n>15){
System.out.println("Greater than 15!");
}
else if (n>13){
System.out.println("Greater than 13!");
}
else if (n>12){
System.out.println("Greater than 12!");
}
else if (n>11){
System.out.println("Greater than 11!"); //Since 12 is not greater than 15 but is greater than 11, it is printed that it is greater than 10.
}
else{
System.out.println("Less than or equal to 10!");
}
This same process can be repeated using switch-case instead
int n = 12;
String output;
switch (n){
case 9:
output = "greater than 8";
break;
case 10:
output = "greater than 9";
break;
case 11:
output = "greater than 10";
break;
case 12:
output = "greater than 11";
break;
}
System.out.println(output);
boolean A = true;
boolean B = true;
if (!(A && B)){
System.out.println("A and B both are false");
}
else{
System.out.println("A and B are true");
}
if (!A || !B){
System.out.println("A and B both are false");
}
else{
System.out.println("A and B are true");
}
As we can see, both of the if statements provide the same output, proving to us that De Morgan's law is true.