A Switch Statement Tests a Variable or Expression Agains
SwitchDemo 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 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
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
, 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: Technically, the finalNumber of Days = 29
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 Finally, you should use the default
statement at the terminate of the switch to handle all values that aren't explicitly handled by one of the example statements.
switch statements. You'll larn all about enumerated types later, in the Classes and Inheritanceswitch statement. Fortunately, information technology's simply similar using integers in a switch argument. The following code, taken from SwitchEnumDemo
is almost identical to the code you previously saw from SwitchDemo2
. 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
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