0% found this document useful (0 votes)
54 views12 pages

Switch Case: (Selection Statement)

The document discusses the switch case statement in Java, which provides a multi-branch selection statement to test the value of an expression against a list of case values and execute different blocks of code based on matching values. The syntax includes a switch expression, cases with values and code blocks, an optional default, and break statements to end execution within each case block. Examples demonstrate using switch case to translate day numbers to names and check if a character is a vowel.

Uploaded by

Najiya Pm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views12 pages

Switch Case: (Selection Statement)

The document discusses the switch case statement in Java, which provides a multi-branch selection statement to test the value of an expression against a list of case values and execute different blocks of code based on matching values. The syntax includes a switch expression, cases with values and code blocks, an optional default, and break statements to end execution within each case block. Examples demonstrate using switch case to translate day numbers to names and check if a character is a vowel.

Uploaded by

Najiya Pm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SWITCH CASE

(SELECTION STATEMENT)

INTRODUCTION

Multi Branch selection statement.


To test the value of an expression with a series of

character or integer values.

On finding a matching value the control jumps to the statement pertaining to that value

and the statement is executed, till the break statement is encountered or the end of switch
is reached.
The expression must either evaluate to an integer value or a character value. It

cannot be a String or a real number.

SYNTAX:
switch( variable / expression )
{
case value1: statements1;
break;
case value2: statements2;
break;
..
default: statements3;

} EG1

EG2

When a match is found, the statement sequence associated with that case is executed until

the break statement or the end of switch statement is reached.

DEFAULT CLAUSE

The default statement gets executed when no match is found.


If the control flows to the next case below the matching case, in the absence of break, this

is called fall through.


The default clause is Optional and, if it is missing, no action takes place if all matches

fail.

BREAK STATEMENT IN SWITCH

Stop the execution of the switch statement and transfer the control to the statement

immediately following the closing brace of the switch statement.


It has to be included as the last statement of each case.
If you do not give break after each case, then java will start executing the

statements from matching case and keep on executing statements for following
cases as well until either a break statement is found or switch statements end is
encountered.
Fall through

A case statement cannot exist by itself, outside of a switch


Break statement is one of the java jump statements
When a break is encountered in a switch statement, program execution jumps to the line of

code following the switch statement i.e., outside the body of switch statement.
No two case labels in the same switch can have identical values

SWITCH V/S IF - ELSE

1) Test for equality


2) Test the value of same variable against a set of constants
3) Cannot test for ranges
4) Test only integer (short, byte, int ) or character
5) Case label value must be a constant
6) Efficiency in terms of code use

EXAMPLE TRANSLATE EQUIVALENT NAME OF THE DAY OF


THE WEEK ( 0-SUNDAY, 1- MONDAY,..)
//assuming day number is available in int variable dow

switch(dow)
{
case 0:
ans=Sunday;break;
case 1: ans=Monday;break;
case 2: ans=Tuesday;break;
case 3: ans=Wednesday;break;
case 4: ans= Thursday;break;
case 5: ans=Friday;break;
case 6: ans=Saturday; break;
default: ans=invalid input ;
}
outputLabel.setText(Weekday for day no: +dow+ is +ans);

Syntax
:

EXAMPLE 2- CHECK CHARACTER IS VOWEL OR NOT


//assuming a character is available in the char variable ch
switch(ch){
case a:
case e:
case i:
case o:
case u:
outputLabel.setText(charcter +ch+ is vowel);

QUESTIONS

1) day finder
2) Week of the day
3) Calculator program
4) Area of shape

Create an application that obtains day (1-31), month (1-12), and year (>0) from the user

and displays in dd -MON- yyyy format, e.g., if the user has entered as 13,04,2009 as day ,
month and year then it display date as 13-APR-2009.

You might also like