if-else Program in Java
Dry-Run of if-else statements
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.
Below is the implementation of the above statements:
// 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");
}
}
Output
i is greater than 15 Outside if-else block
Java if-else
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. In this article, we will learn about Java if-else.