Call Switch Again and Again in C
SwitchDemo
, that declares an integer named month
whose value supposedly represents the month in a date. The plan displays the name of the calendar month, based on the value of month
, using the switch
argument: Thepublic class SwitchDemo { public static void principal(Cord[] args) { int month = 8; switch (month) { instance 1: Organization.out.println("January"); pause; case ii: System.out.println("February"); interruption; case three: System.out.println("March"); pause; case 4: Organization.out.println("April"); intermission; case 5: Organisation.out.println("May"); break; case vi: System.out.println("June"); intermission; case 7: Organization.out.println("July"); break; case 8: Arrangement.out.println("August"); break; case 9: System.out.println("September"); break; case 10: Arrangement.out.println("October"); pause; case eleven: Organization.out.println("Nov"); interruption; case 12: Arrangement.out.println("December"); break; default: Arrangement.out.println("Not a month!"); pause; } } }
switch
statement evaluates its expression, in this case the value of month
, and executes the appropriate example statement. Thus, the output of the program is August
. Of grade, yous could implement this by using an if
statement: Deciding whether to use anint month = 8; if (month == ane) { System.out.println("Jan"); } else if (month == 2) { System.out.println("February"); } . . . //and so on
if
statement or a switch
argument is a judgment call. You can make up one's mind which to use, based on readability and other factors. An if
statement can be used to make decisions based on ranges of values or atmospheric condition, whereas a switch
statement tin make decisions based only on a single integer or enumerated value. Besides, the value provided to each example
statement must be unique. Another bespeak of interest in the switch
argument is the break argument after each example
. Each pause
statement terminates the enclosing switch
statement, and the flow of control continues with the first statement following the switch
cake. The suspension
statements are necessary considering without them, the case
statements autumn through. That is, without an explicit break
, control will catamenia sequentially through subsequent case
statements. Following is an example, SwitchDemo2
, that illustrates why it might be useful to have case
statements fall through:
The output from this program is:public form SwitchDemo2 { public static void main(String[] args) { int month = ii; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: instance 5: instance 7: case 8: case 10: instance 12: numDays = 31; pause; case 4: case half-dozen: case 9: case 11: numDays = 30; break; case 2: if ( ((yr % four == 0) && !(yr % 100 == 0)) || (twelvemonth % 400 == 0) ) numDays = 29; else numDays = 28; break; default: numDays = 0; break; } Organization.out.println("Number of Days = " + numDays); } }
Technically, the finalNumber of Days = 29
break
is non required, because flow would autumn out of the switch
statement anyway. Withal, nosotros recommend using a break
so that modifying the code is easier and less error-decumbent. Y'all will come across suspension
used to terminate loops in the section Branching Statements. Finally, yous should use the default statement at the end of the switch
to handle all values that aren't explicitly handled by one of the case
statements.
switch
statements. You'll learn all about enumerated types later, in the Classes and Inheritance chapter. This section just shows how you tin can employ them in a switch
argument. Fortunately, it's merely like using integers in a switch
argument. The following code, taken from SwitchEnumDemo
is near identical to the code yous previously saw from SwitchDemo2
. It substitutes enumerated values for the integers, but otherwise the switch
argument is the same.
public course SwitchEnumDemo { public enum Month { JANUARY, Feb, MARCH, Apr, MAY, JUNE, JULY, August, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER } public static void main(String[] args) { Calendar month month = Calendar month.FEBRUARY; int twelvemonth = 2000; int numDays = 0; switch (month) { example JANUARY: case MARCH: example MAY: instance JULY: example AUGUST: case OCTOBER: case Dec: numDays = 31; break; case April: instance JUNE: case SEPTEMBER: case Nov: numDays = thirty; pause; case February: if ( ((yr % 4 == 0) && !(yr % 100 == 0)) || (twelvemonth % 400 == 0) ) numDays = 29; else numDays = 28; break; default: numDays=0; suspension; } Arrangement.out.println("Number of Days = " + numDays); } }
This example showed just a bit of what Java language enumerations can do. To larn more, see Enumerated Types
Source: https://www.iitk.ac.in/esc101/05Aug/tutorial/java/nutsandbolts/switch.html
0 Response to "Call Switch Again and Again in C"
Post a Comment