A Switch Statement Tests a Variable or Expression Agains

Utilize the switch  (in the glossary) statement 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 programme displays the name of the calendar month, based on the value of month, using the switch statement:
public class SwitchDemo {     public static void main(String[] args) {          int calendar month = 8;         switch (month) {             case 1:  Organization.out.println("January"); break;             case 2:  Arrangement.out.println("February"); break;             case 3:  System.out.println("March"); break;             case iv:  System.out.println("April"); intermission;             instance 5:  Arrangement.out.println("May"); suspension;             example vi:  System.out.println("June"); break;             case 7:  Organization.out.println("July"); interruption;             case eight:  System.out.println("August"); break;             case 9:  Arrangement.out.println("September"); pause;             instance 10: System.out.println("October"); intermission;             case 11: System.out.println("November"); interruption;             instance 12: Organisation.out.println("December"); interruption;             default: System.out.println("Not a calendar month!"); suspension;         }     } }          
The switch statement evaluates its expression, in this case the value of calendar month, and executes the appropriate case  (in the glossary) statement. Thus, the output of the program is August. Of course, y'all could implement this by using an if statement:
int month = 8; if (month == one) {     System.out.println("January"); } else if (month == 2) {     Organisation.out.println("Feb"); } . . . //then on          
Deciding whether to use an if argument or a switch statement is a judgment call. You tin can determine which to use, based on readability and other factors. An if statement can be used to make decisions based on ranges of values or conditions, whereas a switch argument can make decisions based only on a single integer or enumerated value. Besides, the value provided to each example statement must be unique.

Another point of interest in the switch statement is the break  (in the glossary) argument after each case. Each suspension statement terminates the enclosing switch statement, and the menses of control continues with the first statement following the switch block. The break statements are necessary because without them, the instance statements fall through. That is, without an explicit intermission, control will flow sequentially through subsequent case statements. Following is an example, SwitchDemo2  (in a .java source file), that illustrates why it might exist useful to have example statements fall through:

public class SwitchDemo2 {     public static void main(String[] args) {          int calendar month = 2;         int twelvemonth = 2000;         int numDays = 0;          switch (month) {             case one:             case three:             case five:             case 7:             instance 8:             case ten:             case 12:                 numDays = 31;                 suspension;             case four:             case 6:             example 9:             case 11:                 numDays = xxx;                 suspension;             case 2:                 if ( ((year % iv == 0) && !(yr % 100 == 0))                      || (year % 400 == 0) )                     numDays = 29;                 else                     numDays = 28;                 break;             default:                 numDays = 0;                 break;         }         Organisation.out.println("Number of Days = " + numDays);     } }          
The output from this program is:
Number of Days = 29          
Technically, the final break is not required, because period would fall out of the switch statement anyway. Still, we recommend using a pause and then that modifying the code is easier and less error-decumbent. You will encounter interruption used to terminate loops in the section Branching Statements (in the Learning the Java Language trail).

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

Enumerated types, a characteristic introduced in v.0, can be used in switch statements. You'll larn all about enumerated types later, in the Classes and Inheritance (in the Learning the Java Language trail) chapter. This section just shows how you can utilise them in a switch statement. Fortunately, information technology's simply similar using integers in a switch argument.

The following code, taken from SwitchEnumDemo  (in a .java source file) is almost identical to the code you previously saw from SwitchDemo2  (in a .java source file). It substitutes enumerated values for the integers, but otherwise the switch statement is the same.

public class SwitchEnumDemo {     public enum Month { JANUARY, Feb, MARCH, Apr,                         MAY, JUNE, JULY, Baronial, SEPTEMBER,                         Oct, Nov, DECEMBER }      public static void main(String[] args) {         Month month = Month.FEBRUARY;         int year = 2000;         int numDays = 0;          switch (month) {             case JANUARY:             instance MARCH:             instance MAY:             case JULY:             example AUGUST:             example OCTOBER:             instance DECEMBER:                 numDays = 31;                 suspension;             case April:             instance JUNE:             example SEPTEMBER:             case NOVEMBER:                 numDays = thirty;                 suspension;             case Feb:                 if ( ((yr % 4 == 0) && !(year % 100 == 0))                      || (year % 400 == 0) )                     numDays = 29;                 else                     numDays = 28;                 break;             default:                 numDays=0;                 break;         }         Organisation.out.println("Number of Days = " + numDays);     } }          

This example showed just a flake of what Coffee linguistic communication enumerations tin can practise. To learn more, see Enumerated Types (in the Learning the Java Language trail)

pricetegamay.blogspot.com

Source: https://www.iitk.ac.in/esc101/05Aug/tutorial/java/nutsandbolts/switch.html

0 Response to "A Switch Statement Tests a Variable or Expression Agains"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel