If else Program in Java

What is the if-else statement in Java?

If else is a conditional statement used in the execution of computer programs according to pre-established rules. If the condition is true, the if-else statement enables you to run a certain block of code; otherwise, it checks additional criteria. It is employed to set the rules or manage the flow of a program.
Mentioned below are the four types of ‘if else’ statements in the Java programming language.

  • If statement
  • If-else statement
  • If-else-if ladder
  • Nested if statement

If statement:

The ‘If statement’ is a rule that is performed when the condition is satisfied or true.

Syntax of If statement:

If (condition){
//code to be executed
}

Code Implementation:

class IfExample {  
public static void main(String[] args) {  
    //defining an 'age' variable  
    int age=20;  
    //checking the age  
    if(age>18){  
        System.out.print("Age is greater than 18");  
    }  
}  
}

Output: Age is greater than 18

if-else statement:

The if-else condition in Java is a group of guidelines or instructions that carry out a certain set of conditions. If the condition is met, it will display true according to the rules, and if it is not, it will output false.

Syntax of If-else statement:

If(condition){
// code if the condition is true
}else{
//code if the condition is false
}

Code Implementation:

class IfElseExample {  
public static void main(String[] args) {  
    //defining a variable  
    int number=13;  
    //Check if the number is divisible by 2 or not  
    if(number%2==0){  
        System.out.println("even number");  
    }else{  
        System.out.println("odd number");  
    }  
}  
}

Output: odd number

if-else-if Ladder Statement:

The collection of conditions in the if-else-if ladder statement is based on "Nested If Conditions." The phrases are executed top to bottom under the if-else ladder condition. When the condition is met, the corresponding statement is carried out and the balancing ladder rules are disregarded.

Syntax of if-else-if Ladder:

If(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
Else if(condition3){
//code to be executed if condition3 is true
}
…
Else{
//code to be executed if all the condition is false
}

Code Implementation:

class Test {
   public static void main(String args[]) {
      int x = 30;

      if( x == 10 ) {
         System.out.print("Value of X is 10");
      }else if( x == 20 ) {
         System.out.print("Value of X is 20");
      }else if( x == 30 ) {
         System.out.print("Value of X is 30");
      }else {
         System.out.print("This is else statement");
      }
   }
}

Output: Value of X is 30

Java Ternary Operator if-else Statement:

The ternary operator is a condition with three arguments: the first determines the comparison, the second determines the outcome of a true comparison, and the third does the opposite of the second, giving the outcome of a comparison of a false statement if the criteria make it easier to write an if else statement quickly.
For example, variable=Expression? Expression1: expression2
How does it work in the ternary operator?

  • If the expression is true, the expression is assigned to the variable
  • If the expression is false, the expression is assigned to the variable

Code Implementation:

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    
    // take input from users
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your marks: ");
    double marks = input.nextDouble();

    // ternary operator checks if
    // marks is greater than 40
    String result = (marks > 40) ? "pass" : "fail";

    System.out.println("You " + result + " the exam.");
    input.close();
  }
}

Input:75
Output: Enter your marks:
You pass the exam.

Java in Nested Ternary Operators if-else statement:

In Java, it is known as the nested ternary operator. In this case, one ternary operator may be used inside another ternary operator.
Nested if statement in Java
It is possible to use if-else statements inside of if-else statements in the Java nested if statement.

Syntax:

If(condition){
//code to be executed
If(condition){
//code to executed
}
}

Code Implementation:

class Main {
  public static void main(String[] args) {    
    // create a variable
    int n1 = 2, n2 = 9, n3 = -11;
    // nested ternary operator
    // to find the largest number
    int largest = (n1 >= n2) ? ((n1 >= n3) ? n1 : n3) : ((n2 >= n3) ? n2 : n3);
    System.out.println("Largest Number: " + largest);
  }
}

Output: Largest Number: 9

Conclusion
The numerous iterations of the Java if-construct, such as the basic if condition, the if-else condition, the nested if condition, the if-else-if ladder, and the ternary operator with an example of the if-else equivalent, have been covered in this lesson. Each of them has a suitable illustration, syntax explanation, and explanation of what it does and how it works.

A flowchart diagram and programming examples are used to describe each variant, which will aid in your comprehension of the subject.

Aside from certain other methods like switch statements and loops, which will be covered later, this is the most typical approach to execute a conditional check or decision-making in Java.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *