Introduction Decision making is an essential aspect of programming, just as it is in real life. In programming, certain situations require executing a block of code based on a specific condition. Programming languages use control statements to direct the execution flow of a program based on specific conditions, allowing the program's flow of execution to advance and branch based on the program's state changes. Java provides various selection statements for decision-making purposes, including if, if-else, nested-if, if-else-if, switch-case, and jump statements. This article discusses each of these statements in detail, along with their syntax, examples, time complexity, and auxiliary space.
Java's Selection Statements
- If statement The if statement is the most straightforward decision-making statement in Java. It allows a programmer to execute a certain statement or block of statements if a specific condition is fulfilled. The if statement accepts Boolean values; if the value is true, it executes the block of statements under it. If we do not provide the curly braces '{' and '}' after if(condition), the if statement will consider only the immediate one statement to be inside its block. The following is the syntax of the if statement in Java:
Syntax: if(condition) { // Statements to execute if condition is true }
Example:
csharp// Java program to illustrate if statement
class IfDemo {
public static void main(String args[])
{
int i = 10;
if (i > 15)
System.out.println("10 is less than 15");
// This statement will be executed
// as if considers one statement by default
System.out.println("I am Not in if");
}
}
Output: I am Not in if
Time Complexity: O(1) Auxiliary Space: O(1)
- If-else statement The if-else statement executes a block of code if the condition specified in the if statement is true. However, if the condition is false, the statement following the else block executes. We can use the else statement with the if statement to execute a block of code when the condition is false. The following is the syntax of the if-else statement in Java:
Syntax: if (condition) { // Executes this block if condition is true } else { // Executes this block if condition is false }
Example:
csharp// Java program to illustrate if-else statement
class IfElseDemo {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output: i is smaller than 15
Time Complexity: O(1) Auxiliary Space: O(1)
- Nested-if statement The nested-if statement is an if statement that is the target of another if or else. In Java, we can nest if statements within if statements, meaning we can place an if statement inside another if statement. If the first if statement's condition is true, the code within its block executes, and the program moves to the nested if statement if present. The following is the syntax of the nested-if statement in Java:
Syntax: if (condition1) { // Executes when condition1 is true if (condition2) { // Executes when condition2 is true } }
Example:
typescript// Java program to illustrate nested-if statement
class NestedIfDemo {
public static void main(String args[])
{
int i = 10;
if (i == 10 || i<15) {
// First if statement
if (i < 20)
{ System.out.println("i is less than 20");
}
// Second if statement
if (i < 12) {
System.out.println("i is less than 12 too");
} else {
System.out.println("i is greater than or equal to 12");
}
}
}
}
// Output:
// i is less than 20
// i is less than 12 too
In this program, we have used nested-if statements. First, we check whether the value of i is equal to 10 or less than 15 using the OR operator in the if statement. If the condition is true, then we move to the nested-if statements.
In the nested-if statements, we check whether i is less than 20. If the condition is true, then we print "i is less than 20". After that, we check whether i is less than 12. If it is true, we print "i is less than 12 too". Otherwise, we print "i is greater than or equal to 12".
Since the value of i is 10, both conditions in the nested-if statements are true. Therefore, the output of the program is "i is less than 20" and "i is less than 12 too".