If statement; if statements are used to specify a block of 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 (5 > 4) { //the program decides whether this is true or not
    System.out.println("5 is greater than 4"); //if it is then this line is printed
  }
5 is greater than 4

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 evening."); //if it is then print this line
}else {
  System.out.println("I hope you have an amazing day."); //if it is not then print this
}
I hope you have an amazing day.

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 afternoon."); //if it is then print this line
} else {
  System.out.println("Good night."); //or else print this line
}
Good afternoon.
if (1 > 2) {
    System.out.println("5 is greater than 4");
  }
  else if(3 < 1.5) {
    System.out.println("5 is less than 4");
  }
  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");
  }
7 is less than 8

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.
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)
Enter first number- 
|   String something = input.nextLine();
cannot find symbol
  symbol:   variable input

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.

  • || = "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");
}
I play baseball but not basketball
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");
}
I play baseball but not basketball
|   I play baseball but not basketball
';' expected