Boolean and If Statements Lesson
int x = 20; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
System.out.println(x+" is greater than 18.");
}
int x = 8; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
System.out.println(x+" is greater than 18.");
}
else { //this is what will print if the "if" statement is false
System.out.println(x+ " is less than 18.");
}
int x = 8; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
System.out.println(x+" is greater than 18.");
}
else if (x>9) { //this is what will print if the "if" statement is false
System.out.println(x+ " is greater than 9.");
}
else{
System.out.println(x+" is smaller than 9."); //what will print if nothing above is true
}
int x = 13; // the integer given in the variable is 20
if (x > 18) { //since this condition is true, the statement will print
System.out.println(x+" is greater than 18.");
}
else if (x>16) { //this is what will print if the "if" statement is false
System.out.println(x+ " is greater than 16.");
}
else if (x>14) {
System.out.println(x+" is greater than 14");
}
else if (x>12) {
System.out.println(x+" is greater than 12");
}
else{
System.out.println(x+" is smaller than 12."); //what will print if nothing above is true
}
You can write this as a switch case.
int x = 13; // the integer given in the variable is 20
String output;
switch (x) {
case 10:
output = x + " is equal to 10";
break;
case 11:
output = x + " is equal to 11";
break;
case 12:
output = x + " is equal to 12";
break;
case 13:
output = x + " is equal to 13";
break;
}
System.out.println(output);
boolean vanilla = true;
boolean chocolate = true;
if (!(chocolate && vanilla)){
System.out.println("I do not like chocolate or vanilla");
}
else{
System.out.println("I like chocolate and vanilla");
}
if (!chocolate || !vanilla){
System.out.println("I do not like chocolate or vanilla");
}
else{
System.out.println("I like chocolate and vanilla");
}
Both the statements have the same output.