Java Reference
In-Depth Information
}
}
}
Output : 30
The switch Statement
As we studied, if statements are useful in deciding the sequence control of program. But in
case when there a number of alternatives to be tested against a variable, then the complexity
of the program with a number of IF statements increase. Switch statements solves this
problem by allowing a variable to be tested against a list of values. These values are called
cases. When the variable is matched with
a particular case then the statements of that
particular case is executed.
Syntax:
Switch (expression ){
case value1 :
block1
break; //optional
case value2 :
block2
break; //optional
…………………………………………..
……………………………………………….
………………………………………………………
default : //Optional
//default block
Search WWH ::




Custom Search