Conditions in Java

Conditions In Java

You need to execute a certain code when a certain condition is true. In such situations, you can use conditions in Java. E.g., if condition, if-else condition, switch case, etc. E.g., if a person age is greater than 18, person is voter.

If Condition

It is used when you have to execute statements when a certain condition is true.

Syntax

if(condition){
statements;
}

If-Else Condition

It is used when you have to execute statements when a certain condition is true & execute statements when a certain condition is false.

Syntax

if(condition){
statements;
}else{
statements;
}

If-Else-If Condition

When you have multiple if conditions, then you can use if-else if.

Syntax

if(condition){
statements;
}else if(condition){
statements;
}else if(condition){
statements;
}
Info

Note: You can add multiple else if and, at last, use else condition also.

Example Of If-Else Condition

This program finds the person is a voter or not according to age.

public class Main {
    public static void main(String[] args) {
        int age = 12;
        if(age <=17){
            System.out.println("You are not voter.");
        }else{
            System.out.println("You are voter.");
        }
    }
}
Show Output

Example Of If-Else-If Condition

This program prints the month name according to the number. For e.g 1 for jan and 12 for december.

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        // Creating Scanner Object
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter No of months");
        int noOfMonth = sc.nextInt();
        // Check the no of month
        if(noOfMonth == 1){
            System.out.println("The month is jan");
        }else if(noOfMonth ==2){
            System.out.println("The month is feb");
        }else if(noOfMonth ==3){
            System.out.println("The month is march");
        }else if(noOfMonth ==4){
            System.out.println("The month is april");
        }else if(noOfMonth ==5){
            System.out.println("The month is may");
        }else if(noOfMonth ==6){
            System.out.println("The month is june");
        }else if(noOfMonth ==7){
            System.out.println("The month is july");
        }else if(noOfMonth ==8){
            System.out.println("The month is aug");
        }else if(noOfMonth ==9){
            System.out.println("The month is sep");
        }else if(noOfMonth ==10){
            System.out.println("The month is oct");
        }else if(noOfMonth ==11){
            System.out.println("The month is nov");
        }else if(noOfMonth ==12){
            System.out.println("The month is dec");
        }else{
            System.out.println("Invalid option given.");
        }
    }
}
Show Output

Find the greatest number among 3 numbers.

This program find greatest number among 3 numbers.

public class Main {
    public static void main(String[] args) {
        int num1 = 1000;
        int num2 = 200;
        int num3 = 150;

        if(num1 > num2  && num1 > num3){
            System.out.println("Num 1 is greater: i.e "+num1);
        }
        if(num2 > num1 && num2 > num3){
            System.out.println("Num2 is greater: i.e "+num2);
        }
        if(num3 > num1 && num3 > num2){
            System.out.println("Num3 is greater: i.e "+num3);
        }
    }
}
Show Output