Learn Java , Selenium

Conditional Statement in Java – if else if Statement –

				
					package practice;

public class IfElseif {

	public static void main(String[] args) {
		int time = 15;
		if (time < 10) {
		  System.out.println("Good morning.");
		} else if (time < 18) {
		  System.out.println("Good day.");
		} else {
		  System.out.println("Good evening.");
		}

	}

}

				
			

💡 Explanation of if - else if - else in Java

The if - else if - else structure allows your program to make decisions based on conditions. It checks each condition one by one from top to bottom, and executes only the first true block. If no condition is true, the else block is executed (if present).

Output – 

Since time = 15, which is less than 18 but greater than 10, the output will be:

Good day.

Scroll to Top