Results 1 to 12 of 12
Thread: How to come out of For loop.
- 10-25-2011, 06:03 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
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.
}
- 10-25-2011, 07:35 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: How to come out of For loop.
Read a Java textbook on labeled breaks and continues.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-25-2011, 08:01 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
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.
- 10-25-2011, 08:02 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: How to come out of For loop.
Also, try and use code tags please...
- 10-25-2011, 08:31 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 10-26-2011, 05:32 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
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.
- 10-26-2011, 05:43 AM #7
- 10-26-2011, 07:46 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: How to come out of For loop.
Have a look at the following example snippet; it uses a labeled break.
kind regards,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"); } } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-26-2011, 07:03 PM #9
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-27-2011, 07:41 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: How to come out of For loop.
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-27-2011, 06:33 PM #11
Re: How to come out of For loop.
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.
- 10-27-2011, 06:53 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: How to come out of For loop.
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
A loop that doesn't loop
By MichYer in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks