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 (5 > 2) { //the program decides whether this is true or not
    System.out.println("5 is greater than 2"); //if it is then this line is printed
  }
5 is greater than 2

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 = 20; //set variable
if (time < 18) { //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
}
Good evening.

What is an If-else If-else Statement?

int time = 22;
if (time < 10) { //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 evening."); //or else print this line
}
Good evening.

5 If-Else Statements

Each

if (5 > 3) {
    System.out.println("5 is greater than 3");
  }
  else if(5 < 3) {
    System.out.println("5 is less than 3");
  }
  else if (6 > 7) {
    System.out.println("6 is greater than 7");
  }
  else if (10 < 7) {
    System.out.println("10 is less than 7");
  }
  else {
    System.out.println("Invalid");
  }
5 is greater than 3

What is a Switch Case Program in Java?

The switch statement is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.

int x = 1; // the integer given in the variable is 20
String output;
switch (x) { 
    case 1:
        output = x + " > 0";
        break;
    case 2:
        output = x + " > 2";
        break;
    case 3:
        output = x + " > 3";
        break;
    case 4:
        output = x + " > 4";
        break;
}
System.out.println(output)
1 > 0

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"

boolean blue = true;
boolean red = true;

if (!(blue && red)){
    System.out.println("I do not like blue or red");
}
else{
    System.out.println("My favorite colors are blue and red");
}
My favorite colors are blue and red
if (!blue || !red){
    System.out.println("I do not like red or blue");
}
else{
    System.out.println("My favorite colors are red and blue");
}
My favorite colors are red and blue

Random Math Class

The random math class allows you to generate random numbers throughout your code. You can specify certain ranges where you would like the number to be in.

// Java program to demonstrate working
// of java.lang.Math.random() method
import java.lang.Math;
 
class Gfg1 {
 
    // driver code
    public static void main(String args[])
    {
        // Generate random number
        double rand = Math.random();
 
        // Output is different everytime this code is executed
        System.out.println("Random Number:" + rand);
    }
}

Compound Boolean Expressions

If two boolean values/expressions are combined with a logical and (&&) and the first expression is false, then the second expression won't be executed.

public class Test2
{
   public static void main(String[] args)
   {
     boolean walking = false;
     boolean carIsAvailable = false;
     if (walking || carIsAvailable)
     {
        System.out.println("You can go out");
     }
     else
     {
       System.out.println("No, you can't go out");
     }
   }
}

Truth Table

shows how the truth or falsity of a compound statement depends on the truth or falsity of the simple statements from which it's constructed

first case printed when x = 3 and y = 9

if (x > 0 && (y / x) == 3)
{
   System.out.println("first case");
}
else
{
   System.out.println("second case");
}

Comparing Numbers

To check two numbers for equality in Java, we can use the Equals() method as well as the == operator.

Integer val1 = new Integer(5);
Integer val2 = new Integer(5);
(val1 == val2)
public class Demo {
    public static void main( String args[] ) {
       Integer val1 = new Integer(5);
       Integer val2 = new Integer(5);
       Integer val3 = new Integer(10);
       System.out.println("Integer 1 = "+val1);
       System.out.println("Integer 2 = "+val2);
       System.out.println("Integer 3 = "+val3);
       System.out.println("val1 is equal to val2 = "+(val1 == val2));
       System.out.println("val2 is not equal to val3 = "+(val2 != val3));
    }
 }

Comparing Strings

Many ways to compare strings, one way is to use: String.equalsIgnoreCase()

// Java program to Compare two strings
// lexicographically
public class GFG {
	public static void main(String args[])
	{
		String string1 = new String("Geeksforgeeks");
		String string2 = new String("Practice");
		String string3 = new String("Geeks");
		String string4 = new String("Geeks");
		String string5 = new String("geeks");

		// Comparing for String 1 != String 2
		System.out.println("Comparing " + string1 + " and " + string2
						+ " : " + string1.equalsIgnoreCase(string2));

		// Comparing for String 3 = String 4
		System.out.println("Comparing " + string3 + " and " + string4
						+ " : " + string3.equalsIgnoreCase(string4));

		// Comparing for String 4 = String 5
		System.out.println("Comparing " + string4 + " and " + string5
						+ " : " + string4.equalsIgnoreCase(string5));

		// Comparing for String 1 != String 4
		System.out.println("Comparing " + string1 + " and " + string4
						+ " : " + string1.equalsIgnoreCase(string4));
	}
}

Comparing Objects

We can use == and != operators in order to compare objects.

assertThat(1 == 1).isTrue();
Integer a = new Integer(1);
assertThat(1 == a).isTrue();