Boolean Expressions and If Statements
Focus on Ifs, Switch, Expressions and how to build control structures in Java.
Expression, Assignments, and Condition Statements
All of the Units have video series. We have access to information and content for College Board and Code.org. Below are two sample videos, it is up to you to determine the detail or brevity that you will need according to your experience.
- AP Classroom Boolean Expressions
- Code.org If and Conditions, less than 2 minutes
// All of the boolean expressions below evaluate to true
if (true) {
System.out.println("True code block");
}
if (true && !false) {
System.out.println("True and Not False code block");
}
if (true || false) {
System.out.println("True or False code block");
}
if ((true && !false) && (true || false)) {
System.out.println("Confusing code block");
}
if (!((false || !true) || (false && true))) {
System.out.println("De Morgan's law in my head of confusing code block");
}
// Can any of the above expression be simplified? What would they simplify to? Are any of these expressions useful?
Hacks
Build your own Jupyter Notebook lesson on ifs
- Explain if, if-else, and if-elseif-else.
- Make a markdown block before you sample code
- Comment in code to describe each decision
Add to lesson switch-case
- Create and if-elseif-elseif-elseif-else statement, 5 or more conditions.
- Covert the 5 or more decisions to a switch-case-case-case-case-otherwise.
- Make a markdown block before each code example
- Comment/establish a style of comments for your if-elseif and switch-case code blocks
Finish lesson with De Morgan's law
- Describe De Morgan's law
- Illustrate De Morgan's law
- Show some code running that shows understanding
Resources, it is really time to show you can find resources beyond the Teacher. Code/Code/Coding is everywhere, find something that helps.
- Code.org Unitt 4 section 1 to 5 can help with some ideas.
- AP Classroom unit 3 has outline for unit
- CodeAcademy has some online resources
Personal hacks
If Statements
If statements are, in my opinion, at the core of all conditional statements. Essentially, whatever comes inside if statements only runs if the expression inside the if statement evaluates to be true. Otherwise, the code inside will not run and will move on.
If the programmer wants to create other code to run in a false case, then they have the else statement to use in an if-else statement. Else statements only run if the if statement evalulates to false.
If statements can be chained in succession using the if-elseif-else statement. The if and else components work exactly the same, but the elseif part can be thought of as a combination between the two. Elseif only runs if the if (or another elseif) satement before evalulates to false and the expression inside the elseif evalulates to true. These elseif statements can be stacked over and over again to handle all sorts of different cases.
Taken altogether, these three statements can be used to handle every sort of conditional case imaginable. It may be a bit confusing to have it explained on text, but it's much easier to just use actual code to demonstrate:
public class ifTest {
public static void main(String[] args) {
int numberOne = 5;
// if statement
// Since numberOne (5) is greater than 1, then it evaluates to be true and the print statement below runs
if (numberOne > 1) {
System.out.println(numberOne + " is greater than 1!");
}
// if-else statement
// Since numberOne (5) is not greater than 9, then it evalulates to be false and then the else portion runs instead of the if
if (numberOne > 9) {
System.out.println(numberOne + " is greater than 9!");
} else {
System.out.println(numberOne + " is not greater than 9!");
}
// if-elseif-else statement
// numberOne (5) is not equal to 1, so the else-if runs next. Since numberOne (5) is equal to 5, then the code under else-if runs
if (numberOne == 1) {
System.out.println("numberOne is equal to 1!");
} else if (numberOne == 5) {
System.out.println("numberOne is equal to 5!");
} else {
System.out.println("I don't know what numberOne is!");
}
}
}
ifTest.main(null);
public class elseifStatements {
public static void main(String[] args) {
String favoriteColor = "green";
if (favoriteColor.equals("red")) {
System.out.println("The favorite color is red!");
} else if (favoriteColor.equals("orange")) {
System.out.println("The favorite color is orange!");
} else if (favoriteColor.equals("yellow")) {
System.out.println("The favorite color is yellow!");
} else if (favoriteColor.equals("green")) {
System.out.println("The favorite color is green!");
} else if (favoriteColor.equals("blue")) {
System.out.println("The favorite color is blue!");
} else if (favoriteColor.equals("purple")) {
System.out.println("The favorite color is purple!");
} else {
System.out.println("I don't know what the favorite color is!");
}
}
}
elseifStatements.main(null);
Switch Cases
Switch cases are similar to if statements in that they run certain lines of code when certain conditions are true. However, switch cases only run when a prespecified variable is equal to whatever is given in each of the cases.
Why would I use this? Well, they can be more efficient than writing a ton of elseif statements.
This is also a bit confusing to explain through text, so here's another example (note how break is used to prevent everything below it from running):
public class switchTest {
public static void main(String[] args) {
String favoriteColor = "green";
switch (favoriteColor) {
case "red":
System.out.println("The favorite color is red!");
break;
case "orange":
System.out.println("The favorite color is orange!");
break;
case "yellow":
System.out.println("The favorite color is yellow!");
break;
case "green":
System.out.println("The favorite color is green!");
break;
case "blue":
System.out.println("The favorite color is blue!");
break;
case "purple":
System.out.println("The favorite color is purple!");
break;
default:
System.out.println("I don't know what the favorite color is!");
}
}
}
switchTest.main(null);
De Morgan's Law
De Morgan's Law is a bit more mathematical in nature, dealing with set theory, so I'll try my best to describe it here.
De Morgan's Law basically explains how certain boolean statements can be rewritten as different expressions, yet essentially be the same. More specifically, it states that "The complement of the union of two sets is the same as the intersection of their complements" or "The complement of the intersection of two sets is the same as the union of their complements".
What the heck does that mean? An easier way to think about it would be like this:
- not (A or B) = (not A) and (not B)
- not (A and B) = (not A) or (not B)
In a more formalalized, mathematical way:
But even with all that text, it's still pretty confusing, right? So it's probably best to visualize it using an actual image. This one I found online pretty much summarizes it best:
The green shaded area is basically what's being covered in each equality. Both sides cover the same thing.
Finally, let's go through an example of how this would apply in code. Essentially, we'll use a few variables to make a whole bunch of complicated boolean statements that actually mean the same exact thing:
public class deMorgansLaw {
public static void main(String[] args) {
boolean a = false;
boolean b = true;
// These two statements are exactly equal according to De Morgan's law
// In this case, the only way to yield an output of true would be to have both a and b to be false (change it to check)
System.out.println(!(a || b));
System.out.println(!a && !b);
boolean c = true;
boolean d = false;
// These two statements are also exactly equal according to De Morgan's Law
// In this case, the only way to yield an output of false would be to have both c and d to be true (change it to check)
System.out.println(!(c && d));
System.out.println(!c || !d);
}
}
deMorgansLaw.main(null);
Conclusion
If statements, elseif statements, and else statmenet are the most basic components of conditionals, and thus are essential for any programmer to understand. They allow programs to run different lines of codes depending on given conditions, letting a program do various things when run multiple times.
Switch statements are similar to if statements, but are more tailored towards testing a single variable. However, they can be more efficient than branching many if and elseif statements.
De Morgan's Law describes the more mathematical complexities of if statements, explaining how certain conditions are actually equal to certain others. These can allow a programmer to rewrite their code or reexamine how it works.
Overall, knowing all of these things can be super helpful in one's journey as a programmer. To finish things off, I made a simple guessing number game below, applying conditionals to check victory, defeat, and more.
import java.util.*;
public class guessANumber {
public static void main(String[] args) {
Scanner input;
int guessNumber = 0;
int guesses = 5;
int secretNumber = (int)((Math.random() * 100) + 1);
while (true) {
input = new Scanner(System.in);
System.out.println("You have " + guesses + " guesses left!");
System.out.println("Guess a number (1-100): ");
guessNumber = input.nextInt();
input.close();
if (guessNumber == secretNumber) {
System.out.println("You guessed it! The secret number was " + secretNumber + "!");
break;
} else if (guessNumber > secretNumber) {
System.out.println(guessNumber + " is too high!");
guesses--;
} else if (guessNumber < secretNumber) {
System.out.println(guessNumber + " is too low!");
guesses--;
}
if (guesses <= 0) {
System.out.println("You lose! The secret number was " + secretNumber + "!");
break;
}
}
}
}
guessANumber.main(null);