no title...

Page 99

Note: All statements following a match will execute due to switch branches executing sequentially utilizing a top-down approach. Therefore, the keyword “break� must be used to jump out of the switch branch after a match is found.

Nested Switch Branch The nested switch branch consists of a switch branch inside another switch branch. In addition, an if branch can be nested inside of a switch branch. The outer switch branch serves as a nest for the inner branch. Values are unique to their respective outer and inner branch. For instance, a constant can contain the same value in multiple switch branches. The following is a switch branch example:

public class ControlStructures { public static void main (String[] args) { int day = 2, numHours = 4; 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"); switch (numHours) { case 4: System.out.println("Plan to work 4 hours (half a day) due to an appointment"); break; case 8: System.out.println("Plan to work 8 hours today"); break; default: System.out.println("Not sure how many hours I will work today"); break; } break; case 3: System.out.println("Tuesday is the 3rd day of the week"); break;


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