Boolean Expressions and If Statements Lesson
Week 3 Meena's Ifs Lesson
if (condition) {
//run this code if the condition is true
}
// Here is an example
int num1 = 10;
int num2 = 20;
if (num1 < num2) {
System.out.println("10 < 20 is true");
}
if (num1 > num2) {
//this statement will not be printed as the condition is false
System.out.println("10 > 20 is true");
}
if (condition) {
//run this code if the condition is true
} else {
// run this code if the condition is false
}
// Going back to the previous example
int num1 = 10;
int num2 = 20;
if (num1 < num2) {
System.out.println("10 < 20 is true");
} else {
//this statement will not be printed as the condition above was true so this code will never run
System.out.println("10 < 20 is false");
}
if (num1 > num2) {
System.out.println("10 > 20 is true");
} else {
//this statement will be printed as the condition above was false so this code will run
System.out.println("10 > 20 is false");
}
if (firstCondition) {
//run this code if firstCondition is true
} else if (secondCondition) {
// run this code if firstCondition is false and secondCondition is true
} else {
// run this code if firstCondition is false and secondCondition is false
}
// Going back to the previous example
int num1 = 10;
int num2 = 20;
if (num1 > num2) {
// num1 > num2 is false so this code will not run and will move on to check the next condition
System.out.println("10 > 20 is true");
} else if (num1 < num2) {
// this statement will be printed as the condition above was false and this conditions is true
System.out.println("10 < 20 is true");
//any number of else-if statements can be used
} else {
// this statement will never be printed as the previous condition was true
System.out.println("10 = 20 is true");
}
switch(expression) {
case case1:
//code
break;
case case2:
//code
break;
case case3:
//code
break;
//any number of case statements can be used
default:
//code
}
The expression specified is checked and compared with each case and if the expression matches a case, the code under that case will run.
break
statements are often used with switch-case. break
exits the loop it is in and move on to the next part of the code.
The code under default
will run if none of the cases are true.
Below is an example of taking an if-elseif-else statement with multiple conditions and converting it to a switch-case statement
boolean raining = true;
boolean cold = false;
boolean windy = true;
if (cold) {
//checks if cold is true but it's false so moves on to next condition
System.out.println("It is cold");
} else if ( cold && windy ) {
//cold is false and windy is true so the condition is false
System.out.println("It is cold and windy");
} else if ( raining && cold) {
// raining true but cold false so condition is false
System.out.println("It is raining and it is cold");
} else if ( raining && cold && windy) {
// again, cold is false so the condition is false
System.out.println("It is raining, windy, and cold");
} else if ( windy && cold) {
// again, cold is false so the condition is false
System.out.println("It is windy and it is cold");
} else if ( raining && windy) {
// prints out this statement since both conditions are true
System.out.println("It is windy and it is raining");
}
else {
// doesn't get to this part of the code since the previous condition was true
System.out.println("It could be raining, cold, and/or windy");
}
String weather = "windy and raining";
switch (weather) {
case "cold":
// checks if weather has a value of "cold" --> it doesn't so moves on to check next case
System.out.println("It is cold");
break;
case "raining":
System.out.println("It is raining");
break;
case "raining and cold":
System.out.println("It is raining and it is cold");
break;
case "raining and cold and windy":
System.out.println("It is raining, windy, and cold");
break;
case "windy and cold":
System.out.println("It is windy and it is cold");
break;
case "windy and raining":
// this case matches the String weather so this statement is printed out
System.out.println("It is windy and it is raining");
break;
default:
// doesn't run this code since a case matched the expression
System.out.println("It could be raining, cold, and/or windy");
}
boolean cold = false;
boolean windy = true;
//this first if-else statement checks if both booleans are true and then reverse the result
if (!(cold && windy)) {
System.out.println("It is not both cold and windy");
} else {
System.out.println("The condition is false");
}
//this first if-else statement checks if cold is not true or if windy is not true
if (!cold || !windy) {
System.out.println("It is not both cold and windy");
} else {
System.out.println("The condition is false");
}
// Based on De Morgan's Law, both if-else statements provide the same output