Results 1 to 4 of 4
  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default Breaking from nested switch

    I have 3 switch block that are nested. From inner most, I have a case from which I want to break from all the switches.

    How this can be done.

    Thanks a lot for looking into this.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    9

    Default Example similar to the goto statement in other languages

    Hello javaplus

    This is the best that I can do now:
    Java Code:
    int i = 3, j = 2, k = 4;
    boolean switching = true;
    [B]gohere:[/B]{
    	if (switching) switch (i){
    		case 0:
    			System.out.println("impossible :)");
    			break;
    		case 3:
    			switch (j){
    				case 2:
    					switch (k){
    						case 4:
    							System.out.println("breaking");
    							i = 4;
    							switching = false;
    							[B]break gohere;[/B] // similar to a while loop, but execution skips to the label immediately.
    					}
    					break;
    			}
    			System.out.println("oops)");
    			break;
    		case 4:
    			System.out.println("oops)");
    			break;
    	}
    }
    This gives the output:
    Java Code:
    breaking
    Note that there was no oopses. :D

    I hope that helped. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Thanks Tim. You used labels. I have read somewhere that it is not recommended to use labels in Java. But if there is an absolute need, then one may use them. I think its the only solution to my problem - so Ill use it.

    Tanks again.

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    10

    Default

    Sure, in absolute cases it's acceptable. But as in Java, there are many ways to perform a task... maybe there was another way to write your logic? I don't know, I'm just throwing that out there...
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:39 PM
  2. Replies: 2
    Last Post: 02-18-2008, 05:24 AM
  3. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  4. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM
  5. Nested Tags JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2007, 05:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •