if Statement

 

if Statement

Decision-making involves evaluating a condition to a Boolean value and making a decision based on it. The basic idea revolves around executing the block of code whose condition evaluates to true. Below are the types of decision-making statements:

  • if statement
  • if…..else statement
  • if…..else if statement
  • nested if statements
  • switch statement


if statement:

In a simple if statement, a block of code inside the if statement is only executed if the condition evaluates to true.

Syntax:

if (condition) {
    //block of code
}
e.g.1
public class JavaIf {
    public static void main(String[] args) {
        String name = "Maxon";
        int Roll = 25;
        if (name == "Maxon" && Roll == 25) {
            System.out.println("Details of Maxon.");
        }
    }
}

Output:

Details of Maxon.


Example: 

public class JavaIf {
    public static void main(String[] args) {
        String name = "Maxon";
        int Roll = 25;
        if (name == "Maxon" && Roll == 26) {
            System.out.println("Details of Maxon.");
        }
    }
}

Output:

Above code won’t give an output.