switch statement in java

switch statement in java

The `switch` statement in Java programming is used to execute multiple code statements from multiple related program code blocks at a time, based on a user-defined custom program expression or logic value. The switch statement in Java programs is commonly used when the programmer needs to make a specific decision based on the value of a desired program variable, where a single output may have multiple possible outputs across multiple blocks.

switch statement in java

The `switch` statement in any Java program is mostly used as an alternative choice in a series of `if-else-if` program logic branch statement conditions. The switch statement in Java programs can be more readable and easier to maintain than dealing with multiple choices. Especially when Java user-defined multiple conditions are involved where a single variable value is being compared to a constant value.

The syntax of the `switch` statement in Java.

case expression 1:

// here Block of program code to be executed, if the expression is equal to expression 1

break; // Optional choice – if you want you can, exit from switch statement

case expression 2:

// here Block of program code to be executed, if the given expression is equal to expression 2

break; // Optional choice – if you want you can, exit from the switch statement

case expression 3:

// here Block of program code to be executed, if the given program expression is equal to expression 3

break; // Optional choice – here if you want the we can exit the switch statement

default:

// here Block of program code to be executed, if here no above given case matches to the expression

break; // Optional choice – finally it exits the switch statement program logic

}

Element of switch statement.

  • expression – This is the variable or user-defined value in the switch statement that is being compared in the switch block. Here, the user-defined switch statement expression can be any one of the given data types. For example, byte, short, int, char, String, or an enum data type.
  • case value – This is the value in a switch statement against which the expression is compared. If the switch expression matches a case value expression, the corresponding block of program code will be automatically executed.
  • break – Here, the break keyword breaks the execution of the `switch` statement, preventing it from moving the switch logic to the next case block. If the `break` statement is avoided, the program continues executing subsequent switch case statements, which is known as the fall-through concept in Java.
  • default – The default switch statement is an alternative block of code that is executed in a switch program if none of the cases in the switch expression matches the expression. If no `default` value is provided in the default statement, and none of the case values match, the `switch` statement terminates automatically without executing any block.

Example of `switch` statement in Java.

public class Main

{

public static void main(String[] args) {

int dayofweek = 7;

String dayvalue;

switch (dayofweek) {

case 1:

dayvalue = “Monday”;

break;

case 2:

dayvalue = “Tuesday”;

break;

case 3:

dayvalue = “Wednesday”;

break;

case 4:

dayvalue = “Thursday”;

break;

case 5:

dayvalue = “Friday”;

break;

case 6:

dayvalue = “Saturday”;

break;

case 7:

dayvalue = “Sunday”;

break;

default:

dayvalue = “Wrong day selection”;

break;

}

System.out.println(“Day of today is – ” + dayvalue);

}

}

Detailed explanation of the switch.

In this switch example, the user-defined variable `dayvalue` is compared against each `case`. Since the value of `dayvalue` is defined as 7, the program finds an exact match with `case 7` and executes the block statement `dayvalue = “Sunday”;`. Subsequently, the `break` statement exits the program.

Example of a switch with the String data type.

Java developers can also use switch statement with String data type in programs.

public class Main

{

public static void main(String[] args) {

String programming = “Php”;

switch (programming) {

case “Java”:

System.out.println(“Java programming.”);

break;

case “Python”:

System.out.println(“Python programming.”);

break;

case “JavaScript”:

System.out.println(“JavaScript programming.”);

break;

case “Php”:

System.out.println(“Php programming.”);

break;

default:

System.out.println(“No choice available.”);

break;

}

}

}

Explanation of the switch with the String data type.

In this program, the variable programming is compared with each String value in the case. Since the programming variable has the value “Php”, the program performs a proper match against the case “Php” and executes the output statement System.out.println(“Php programming.”);.

Example of fall-through behavior in a Java program.

In a Java program, if the user does not use the `break` statement keyword, the current program will continue to run “fall-through” and subsequent `case` statements will continue to run one by one, whether or not they match the program variable condition.

