java2db

How to use switch control flow statement in java



To select one option from the multiple options ,  use switch control flow statement.Depending on the selected option, a particular task can be performed. A task represents one or more statements .

 

switch control flow statement in javaA switch works with the byte,short,char and int primitive data types.It also works with enumerated types.
switch control flow statement in javaIn Java SE 7 and later, we can use String object in the switch statement expression.

 

Syntax:

 switch (Value/expression)
{
case  Vale1 :// statements
————–
break;
case Vale2 :// statements
————–
break;
default: // statements
————–
break;
}

switch control flow statement in java switch case checking with a  character  :

switch control flow statement in java switch case checking with a number value :

switch control flow statement in java switch case without  break :

From the above example ,if there is no break statement in between the switch cases , all statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels , until a break statement is encountered.

switch control flow statement in java switch case without  default  :

switch control flow statement in java  Another example for switch :

Execution Result:

 

 

 




Related Posts :
What are the main basic control flow statements in java
   [will not be published]



^ <