Switch Statement

Switch statement are like if-else statement which uses equal to operator (==) in each conditional statement. We use switch statements to test the variable out with multiple choices.

The code below is how the syntax of the switch statement will look like:

// we are gonna test the variableName against some choices
switch(variableName){

case 1:
// read case 1 as if the variableName is equal to 1. The case replace the if statement.
System.out.println(“The variableName is 1”);
break; // if the case is true it will break out of the switch statement.

default:
// is like the else statement. If none of the given cases is true then the default block of code will be executed.
System.out.println(“The variableName is unknown”);
break;

}

Below is the snippet of the code:

switchStatement
Code for switch statement to check integers with comments.

Leave a comment