public class SwitchFallThroughExample {

public class Main

{

public static void main(String[] args) {

int test = 3;

switch (test) {

case 1:

System.out.println(“Test 1”);

case 2:

System.out.println(“Test 2”);

case 3:

System.out.println(“Test 3”);

case 4:

System.out.println(“Test 4”);

case 5:

System.out.println(“Test 5”);

default:

System.out.println(“Wrong test selection”);

}

}

}

Explanation of fall-through behavior.

In this program, because the `break` statement is not used, when the `case 3` expression matches, it will fall-through to the subsequent `case` statement block and run all of those statements simultaneously. This type of switch statement is called fall-through behavior.

To avoid unwanted fall-through behavior in a Java program, Java users can add a `break` statement after each `case` block to stop the fall-through flow control.

Java `default` statement usage example.

The `default` `case` is used in a Java switch statement program when none of the user-defined `case` expressions match the value. It is, in the end, an alternative selection choice to the `switch` statement. Sometimes, the default choice statement is used to handle unexpected values in a switch statement.

public class Main

{

public static void main(String[] args) {

int laptop = 14;

switch (laptop) {

case 1:

System.out.println(“Macbook Pro”);

break;

case 2:

System.out.println(“HP”);

break;

case 3:

System.out.println(“Dell”);

break;

case 4:

System.out.println(“Lenovo”);

break;

case 5:

System.out.println(“Asus”);

break;

case 6:

System.out.println(“Acer”);

break;

case 7:

System.out.println(“Samsung”);

break;

case 8:

System.out.println(“Gigabyte”);

break;

case 9:

System.out.println(“Msi”);

break;

case 10:

System.out.println(“Android”);

break;

case 11:

System.out.println(“Microsoft”);

break;

case 12:

System.out.println(“Alienware”);

break;

case 13:

System.out.println(“Skytech”);

break;

default:

System.out.println(“Wrong laptop selection”);

break;

}

}

}

}

Java `default` statement.

Here, the switch variable laptop has the value 14, which does not match any case value in the current program. Because of this, the program jumps directly to the default block and prints the statement System.out.println(“Wrong laptop selection”);

Limitations of the Java switch statement.

  • Limited to certain types – The switch expression statement in Java programming only supports certain data types, such as byte, short, int, char, String, and enum. The switch statement in Java programs cannot be used with float or double data types or complex program conditions such as ranges.
  • No range checking – The switch statement analyzes only exact matches in a program, so it cannot be used for multiple conditions such as “between a start point and an end point” without using additional logic.
  • No multiple conditions per case – A switch statement must match each case in a program to a given specific value. Users cannot directly include a case with multiple conditions in a switch statement.

Use switch vs. if-else-if in Java.

Use switch in Java when.

  • In a Java user program, a variable or expression is comparing a single variable or expression to multiple constant values.
  • Where the value in the current program is an integer data type, such as a char, string, or enum data type.
  • When Java programmers want to improve program source code readability and program structure.

Use if-else-if in Java when.

  • When the user needs to test more complex program conditions or ranges in a Java program, such as whether a data type value is between 1 and 10.
  • When Java programmers need to evaluate multiple program expressions or perform logical condition operations in a program.

Detail explanation of switch statement.

Aspect behaviourSwitch statementif-else-if block statement
Support Expression TypesIt supports int, char, String, enum, data type etc.It supports Any boolean condition or complex expression given by user.
ReadabilitySwitch statement Easier to read for many constant comparisons define by user in multiple blockIn if else block statement Can become complex with many conditions to deal all at a same time
Where to Use CaseSwitch case statement helpful When you need to check a single variable for multiple given constant valuesIf else statement capable to handling more complex conditions or ranges given by user
Fall-Through conceptSwitch case statement support fall through concept if break is omitted in each blockIf else statement No fall-through behaviour support, each if else condition is checked independently by its block statement

Leave a Reply