Call Switch Again and Again in C

Use the switch  (in the glossary) argument to conditionally perform statements based on an integer expression or enumerated type. Following is a sample program, SwitchDemo  (in a .java source file), 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:
public 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;         }     } }          
The switch statement evaluates its expression, in this case the value of month, and executes the appropriate example  (in the glossary) statement. Thus, the output of the program is August. Of grade, yous could implement this by using an if statement:
int month = 8; if (month == ane) {     System.out.println("Jan"); } else if (month == 2) {     System.out.println("February"); } . . . //and so on          
Deciding whether to use an 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  (in the glossary) 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  (in a .java source file), that illustrates why it might be useful to have case statements fall through:

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);     } }          
The output from this program is:
Number of Days = 29          
Technically, the final 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 (in the Learning the Java Language trail).

Finally, yous should use the default  (in the glossary) statement at the end of the switch to handle all values that aren't explicitly handled by one of the case statements.

Enumerated types, a feature introduced in 5.0, can be used in switch statements. You'll learn all about enumerated types later, in the Classes and Inheritance (in the Learning the Java Language trail) 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  (in a .java source file) is near identical to the code yous previously saw from SwitchDemo2  (in a .java source file). 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 (in the Learning the Java Language trail)

morgandoody1986.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel