Conditions are an integral part of programming as they help in controlling the flow of the program.
In Java, conditions are used to execute a certain block of code only if a certain condition is satisfied.
This article will guide you through various conditional statements in Java and how to use them.
If Statement
The if statement is the most basic conditional statement in Java.
It checks if a particular condition is true, and if it is, it executes the code inside the block.
The basic syntax of the if statement is as follows:
if (condition) {
// code to be executed if condition is true
}
For example, consider the following code to find if a number is even or odd:
int num = 4;
if (num % 2 == 0) {
System.out.println(num + " is even");
}
The output of the code will be "4 is even".
If-Else Statement
The if-else statement is used when you need to execute different blocks of code based on different conditions.
In this statement, if the condition in the if statement is true, the code inside the if block will be executed, and if the condition is false, the code inside the else block will be executed.
The basic syntax of the if-else statement is as follows:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
For example, consider the following code to find if a number is positive, negative or zero:
int num = 0;
if (num > 0) {
System.out.println(num + " is positive");
} else if (num < 0) {
System.out.println(num + " is negative");
} else {
System.out.println(num + " is zero");
}
The output of the code will be "0 is zero".
Switch Statement
The switch statement is used to execute a block of code based on the value of a variable.
It is useful when you need to perform different actions based on multiple cases.
The basic syntax of the switch statement is as follows:
switch (expression) {
case value1:
// code to be executed if expression matches value1
break;
case value2:
// code to be executed if expression matches value2
break;
...
default:
// code to be executed if expression does not match any case
}
For example, consider the following code to find the name of a day based on a number:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
...
default:
System.out.println("Invalid day");
}
The output of the code will be "Wednesday".
Ternary Operator
The ternary operator is a shorthand way of writing an if-else statement.
It is used to return a value based on a condition.
The basic syntax of the ternary operator is as follows:
(condition) ? expression1 : expression2;
The expression before the ? is the condition that is being checked, and the expression after the ? is executed if the condition is true.
The expression after the : is executed if the condition is false.
For example, consider the following code to find the minimum of two numbers:
int num1 = 10;
int num2 = 20;
int min = (num1 < num2) ? num1 : num2;
System.out.println("Minimum: " + min);
The output of the code will be "Minimum: 10".
Conclusion
Conditions are a vital part of programming and are used to control the flow of a program.
Java provides multiple conditional statements such as if, if-else, switch, and the ternary operator to help you write complex logic in a clean and concise manner.
By understanding these statements, you can write more efficient and effective code.