- A conditional statement lets us choose which
statement will be executed next
• Therefore they are sometimes called selection
statements
• Conditional statements give us the power to make
basic decisions
• The Java conditional statements are the:
# Boolean Notes– if statement – if-else statement – switch statement
- The Boolean logical operators are : | , & , ^ , ! , || , && , == , != .
- Java supplies a primitive data type called Boolean, instances of which can take the value true or false only, and have the default value false.
- The major use of Boolean facilities is to implement the expressions which control if decisions and while loops.
boolean isSoccerFan = true;//If you set a variable along with the word "boolean" to true,
//this allows for the if statements to print the correct statements
if(isSoccerFan){
System.out.println("Watches 1 Soccer Game Every Weekend");
} else {
System.out.println("Watches 0 Soccer Games");
}
int weight = 200;
//Weights Are Rounded to Nearest 10(in pounds) between 130-250lbs
if(weight==130){
System.out.println("Your Are Very Light");
}
else if(weight==140){
System.out.println("Your Are Quite Light");
}
else if(weight==150){
System.out.println("Your Are A Little Bit On The Lighter Side");
}
else if(weight==160){
System.out.println("You Are At A Good Weight");
}
else if(weight==170){
System.out.println("You Are At A Good Weight");
}
else if(weight==180){
System.out.println("You Are At A Strong Weight");
}
else if(weight==190){
System.out.println("You Are Very Close To Obese");
}
else if(weight==200){
System.out.println("You Are Just Obese");
}
else if(weight==210){
System.out.println("You Are Slightly Obese");
}
else if(weight==220){
System.out.println("You Are Nearly Moderately Obese");
}
else if(weight==230){
System.out.println("You Are Moderately Obese");
}
else if(weight==240){
System.out.println("You Are Nearly Very Obese");
}
else if(weight==250){
System.out.println("You Are Very Obese");
}
Switch Statement Notes
- A switch statement allows a variable to be tested for equality against a list of values.
- Each value is called a case, and the variable being switched on is checked for each case.
- The variable used in a switch statement can only be integers, convirtable integers (byte, short, char), strings and enums.
- You can have any number of case statements within a switch.
- End of each statement is followed with a comparable value and a colon.
- The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
int weight = 200;
//Weights Are Rounded to Nearest 10(in pounds) between 130-250lbs. Intended for indivduals between the height of 5'8 to 5'11
switch(weight){
case 130:
System.out.println("You are Very Light");
break;
case 140:
System.out.println("Your Are Very Light");
break;
case 150:
System.out.println("Your Are A Little Bit On The Lighter Side");
break;
case 160:
System.out.println("You Are At A Good Weight");
break;
case 170:
System.out.println("You Are At A Good Weight");
break;
case 180:
System.out.println("You Are At A Strong Weight");
break;
case 190:
System.out.println("You Are Very Close To Obese");
break;
case 200:
System.out.println("You Are Just Obese");
break;
case 210:
System.out.println("You Are Slightly Obese");
break;
case 220:
System.out.println("You Are Nearly Moderately Obese");
break;
case 230:
System.out.println("You Are Moderately Obese");
break;
case 240:
System.out.println("You Are Nearly Very Obese");
break;
case 250:
System.out.println("You Are Very Obese");
break;
default:
System.out.println("Invalid Input")
break;
}
DeMorgan's Law Notes
- DeMorgan’s laws were developed by Augustus De Morgan in the 1800s.
- They show how to handle the negation of a complex conditional, which is a conditional statement with more than one condition joined by an and (&&) or or (||), such as (x < 3) && (y > 2).
- Applying DeMorgan’s Laws to !(x < 3 && y > 2) yields !(x < 3) || !(y > 2) which means that this complex conditional will be true when (x >= 3 || y <= 2).
- The negation modifies each conditional as shown below.
- < becomes >=
- "> becomes <="
- == becomes !=
- <= becomes >
- ">= becomes <"
- != becomes ==
- not (a and b) is the same as (not a) or (not b). In Java this is written as !(a && b) == !a || !b
- not (a or b) is the same as (not a) and (not b). In Java this is written as !(a || b) == !a && !b
public class DeMorgansLawExample {
public static boolean exVarOne = true;
public static boolean exVarTwo = false;
public static void main(String[] args) {
// The two boolean expressions below are equal
if (!(exVarOne && exVarTwo)) {
System.out.println("The boolean expression is true");
} else {
System.out.println("The boolean expression is false");
}
if (!(exVarOne || exVarTwo)) {
System.out.println("The boolean expression is true");
} else {
System.out.println("The boolean expression is false");
}
}
}
DeMorgansLawExample.main(null)