Java if-else statement with Examples

Decision Making in Java helps to write decision driven statements and execute a particular set of code based on certain conditions.

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax:

if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}

Working of if-else statements



  1. Control falls into the if block.
  2. The flow jumps to Condition.
  3. Condition is tested.
    1. If Condition yields true, goto Step 4.
    2. If Condition yields false, goto Step 5.
  4. The if-block or the body inside the if is executed.
  5. The else block or the body inside the else is executed.
  6. Flow exits the if-else block.

Flowchart if-else:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate if-else statement
  
class IfElseDemo {
    public static void main(String args[])
    {
        int i = 20;
  
        if (i < 15)
            System.out.println("i is smaller than 15");
        else
            System.out.println("i is greater than 15");
  
        System.out.println("Outside if-else block");
    }
}

chevron_right


Output:

i is greater than 15
Outside if-else block

Dry-Running Example 1:

1. Program starts.
2. i is initialized to 20.
3. if-condition is checked. 20<15, yields false.
4. flow enters the else block.
  4.a) "i is greater than 15" is printed
5. "Outside if-else block" is printed.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate if-else statement
  
class IfElseDemo {
    public static void main(String args[])
    {
        String str = "geeksforgeeks";
  
        if (str == "geeks")
            System.out.println("Hello geek");
        else
            System.out.println("Welcome to GeeksforGeeks");
    }
}

chevron_right


Output:

Welcome to GeeksforGeeks

Related Articles:

  1. Decision Making in Java
  2. Java if statement with Examples
  3. Java if-else-if ladder with Examples
  4. Switch Statement in Java
  5. Break statement in Java
  6. return keyword in Java

Don't stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.