Selection statements | Java

Selection statements | Java

Selection statements | Java

It is used to evaluate the condition. It is an expansion of if statement. In if statement, if the condition is true then the corresponding statement will be executed otherwise the block will be discarded.

Syntax:

if (condition) < // statement if action is true >else < // statement if action is false >
 int x = 10; if (x) < // int cannot be converted to boolean System.out.println("Program executed successfully"); > else < System.out.println("Program failed"); > 
Example 2 -
 int x = 10; if (x = 20) < // int cannot be converted to boolean System.out.println("Program executed successfully"); > else < System.out.println("Program failed"); > 
Example 3 (Valid) -
 int x = 10; if (x == 20) < System.out.println("Program executed successfully"); > else < System.out.println("Program failed"); > 
Example 4 (Valid) -
 int x = true; if (x == false) < System.out.println("Program executed successfully"); > else < System.out.println("Program failed"); > 
 // case 1 - Valid if (true) System.out.println("Program executed successfully"); // case 2 - valid if (true) // case 3 - Invalid if (true) int x = 10; System.out.println(x); // case 4 - Valid if (true) < int x = 10; System.out.println(x); > 

Nested if-else statements

A nested if-else statement is an if-else statement that is embedded within another if or else block. This allows for more complex decision-making logic based on multiple conditions.

Here's an example of nested if-else statements in Java:

public class NestedIfElseExample < public static void main(String[] args) < int x = 10; int y = 20; if (x > y) < System.out.println("x is greater than y"); > else < System.out.println("x is not greater than y"); if (x < y) < System.out.println("x is less than y"); > else < System.out.println("x is equal to y"); > > > > 

In this example, there is an outer if-else statement checking if x is greater than y . If that condition is false, the nested if-else statement inside the else block is executed, checking whether x is less than y or equal to y .

if-else-if ladder statements

It is a series of if-else statements where each "else-if" condition is checked only if the preceding conditions are false. This allows you to evaluate multiple conditions in a sequential manner. Here's the basic syntax:

if (condition1) < // Code to execute if condition1 is true > else if (condition2) < // Code to execute if condition2 is true > else if (condition3) < // Code to execute if condition3 is true > else < // Code to execute if none of the conditions are true > 

Each "if" or "else-if" condition is evaluated from top to bottom, and as soon as a true condition is encountered, the corresponding block of code is executed, and the rest of the conditions are skipped.

Here's an example of an if-else-if ladder:

public class IfElseIfExample < public static void main(String[] args) < int number = 5; if (number > 0) < System.out.println("Positive number"); > else if (number < 0) < System.out.println("Negative number"); > else < System.out.println("Zero"); > > > 

In this example, the program checks whether the variable number is positive, negative, or zero. The conditions are checked in a sequential manner, and the corresponding block of code is executed based on the first true condition encountered.

It's important to note that in an if-else-if ladder, only the code block associated with the first true condition is executed, even if multiple conditions are true. This is different from a series of independent if statements, where multiple conditions can be true, and their corresponding code blocks would all be executed.

switch Statement

 switch (expression) < case value1: // Code to be executed if expression == value1 break; case value2: // Code to be executed if expression == value2 break; // Additional cases. default: // Code to be executed if none of the cases match > 

Here's an example of a switch statement:

 public class SwitchExample < public static void main(String[] args) < int dayOfWeek = 3; switch (dayOfWeek) < case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; default: System.out.println("Weekend"); > > > 

In this example, the program prints the day of the week based on the value of the dayOfWeek variable. The switch statement provides a concise way to handle multiple cases and improves code readability compared to a series of if-else statements.

 //Valid statement switch (x) < >//Valid statement switch (x) < case 1: //case statement > //Valid statement switch (x) < case 1: //case statement default : //default statement > //Valid statement switch (x) < default : //default statement > 
 switch (x) < System.out.println("Switch statement"); //Invalid > 
 public class SwitchExample < public static void main(String[] args) < int y = 3; int x = 10; switch (x) < case 1: System.out.println("Monday"); break; case x: //Constant Expression Required System.out.println("Tuesday"); break; default: System.out.println("Weekend"); > > > 
 public class SwitchExample < public static void main(String[] args) < int final y = 3; int x = 10; switch (x) < case 1: System.out.println("Monday"); break; case x: //Valid System.out.println("Tuesday"); break; default: System.out.println("Weekend"); > > > 
 public class SwitchExample < public static void main(String[] args) < int y = 3; int x = 10; switch (x) < case 1: System.out.println("Monday"); break; case 12+20: //Valid System.out.println("Tuesday"); break; default: System.out.println("Weekend"); > > > 
 public class SwitchExample < public static void main(String[] args) < byte x = 10; switch (x) < case 1: case: 100; case: 1000; // found 'int' required 'byte' > > > 
 public class SwitchExample < public static void main(String[] args) < byte x = 10; switch (x) < case 32: case: 32; // Duplicate case label > > > 

Fall through inside switch

Within the switch if any case is matched from that case onwards all statement will be executed until break or end of the switch this is called fall through inside a switch.

The main advantage of fall through inside switch is we can define common action for multiple cases (code reusability).

public class SwitchExample < public static void main(String[] args) < int x = 1; switch (x) < case 1: System.out.println("Monday"); case 2: System.out.println("Tuesday"); break; default: System.out.println("Weekend"); > > > 

In the above examples the output will be-

Monday Tuesday 

default

Within switch statement we can write default case anywhere but it is recommended to write as last case.

public class SwitchExample < public static void main(String[] args) < int y = 3; int x = 10; switch (x) < case 1: System.out.println("Monday"); break; default: System.out.println("Weekend"); case 2: System.out.println("Tuesday"); break; > > > 

In the above examples the output will be-

Weekend 

Conclusion

Decision-making in Java involves various statements, including if-else, nested if-else, if-else-if ladder, and switch. Understanding their syntax, rules, and use cases is crucial for effective control flow and decision logic in programming.

Did you find this article valuable?

Support Xander Billa by becoming a sponsor. Any amount is appreciated!