Java if-else statement with Examples - GeeksforGeeks

30 Mar, 2022
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 the 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  
Control falls into the if block.The flow jumps to Condition.Condition is tested. If Condition yields true, go to Step 4.If Condition yields false, go to Step 5.The if-block or the body inside the if is executed.The else block or the body inside the else is executed.Flow exits the if-else block.
Control falls into the if block.
The flow jumps to Condition.
Condition is tested. If Condition yields true, go to Step 4.If Condition yields false, go to Step 5.
If Condition yields true, go to Step 4.If Condition yields false, go to Step 5.
If Condition yields true, go to Step 4.
If Condition yields false, go to Step 5.
The if-block or the body inside the if is executed.
The else block or the body inside the else is executed.
Flow exits the if-else block.
Flowchart if-else: 
Example 1: 
Java
// 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");    }}
i is greater than 15
Outside if-else block
Time Complexity: O(1)
Auxiliary Space: O(1)
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:  
Java
// 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");    }}
Welcome to GeeksforGeeks
Time Complexity: O(1)
Auxiliary Space: O(1)
YouTubeGeeksforGeeks507K subscribersIf-Else | Java Programming Language | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 11:23•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=LLQVi7kCuTA" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Related Articles: 
Decision Making in JavaJava if statement with ExamplesJava if-else-if ladder with ExamplesSwitch Statement in JavaBreak statement in Javareturn keyword in Java
Decision Making in Java
Java if statement with Examples
Java if-else-if ladder with Examples
Switch Statement in Java
Break statement in Java
return keyword in Java
nishkarshgandhi
rishavnitro
java-basics
Java
School Programming
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Split() String method in Java with examples
Arrays.sort() in Java with examples
Reverse a string in Java
Stream In Java
How to iterate any Map in Java
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects