Previous | Home | Next |
Similar to the if-else branch, the switch statement is specifically designed to conditionally switch among multiple alternatives. The syntax for the switch statement follows:
switch (input) { Java Tutorialsase label1: Statement1; break; Java Tutorialsase label2: Statement2; break; … default: DefaultStatement; }
The switch branch evaluates and compares input to all of the case label and branches the program’s execution to the matching case statement list. If no case label matches input, the program branches to the DefaultStatement, if one has been defined (DefaultStatement is optional).
In switch a statement list is simply a series, or list, of statements. Unlike the if-else branch, which directs program flow to a simple or compound statement, the switch statement directs the flow to list of statements. When the program execution moves into a case statement list, it continues from there in a sequential manner.
Note that the case labels must be integers. You cannot test strings. For example, the following is an error
Example
String input = "anyvalue"; switch (input) // wrong, show error { Java Tutorialsase "p": //wrong, will show error . . . break; . . . }
In Java we have goto as a reserved keyword, but the designers of java decided not to include it in the language. In general, goto statements are considered poor style of programming. But some programmers argue that goto is useful for an occasional jump out of a loop.
The Java designers gave it a nod and added a new statement to support this programming style, the labeled break. Let us first look at the unlabeled break statement. The break statement that you use to exit a switch can also be used to break out of a loop.
Example
Java Tutorialslass DemoUnlabeledBreak { public static void main (String args[]) { int i = 0; do { System.out.println(“demobreak”); i++; if (i > 50) break; } while (true); } }
In above example, an infinite do-while loop is created by setting the loop condition to true. But here we incorporate the break statement to exit the loop when i is incremented past 50.
Java also offers a labeled break statement that lets you break out of multiple nested loops. Occasionally something weird happens inside a deeply nested loop. In that case, you may want to break completely out of all the nested loops. It is inconvenient to program that simply by adding extra conditions to the various loop tests.
Java Tutorialslass DemoLabelBreak { public static void main (String args[]) { int i = Integer.parseInt(args[0]); labelbreak: while(true) { for(i=0;i<51;i++) { System.out.println(“demobreak”); i++; if (i > 50) break labelbreak; } } System.out.println("Undesired value of i"); } }
In above program if the value of i is greater than 50 is inputted by user than control will come out of the loop and will pass to the very next statement after the loop.
Another useful statement that works similarly to the break statement is the continue statement. Unlike break, the continue statement is only useful when working with loops and has no relation to the switch branch. The continue statement works like the break statement in that it jumps out of the current iteration of a loop.
The difference with continue is that program execution is restored to the test condition of the loop while break jumps completely out of a loop. Use break when you want to jump out and terminate a loop, and use continue when you want to jump immediately to the next iteration of the loop.
Java Tutorialslass Java Tutorialsontinued { public static void main(String args[]) { int a=0; for(int i=0;i<10;i++) { if(i>7) Java Tutorialsontinue; a=i; System.out.println("The value of a is:"+a); } } }
The output of the program will print values 0 to 7, after the value of i become greater than 7 next statement after continue will not be executed and its control will pass to the header of the loop i.e in case of for loop the control will pass to incrementing slot till the loop terminates.
Previous | Home | Next |