Results 1 to 12 of 12
  1. #1
    vsb
    vsb is offline Member
    Join Date
    Oct 2011
    Posts
    17
    Rep Power
    0

    Default How to come out of For loop.

    Here is my scenario, and I should come out of For loop based on some condition:
    Please advise.

    My Array 'tarr' with 10 cells.
    cell1=2
    cell2=6
    cell3=0
    cell4=10
    cell5=0
    cell6=15
    cell7=1
    cell8=0
    cell9 ......



    My code:

    For (rc=0; rc<=tarr.length-1;rc++)
    {

    ... some code...

    Switch(somevar) // this somevar holds some numeric values.....

    case 1:
    ... do something ..
    break;

    case 2:
    ... do something ..
    break;

    case 0:
    ... do something ..
    break;

    --- here, whenever case =0, then I want to come out of this For loop entirely ---
    --- Even I put 'break' case 0, as I put the condition rc<=tarr.length, this for loop iterates until the array's length.
    I just want to come out of this FOR loop, so I will do something.


    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to come out of For loop.

    Read a Java textbook on labeled breaks and continues.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: How to come out of For loop.

    Jos, do you really think that is helpful? I'm pretty new myself, not as new as the OP, but it is easy to get confused when you're just starting...even when you're trying.

    OP, your break statements are for the switch NOT the for-loop. If you want to exit the loop after some case is met, you will have to change things. For-loops are for a certain number of times (such as array-length). Check out other loop options.

  4. #4
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: How to come out of For loop.

    Also, try and use code tags please...

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to come out of For loop.

    Quote Originally Posted by joeyvitoro View Post
    Jos, do you really think that is helpful?
    Yes, I do think it is helpful as long as the OP is willing to read. It is the solution to the OPs problem.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    vsb
    vsb is offline Member
    Join Date
    Oct 2011
    Posts
    17
    Rep Power
    0

    Default Re: How to come out of For loop.

    Thanks all for your help.
    I would like to know what is 'OP'.

    I need some help regarding a single variable which is used inside 2 loops/conditions.

    1. For
    2. Inside of For is While loop

    In the For loop,I have one local variable/any static variable which is in ANOTHER class

    Let us assume int x started with '0'. And went into While.
    Even after this (one) iteration, when I came out of while, x value still at '0'. The value is just 0, always. I think While condition creates this issue. Am I correct?
    I observed the same with VB Script as well.

    Is there anything that I can use to overcome this issue.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to come out of For loop.

    Quote Originally Posted by vsb View Post
    I would like to know what is 'OP'.
    Original Poster/Original Post depending upon the context.

    I think While condition creates this issue. Am I correct?
    No idea without seeing the code.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to come out of For loop.

    Have a look at the following example snippet; it uses a labeled break.

    Java Code:
    public class T {     
    
    	public static void main(String[] args) {        
    		loop:
    		for (int i= 0; i < 10; i++)
    			switch (i) {
    			case 1: System.out.println("one"); break;
    			case 2: System.out.println("two"); break;
    			case 3: System.out.println("three"); break loop;
    			default: System.out.println("default");
    		}
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Re: How to come out of For loop.

    JosAH provided a perfectly valid example, but... You should really think about the overall structure of what you are doing. Using "break" to get out of the "for" loop while in a "switch" just looks nasty. How about this? Check for the condition before you enter the "switch". Use "break" appropriately. Much cleaner. Labeled break is a valid solution, but if you use it, be VERY careful to document it, as it can be hard to see.
    The Java Tutorial. Read it.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to come out of For loop.

    Quote Originally Posted by Steve11235 View Post
    JosAH provided a perfectly valid example, but... You should really think about the overall structure of what you are doing. Using "break" to get out of the "for" loop while in a "switch" just looks nasty. How about this? Check for the condition before you enter the "switch". Use "break" appropriately. Much cleaner. Labeled break is a valid solution, but if you use it, be VERY careful to document it, as it can be hard to see.
    I think it's only much cleaner in the eye of the beholder; the Java language offers the possibility of labeled breaks and labeled continues, so why not use it if/where applicable? Another example would be those while() { } and do ... while() loops: why use them if we have a for loop? ímho, it´s just because people are not used to a certain language feature that gives them a reason not to use it, because it ´looks funny´ ... Of course a bit of comment here and there never harms.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Re: How to come out of For loop.

    Quote Originally Posted by JosAH View Post
    I think it's only much cleaner in the eye of the beholder; the Java language offers the possibility of labeled breaks and labeled continues, so why not use it if/where applicable? Another example would be those while() { } and do ... while() loops: why use them if we have a for loop? ímho, it´s just because people are not used to a certain language feature that gives them a reason not to use it, because it ´looks funny´ ... Of course a bit of comment here and there never harms.

    kind regards,

    Jos
    I'm sorry. I accidentally criticized your code; I didn't mean to. I use sometimes use labeled breaks to get out of nested loops, and I sometimes get negative feedback as well. It's in the language for a reason. What I meant to say is: Handle exceptional situations first, and then perform normal processing. But, labeled breaks should still be well documented, as they are easy to miss.
    The Java Tutorial. Read it.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to come out of For loop.

    Quote Originally Posted by Steve11235 View Post
    I'm sorry. I accidentally criticized your code; I didn't mean to. I use sometimes use labeled breaks to get out of nested loops, and I sometimes get negative feedback as well. It's in the language for a reason. What I meant to say is: Handle exceptional situations first, and then perform normal processing. But, labeled breaks should still be well documented, as they are easy to miss.
    No need to apologize; I like my code being criticized (in a reasonable manner). I agree that seldom used code constructs should be commented but it shouldn't be avoided because it is used so rarely and especially not when it is (one of) the most efficient ways to accomplish something.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  2. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  3. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 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
  •