Complete information on Decision-Making statements in Java

In programming, control statements are used to control the flow of execution of a program. Java, like any other programming language, provides various control statements to control the flow of execution of a program. These control statements are divided into two categories: decision-making statements and loop statements.

Decision-Making statements
Decision-making statements are used to make decisions in a program based on certain conditions. There are several decision-making statements in Java, such as the
- if statement,
- if-else statement,
- if-else-if ladder, and
- switch statement.
if statement
The if statement is used to execute a block of code only if a certain condition is true. The syntax for an if statement is as follows:
// code to be executed if the condition is true
}
For example, the following program uses an if statement to check if a number is even or odd:
if (num % 2 == 0) {
System.out.println(num + ” is even.”);
}
if-else statement
The if-else statement is used to execute a block of code if a certain condition is true, and another block of code if the condition is false. The syntax for an if-else statement is as follows:
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
For example, the following program uses an if-else statement to check if a number is positive or negative:
if (num > 0) {
System.out.println(num + ” is positive.”);
} else {
System.out.println(num + ” is negative.”);
}
You may be interested:
if-else-if ladder
The if-else-if ladder is used to check multiple conditions. It is used when we have to check more than two conditions. The syntax for an if-else-if ladder is as follows:
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else if (condition3) {
// code to be executed if condition1 and condition2 are false and condition3 is true
} …
For example, the following program uses an if-else-if ladder to check the grades of a student:
if (marks >= 90) {
System.out.println(“Grade: A”);
} else if (marks >= 75) {
System.out.println(“Grade: B”);
} else if (marks >= 60) {
System.out.println(“Grade: C”);
} else {
System.out.println(“Grade: D”);
}
Nested if-statement
A nested if statement is an if statement that is the target of another if or else. The syntax for a nested if statement is as follows:
// code to be executed if condition1 is true
if (condition2) {
// code to be executed if condition1 and condition2 are true
}
}
For example, the following program uses a nested if statement to check if a number is divisible by both 3 and 5:
if (num % 3 == 0) {
if (num % 5 == 0) {
System.out.println(num + ” is divisible by both 3 and 5.”);
}
}
switch statement
The switch statement is used to perform different actions based on different conditions. The syntax for a switch statement is as follows:
case value1:
// code to be executed if expression is equal to value1
break;
case value2:
// code to be executed if expression is equal to value2
break;
…
default:
// code to be executed if expression does not match any of the cases
}
For example, the following program uses a switch statement to check the day of the week:
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.”);
}
Nested if-statement
A nested if statement is an if statement that is the target of another if or else. The syntax for a nested if statement is as follows:
if (condition1) {
// code to be executed if condition1 is true
if (condition2) {
// code to be executed if condition1 and condition2 are true
}
}
For example, the following program uses a nested if statement to check if a number is divisible by both 3 and 5:
int num = 15;
if (num % 3 == 0) {
if (num % 5 == 0) {
System.out.println(num + " is divisible by both 3 and 5.");
}
}
Loop statements
Loop statements are used to execute a block of code repeatedly until a certain condition is met. There are several loop statements in Java, such as the
- do-while loop,
- while loop,
- for loop, and
- for-each loop.
do-while loop
The do-while loop is used to execute a block of code repeatedly until a certain condition is met. The syntax for a do-while loop is as follows:
// code to be executed
} while (condition);
For example, the following program uses a do-while loop to print the numbers from 1 to 10:
do {
System.out.println(i);
i++;
} while (i <= 10);
while loop
The while loop is used to execute a block of code repeatedly until a certain condition is met. The syntax for a while loop is as follows:
// code to be executed
}
For example, the following program uses a while loop to print the numbers from 1 to 10:
while (i <= 10) {
System.out.println(i);
i++;
}
for loop
The for loop is used to execute a block of code a specified number of times. The syntax for a for loop is as follows:
// code to be executed
}
For example, the following program uses a for loop to print the numbers from 1 to 10:
System.out.println(i);
}
for-each loop
The for-each loop is used to iterate through an array or a collection. The syntax for a for-each loop is as follows:
// code to be executed
}
For example, the following program uses a for-each loop to print the elements of an array:
for (int num : numbers) {
System.out.println(num);
}
Jump statements
Jump statements are used to transfer control to a specific point in a program. There are several jump statements in Java, such as the
- break statement and
- continue statement.
break statement
The break statement is used to exit a loop or a switch statement. When a break statement is encountered inside a loop, the loop is immediately terminated, and the control is transferred to the next statement following the loop.
if (i == 5) {
break;
}
System.out.println(i);
}
Continue statement
The continue statement is used to skip the current iteration of a loop and continue with the next iteration. When a continue statement is encountered inside a loop, the current iteration of the loop is skipped, and the control is transferred to the next iteration of the loop
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
In conclusion
control statements are used to control the flow of execution of a program. Java provides various control statements such as decision-making statements (if, if-else, if-else-if ladder, switch), loop statements (do-while, while, for, for-each), and jump statements (break, continue) which allow implementation of complex logic in a program. It’s important to use them correctly and in the appropriate context to make the code more readable and maintainable.