Saturday, February 15, 2020

Switch Statements In Java

Switch Statements In Java


The "switch" or "switch-case" is a branching statement like if and if-else, but it generally less used statement than if-else. The "switch" statement is an alternative to the "if-else-if" ladder when there are several possible execution paths. Different paths are represented by cases. The "case" keyword is used to represent a case.  Different actions based on different conditions are executed with the "switch" statement. The "switch" is used with the case clause. Each case also carries a break statement in the end. The break statements in the switch are not actually required by the syntax of the switch statement. The effect of a break is to let control jump past the end of the switch statement and skipping over all the remaining cases. Skipping the break statement will cause the next case to be executed even if the evaluation does not match the case.

Switch Statement Syntax:


switch(expression) 
{
case a:
// code block
break;
case b:
// code block
break;
case c:
// code block
break;

default:
// code block
}

Flow-diagram for switch-case

switch case statement JavaScript


The "default" Keyword


The default keyword represents the default case, it means if none of the cases is matched than the default case will execute.

for example
var choice=5;
       switch (5)
                {
                   case 0:
                   document.write( "Sunday");
                   break;
                   case 1:
                   document.write("Monday");
                   break;
                   case 2:
                   document.write("Tuesday");
                   break;
                   case 3:
                   document.write("Wednesday");
                   break;
                   case 4:
                   document.write("Thursday");
                   break;
                   case 5:
                   document.write("Friday");
                   break;
                   case 6:
                   document.write("Saturday");
                    break;
                   default:
                   document.write("Not able to find the day");
                   break;

                    }


Common Cases


Sometimes, it is possible that we want our two or more cases to execute the same code. We can use a common case for this,


for example case, 1, 2  and 3,4 share the same code blocks.
var choice=5;
       switch (5)
                {
                   case 0:
                   document.write( "Sunday");
                   break;
                   case 1://Common cases
                   case 2:
                   document.write("Monday");
                   break;
                   case 3://Common cases
                   case 4:
                   document.write("Tuesday");
                   break;
                   default:
                   document.write("Wednesday");
                   break;
                   }


JavaScript uses strict comparison testing the case match


Switch-case in JavaScript uses strict comparison using  (===) operator. It means the values and the type must be the same to match with a case. A strict comparison can only be true if the operands are of the same type.

You can edit and run this code