Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

If else Program in Java

Last Updated on August 29, 2023 by Mayank Dham

In the realm of programming, conditional statements play a pivotal role in making decisions and controlling the flow of execution. Among these, the "if-else" statement is a fundamental construct that enables programmers to create dynamic and responsive code. This article delves into the concept of the "if-else" statement in Java programming, its syntax, usage, practical examples, and its significance in building versatile applications.

What is the if-else statement in Java?

The "if-else" statement is a programming construct that facilitates the execution of computer programs based on predefined conditions. When the specified condition evaluates to true, the "if-else" statement allows the execution of a designated code block. If the condition is false, the statement evaluates further criteria. This construct is utilized to establish rules and regulate the program’s flow.

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 in Java:

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 if-else statement in Java is a cornerstone of programming logic, offering developers the ability to make informed decisions and create dynamic, responsive applications. By understanding the syntax, usage, and practical applications of the "if-else" statement in Java, programmers can harness its power to create code that adapts and responds intelligently to a variety of conditions and scenarios.

FAQs related to If else Program in Java

1. Can I have multiple "else if" conditions following an "if" statement?
Yes, you can include multiple "else if" conditions to handle different cases and conditions in a sequential manner.

2. Are nested "if-else" statements common in programming?
Yes, nested "if-else" statements are used when you need to evaluate multiple conditions in a hierarchical manner, allowing for more complex decision-making.

3. What are some practical applications of the "if-else" statement?
"if-else" statements are used for user input validation, error handling, making decisions in games, creating menu options, and controlling program flow based on various conditions.

4. How do "if-else" statements contribute to making code responsive?
"if-else" statements allow programs to react differently to varying inputs or situations, enhancing user experience and tailoring program behavior.

5. Can I use logical operators within the condition of an "if" statement?
Yes, logical operators like "&&" (and) and "||" (or) can be used to combine multiple conditions within an "if" statement.

6. Is there a limit to the number of "else if" conditions that can be used?
There isn’t a strict limit, but having too many "else if" conditions can make code harder to read and maintain. It’s best to keep code concise and organized.

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 *