Topic 4
CONTROL
STRUCTURE
S
Switch ..case
1
WHAT IS
Switch ..case?
Similar to an if statement,
a switch statement allows a
variable/expression to be
examined against a list of
options.
Each option is called a case,
and for each case, different
tasks can be performed.
Switch
STRUCT
URE
Switch..case SYNTAX
switch (variable/expression)
{
case value1: statements1
break;
case value2: statements2
break;
...
case valuen: statementsn
break;
default: statements
break;
}
NESTED If & Switch
STATEMENT
int month = 2;
if (month == 1)
System.out.println("Month is Jan");
else if (month == 2)
System.out.println("Month is Feb");
else if (month == 3)
System.out.println("Month is March");
else
System.out.println("Not the first quarter of the year");
int month = 2;
switch (month){
case 1: System.out.println("Month is Jan");break;
case 2: System.out.println("Month is Feb"); break;
case 3: System.out.println("Month is March"); break;
default: System.out.println("Not the first quarter of the year");
}
THE Break STATEMENT
Often a break statement is used as the last
statement in each case's statement list.
A break statement causes control to transfer
to the end of the switch statement.
If a break statement is not used, the flow of
control will continue into the next case.
CONTROL FLOW OF Switch
STATEMENT WITH AND WITHOUT THE
Break STATEMENTS
switch (n){ switch (n){
case 1: x = 20; case 1: x = 20; break;
case 2: x = 40; case 2: x = 40; break;
default: x = 60; default: x = 60; break;
} }
Switch EXAMPLES
int m = 2; char ch = ‘b’;
switch (m) switch (ch)
{ {
case 1 : case ‘a’ :
System.out.println("m=1"); System.out.println("ch=a");
break;
case 2 : case ‘b’ :
System.out.println("m=2"); System.out.println("ch=b");
break;
case 3 : case ‘c’ :
System.out.println("m=3"); System.out.println("ch=c");
break;
default: default:
System.out.println("default"); System.out.println("default");
break; break;
} }
Output: Output:
m=2 ch=b
ch=c
default
Switch EXAMPLES
char letter = 'B';
if((letter == ‘A’) || (letter == ‘a’))
switch (letter) System.out.println("You chose A");
{
case 'A':
case 'a': System.out.println("You chose A");
break;
case 'B':
case 'b': System.out.println("You chose B");
break;
default: System.out.println("You chose other than A & B");
break;
}
Switch AND NESTED If EXAMPLE
int number=1, x = 2;
if (x>=1 && x<=3)
int number=1, x = 2; number++;
switch (x)
else if (x == 4)
{ number += 4;
case 1:
case 2: else
case 3: number++; number+=5;
break; int number=1, x = 2;
case 4 : number+=4;
break; if (x==1 || x==2 || x==3)
default: number+=5; number++;
break;
} else if (x == 4)
number += 4;
else
number+=5;
switch Example
What is the value of j after these switch
statements?
int i = 1; int i = 1;
int j = 0; int j = 0;
switch (i+1) {
switch (i+1) {
case 0: j = 0;break;
case 0: j = 0; case 1: j = 1;break;
case 1: j = 1; case 2: j = 3;break;
case 2: j = 3; default: j = -2;break;
default: j = -2; }
}
j=-2 j=3
Exercise
Rewrite the following if statement using a switch:
int selection;
System.out.print("Enter your selection: ");
selection = scan.nextInt();
if (selection == 1)
System.out.println("You selected JAVA");
else if (selection == 2)
System.out.println("You selected VISUAL BASIC");
else if(selection == 3)
System.out.println("You selected PHP");
else
System.out.println("Invalid selection");
Exercise
Rewrite the following switch using if else statement :
int number=1, x = 2; int number=1, x = 2;
switch (x) if (x>=1 && x<=4)
{ number = number *2;
case 1:
case 2:
case 3: else if (x== 5)
case 4: number = number *2; number = number *5;
break;
case 5 : number = number *5; else
break; number--;
default: number--; break;
}
TODAY’S TAKE AWAY
Switch selection
control structure.
Nested If & Switch
statement.
Break statement.
14