> switch statement java ~ Online tutorial

switch statement java

The switch Statement
Fetch me a dozen crab-tree staves, and strong ones: these are but switches . . .
The switch statement transfers control to one of several statements depending on
the value of an expression.

SwitchStatement:


switch ( Expression ) SwitchBlock
SwitchBlock:
{
SwitchBlockStatementGroupsopt SwitchLabelsopt
}
SwitchBlockStatementGroups:


SwitchBlockStatementGroup
SwitchBlockStatementGroups SwitchBlockStatementGroup

SwitchBlockStatementGroup:
SwitchLabels BlockStatements

SwitchLabels:
SwitchLabel
SwitchLabels SwitchLabel

SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
EnumConstantName:
Identifier

The type of the Expression must be char, byte, short, int, Character,
Byte, Short, Integer, or an enum type , or a compile-time error occurs.
The body of a switch statement is known as a switch block. Any statement
immediately contained by the switch block may be labeled with one or more case
or default labels. These labels are said to be associated with the switch statement,
as are the values of the constant expressions in the case labels.
All of the following must be true, or a compile-time error will result:
• Every case constant expression associated with a switch statement must be
assignable to the type of the switch Expression.
• No switch label is null.

Program
class Toomany
{
static void howMany(int k)
{
switch (k) {
case 1:System.out.print("one ");
case 2:System.out.print("too ");
case 3:System.out.println("many");
}
}
public static void main(String[] args)
{
howMany(3);
howMany(2);
howMany(1);
}
}





Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: