Boolean Expressions and Control Structures
Lesson on booleans and control structures
Boolean: a primitive data type that is either true or false
boolean computersAreCool = true;
boolean paperIsCool = false;
Boolean Expression: an expression that evaluates as either true or false
1==1 // is true, because 1 will always be equal to 1
a < 1 // will sometimes be true, because not all values of a will be less than 1
“paper” == “good” // is false, because the string “paper” is not equal to the string “good”
We can use boolean expressions in control structures to run programs when certain conditions are met.
if
statements will run a code block if a particular condition is true.
Structure:
if(condition) {
// code will run when condition is true
}
// an if statement will run when the condition is true
if(true) {
System.out.println("this will print");
}
if(1==1) {
System.out.println("this will print");
}
// an if statement will not run if the condition is false
if(false) {
System.out.println("this will not print");
}
if("paper" == "good") {
System.out.println("this will not print");
}
A single if
statement only takes one condition, but what if we wanted to specify more conditions? We can add else if
and else
statements to accomplish this.
Structure:
if(condition) {
// code to execute when condition is met
} else if (condition 2) {
// code to execute when condition is false but condition 2 is true
} else {
// code to execute when condition and condition 2 are false
}
int time = 8; // 24-hour clock, 8 AM
if(time < 8 ) { // will run if it is before 8 AM
System.out.println("School will start in a few hours!");
} else if ( time >= 8 && time < 16) { // will run betwee 8 AM and 4 PM
System.out.println("You should be in school!");
} else { // will run from 4 PM to 12 AM
System.out.println("You are at home!");
}
switch
statements check the value of a condition and run code blocks for different scenarios.
Structure:
switch(condition) {
case value1:
// code
break;
case value2:
// code
break;
default;
// code that should run for unspecified case
break;
}
switch
statements are especially useful for creating menus.
int menuOption = 0;
// print menu for user to see options they can choose from
System.out.println("---- Menu Options ---");
System.out.println("0: print hello world");
System.out.println("1: print goodbye world\n");
switch(menuOption) { // check the value of menuOption
case 0: // what to run when menuOption=0
System.out.println("hello world");
break;
case 1: // what to run when menuOption=1
System.out.println("goodbye world");
break;
default: // default code to run
System.out.println("menu options not found!");
break;
}
The two programs below accomplish the same thing: given an object, the program will rate it as "cool" or "uncool". The first uses if-else if-else
, and the other uses switch-case
.
if statements:
String object = "computer"; // object is "computer"
// the if statement will print different statements depending on the value of object
if(object == "computer") {
System.out.println("cool");
} else if (object == "keyboard") {
System.out.println("cool");
} else if (object == "phone") {
System.out.println("cool");
} else if (object == "paper") {
System.out.println("uncool");
} else if(object == "pencil") {
System.out.println("uncool");
} else {
System.out.println("object not found!");
}
// since object = "computer" , this program should print "cool"
switch-case:
String object = "computer"; // object is "computer"
// the switch statement will test different values of object and print a result depending on the value of object
switch(object) {
case "computer":
System.out.println("cool");
break;
case "keyboard":
System.out.println("cool");
break;
case "phone":
System.out.println("cool");
break;
case "paper":
System.out.println("uncool");
break;
case "pencil":
System.out.println("uncool");
break;
default:
System.out.println("object not found!");
break;
}
// since object = "computer", this program should print "cool"
DeMorgan's Laws show how to deal with the negation of a conditional statements. When !
is applied:
true
will becomefalse
false
will becometrue
and
(&&) will becomeor
(||)
Examples:
Expression | Applying DeMorgan's Law | result |
---|---|---|
!true | opposite of true --> false | false |
!(true || false) | false && true | false |
!((false && true) || (!false && true)) | (true || false) && (false || true) --> true && true | true |
// DeMorgan's law practice
if(!true) {
System.out.println("!true = false, so this statement should not print");
}
if(!((true && false) || (false && false))) {
// using DeMorgan's law, this statement is equal to (false || true) && (true || true), which is true
System.out.println("!((true && false) || (false && false)) is true, so this should print!");
}
if(!(false || true)) {
// using DeMorgan's law, this statement is equal to true && false, which is false
System.out.println("this should not print!");
}