Switch case control structure.

In this article,Switch case control structure is explained in detail with examples.This control structure works same as that of cascading if else provided we use proper break statements and control the execution.
Syntax
switch(expressions)
{
case value1:
---statement1---
break;
case value 2:
---statment2---
break;
case value3:
---statement3---
break;
-----
-----
default:
----default statements---
}
Here,switch accepts expression value,such value is matched with case values linearly one after the other.which ever case value is matching with expressing value, its corresponding statements are executed with a break.
Break statements takes the control of execution directly out of switch control structure body i.e, it skips remaining cases and default case. If no case value is matching with expression value then the last default case statements are executed. (From topic Switch case control structure)
Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
ch='x';
switch(ch)
{
case 'x':
i=10;
break;
case 'y':
i=20;                            //(From topic Switch case control structure)
break;
case 'z':
i=30;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform

Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
ch='y';
switch(ch)
{
case 'x':
i=10;
break;
case 'y':
i=20;
break;
case 'z':
i=30;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform

Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
ch='z';
switch(ch)
{
case 'x':
i=10;
break;
case 'y':
i=20;
break;
case 'z':
i=30;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform

Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
ch='A';
switch(ch)                                            //(From topic Switch case control structure)
{
case 'x':
i=10;
break;
case 'y':
i=20;
break;
case 'z':
i=30;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform

Special Cases
Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
switch(122) //accepted in java because it is int value.
{
case 'x':
i=10;
break;
case 'y':
i=20;
break;
case 122: // unicode value of z=122 so we removed "case z". If we keep both it gives duplicate value                      error.
i=35;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform


Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
switch(120.5f) //do not accepted in java because it is float value.It shows error.
{
case 'x':
i=10;
break;
case 'y':
i=20;
break;
case 'z':
i=30;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform

Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
switch((int)120.5f) //accepted in java.Float is converted to int (120) unicode value of x=120,output is                                  case x.
{
case 'x':
i=10;
break;
case 'y':
i=20;
break;
case 'z':
i=30;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform

Ex
//program related to Switch case control structure--switch case.
class SwitchCaseConstruct
{
public static void main(String args[])
{
int i;
char ch;
switch("hi") //from JDK 1.6; Switch with String is possible but we need to use double quotes.
{
case "x":
i=10;
break;
case "y":
i=20;
break;
case "z":
i=30;
break;
default:
i=40;
}
System.out.println("Number value I is: "+i);
}
}
Output
Switch case control Structure-javaform

Continue to the next topic Looping control structures.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabad, surprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required