Java Conditional Statements
Java Conditional Statements
Java Programming
(Java Conditional Statements)
20/12/2024
CONTENTS
If
if-else
nested if
Ladder
switch
Introduction
In Java, decision-making enables us to write decision-driven
statements and execute code based on given conditions. Java if
statement executes a block of a statement if a condition is true,
and if a condition is false, it will not execute the code.
1. if statement
2. if-else statement
4. if-else-if ladder
5. switch statement
There are five types of if-else statements in Java, which are
as follows:
1. if statement: The if statement is the most basic conditional
statement in Java. It evaluates a boolean expression and executes a
block of code if the condition is true.
Syntax of if Statement
if (condition) {
// code to be executed if condition is true
}
if (num > 0) {
System.out.println("Number is positive");
Explanation:
if (condition) {
// code to be executed if condition is true
}
else {
// code to be executed if condition is false
}
Explanation:
In this example, the if statement checks if the value of the variable
num is greater than 0. If the condition is true, it executes the code
block within the if statement, which prints "Number is positive" to
the console.
If the condition is false, it executes the code block within the else
statement, which prints "Number is non-positive" to the console.
Java Nested if statement
Nested if statement in Java represents the if-else statement within
another if-else statement. The inner if block executes only when the
outer if condition is true.
Syntax of Nested if Statement
if (condition1) {
// code to be executed if condition1 is true
if (condition2) {
// code to be executed if both condition1 and condition2 are
true
}
else {
// code to be executed if condition1 is true and condition2
is false
}
}
else {
In the nested if statement, the outer if statement is
evaluated first. If its condition is true, the code block within
its body is executed.
Example :
} else if (condition2) {
} else if (condition3) {
} else {
}
The if-else-if ladder starts with an initial if statement,
followed by one or more else if statements.
If none of the conditions are true, the code within the else
block is executed.
Example of if-else-if in Java
Example :
int num = 5;
if (num > 0) {
System.out.println("Number is positive");
}
else if (num < 0) {
System.out.println("Number is negative");
}
else {
System.out.println("Number is zero");
}
Explanation:
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
default:
dayName = "Invalid day";
break;
}
System.out.println("The day is: " + dayName);
Explanation:
if (num % 2 == 0) {
else {
num1);
else {
num2);
In this example, the program compares the values of num1
and num2. If num1 is greater than num2, it prints "The
}
maximum number is: 10." Otherwise, it prints "The maximum
3. Checking if a year is a leap year