no title...

Page 96

variableName

Name of the variable

{

An opening curly bracket

case constant1, 2, 3 …

Contains a possible match for the variable’s value

statement(s)

Statement to be executed if the variable’s value match a given case

;

Semi-colon completes a statement

break

An optional keyword that exits out of the switch branch

;

Semi-colon completes the break

default

An optional keyword that will execute if the variable’s value does not match a case

statement

Default statement to be executed if the variable’s value does not match a case

} A closing curly bracket Figure 4.7 – Switch Branch Syntax Details

The following is a switch branch example:

public class ControlStructures { public static void main (String[] args) { int day = 6; switch (day) { case 1: System.out.println("Sunday is the 1st day of the week"); break; case 2: System.out.println("Monday is the 2nd day of the week"); break; case 3:


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.