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.

java-decision-making-statements
java-decision-making-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

  1. if statement,
  2. if-else statement,
  3. if-else-if ladder, and
  4. 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:

program
if (condition) {
// 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:

program
int num = 5;
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:

program
if (condition) {
// 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:

program
int num = -5;
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:

program
if (condition1) {
// 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:

program
int marks = 85;
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:

program
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:

program
int num = 15;
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:

program
switch (expression) {
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:

program
int day = 2;
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:

program
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:

Program
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

  1. do-while loop,
  2. while loop,
  3. for loop, and
  4. 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:

program
do {
// code to be executed
} while (condition);

For example, the following program uses a do-while loop to print the numbers from 1 to 10:

program
int i = 1;
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:

program
while (condition) {
// code to be executed
}

For example, the following program uses a while loop to print the numbers from 1 to 10:

program
int i = 1;
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:

program
for (initialization; condition; increment/decrement) {
// code to be executed
}

For example, the following program uses a for loop to print the numbers from 1 to 10:

program
for (int i = 1; i <= 10; i++) {
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:

program
for (type variable : array/collection) {
// code to be executed
}

For example, the following program uses a for-each loop to print the elements of an array:

program
int[] numbers = {1, 2, 3, 4, 5};
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

  1. break statement and
  2. 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.

program
for (int i = 1; i <= 10; i++) {
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

program
for (int i = 1; i <= 10; i++) {
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.

Tags:

Have any doubt or suggestion? feel free to write here :)

Leave a reply

ProProfession.com
Logo