Tuesday, June 18, 2019

if else Statement in Java With Examples

To control the execution flow Java programming language provides two types of conditional statements if-else and switch-case statement. In this post we'll talk about Java if and if-else statements in detail along with usage examples.

if-else statement in Java

The Java if statement is used to test a condition and take the execution path based on whether the condition is true or false. There are many combinations in which if statement can be used in Java.

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

if statement in Java

Syntax of the Java if statement is as follows-

if(condition){
    //statement(s)
}

Here condition is a boolean expression that evaluates to either true or false. If the condition evaluates to true then the block of code enclosed in curly braces is executed. If the condition evaluates to false then the if block is skipped.

If there is only a single statement with in the if condition then the curly braces are optional.

Java if statement flow

if condition java

Java if statement examples

1- Testing a condition to check if passed number is greater than 5 or not.

public class IfDemo {

    public static void main(String[] args) {
        int i = 10;
        if(i > 5) {
            System.out.println("Value of i is greater than 5");
        }
        System.out.println("After if statement");
    }
}

Output

Value of i is greater than 5
After if statement

2- You can also use conditional operators like Conditional-AND (&&) and Conditional-OR (||) to create a condition.

public class IfDemo {

    public static void main(String[] args) {
        int i = 10;
        String test = "Hello";
        if(i > 5 && test.equals("Hello"))
            System.out.println("Inside if");
        System.out.println("After if statement");
    }
}

Output

Inside if
After if statement

Java if-else statement

Syntax of the Java if-else statement is as follows-

if(condition){
    // if block
}else{
    // else block
}

Here condition is a boolean expression that evaluates to either true or false. If the condition evaluates to true then if block is executed. If the condition evaluates to false then the else block is executed.

Java if-else statement flow

if else Java

Java if-else statement examples

public class IfDemo {

    public static void main(String[] args) {
        int i = 10;
        String test = "Hello";
        if(i > 20 && test.equals("Hello")) {
            System.out.println("Inside if");
        }else {
            System.out.println("Inside else");
        }
        System.out.println("After if-else statement");
    }
}

Output

Inside else
After if-else statement

In the example condition fails therefore else block is executed.

Java if-else-if ladder

You can also have an if statement followed by one or more else-if statements and an optional else statement at the end. Each if and else-if statement has a condition and a particular block is executed if the condition associated with that block evaluates to true. If none of the conditions evaluate to true then the else block (if present) is executed.

Syntax of Java if-else-if syntax is as follows-

if(condition1){
    statement(s);
}else if(condition2){
    statement(s);
}else if(condition3){
    statement(s);
}
.
.
.
else{
    statement(s);
}

Java if-else-if examples

Suppose you have requirement to add 10% to amount if amount is greater than 5000.
Add 15% if amount is more than 3000 but less than or equal to 5000.
Add 20% if amount is more than 1000 but less than or equal to 3000.
Otherwise add 25% to the amount.

public class IfDemo {

    public static void main(String[] args) {
        int amount = 5000;
        if(amount > 5000) {
            // add 10%
            amount = amount + (amount*10/100);
        }else if (amount > 3000 && amount <= 5000) {
            // add 15%
            amount = amount + (amount*15/100);
        }else if (amount > 1000 && amount <= 3000) {
            // add 20%
            amount = amount + (amount*20/100);
        }else {
            //add 25%
            amount = amount + (amount*25/100);
        }
        System.out.println("Amount is- " + amount);
    }
}

Output

Amount is- 5750

Java nested if-else statements

It is possible to have an if-else statement inside a if-else statement in Java. It is known as a nested if-else statement.

public class IfDemo {

    public static void main(String[] args) {
        int amount = 8000;
        if(amount > 5000) {
            if(amount > 7000 && amount <=10000) {
                amount = amount + (amount*10/100);
            }else {
                amount = amount + (amount*5/100);
            }    
        }else {
            if (amount > 3000 && amount <= 5000) {
                amount = amount + (amount*15/100);
            }else {
                amount = amount + (amount*20/100);
            }
        }
        System.out.println("Amount is- " + amount);
    }
}

Output

Amount is- 8800

That's all for this topic if-else Statement in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Java for Loop With Examples
  2. Equality And Relational Operators in Java
  3. String in Java Tutorial
  4. static Block in Java
  5. Marker Interface in Java

You may also like-

  1. finally Block in Java Exception Handling
  2. BigDecimal in Java
  3. How to Loop or Iterate an Arraylist in Java
  4. Java Collections Interview Questions And Answers
  5. Producer-Consumer Java Program Using ArrayBlockingQueue
  6. How to Convert String to Date in Java
  7. Operator Overloading in Python
  8. Data Access in Spring Framework

No comments:

Post a Comment