If-Statements and Booleans
If-Statements and Booleans
What is an if statement? An if statement is used to specify a block of Java code to be executed if a condition is true.
if-else
- Uses keyword “if”
- Checks if statement is true
- If it is, performs certain task
- if not, performs else command
if-else-else Use the else if statement to specify a new condition if the first condition is false.
Conditionals: allows the computer to make decisions based on if a statement is true or false
- If
- Else
- Else If
- Booleans
if (3.14 > 1.5) { //the program decides whether this is true or not
System.out.println("3.14 is greater than 1.5"); //if it is then this line is printed
}
What is an if/else statement? The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
int time = 12; //set variable
if (time < 12) { //the program decides whether this is true or not
System.out.println("Good day."); //if it is then print this line
} else {
System.out.println("Good evening."); //if it is not then print this
}
What is an If-else-else Statement?
int time = 12;
if (time < 12) { //the program decides whether this is true or not
System.out.println("Good morning."); //if it is then print this line
} else if (time < 20) { //if it is not true, then check if this is true
System.out.println("Good day."); //if it is then print this line
} else {
System.out.println("Good night."); //or else print this line
}
if (1 > 2) {
System.out.println("3.14 is greater than 1.5");
}
else if(3 < 1.5) {
System.out.println("3.14 is less than 1.5");
}
else if (7 > 8) {
System.out.println("8 is greater than 7");
}
else if (6 < 8) {
System.out.println("7 is less than 8");
}
else {
System.out.println("Invalid");
}
What is a Switch Case Program in Java? Java switch statement executes one statement from multiple conditions.
- The switch statement evaluates an expression, matching the expression's value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered. The default clause of a switch statement will be jumped to if no case matches the expression's value.
Scanner input = new Scanner(System.in);
System.out.print("Enter first number- ");
String something = input.nextLine(); // Read user input
System.out.println(something);
Scanner sc= new Scanner(System.in);
int x= sc.nextInt();
String output;
switch (x) {
case 1:
output = x + " > 0";
break;
case 2:
output = x + " > 8";
break;
case 3:
output = x + " > 3";
break;
case 4:
output = x + " > 6";
break;
}
System.out.println(output)
De Morgan's Law The complement of the union of two sets is the intersection of their complements and the complement of the intersection of two sets is the union of their complements.
|| means "or" and the && means "and" ! = not
boolean playBasketball = false;
boolean playBaseball = true;
if (!(playBaseball && playBasketball)){
System.out.println("I play baseball but not basketball");
}
else{
System.out.println("I play basketball but not baseball");
}
boolean playBasketball = false;
boolean playBaseball = true;
if (!playBaseball||!playBasketball){
System.out.println("I play basketball but not baseball");
}
else{
System.out.println("I play baseball but not basketball");
}