wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes For Loops Continue Break; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For Loops Continue Break;" Watch "For Loops Continue Break;" New topic
Author

For Loops Continue Break;

Nolan Bradshaw
Greenhorn

Joined: May 13, 2010
Posts: 3
I'm am currently attending college and taking a java programming class. I am somewhat stumped. I get thrown in a situation and need to figure out the answer, now I do have some programming experience and in this case, my thinking would be to use a iterator. I do not want an answer, just a some advice in the right direction.



Now ultimately, I was thinking of using a iterator and use iter.remove() and remove the 5. The object is to redo this code without using a continue. There has to be another way of going about it besides using a iterator.

I know the iterator is not the answer simply for the fact that we have not learned them yet. Now One of my solution was to create two loops and just skip 5. That seems like the hard way, there has to be away to remove count increment easier..

there has to be a way to make it so it removes a counter.

*Keep in mind that arrays have not been learned yet either*

My current solution


Any other ideas?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I understand that you are asked to do the same thing, without using "continue", right ? What about if ( count != 5 ) {...} inside the for loop ?

[My Blog]
All roads lead to JavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Have you learned "if" ? Because then inside the loop, you could test to see if "count" is 5, and only print if it's not.


[Jess in Action][AskingGoodQuestions]
Nolan Bradshaw
Greenhorn

Joined: May 13, 2010
Posts: 3
Well, I'm silly, I can't believe I spaced that. I've been known to think to complex this would be one of those times.
Thanks guys.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
You can also add something like "&& count < 5" to the middle part of the for loop header.

I firmly believe you should avoid "break" (except after "case") and "continue". But I haven't the time to discuss why, I am afraid.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

For an opposing viewpoint, I think break and continue are fine when the situation warrants them. That said, my loop bodies are almost always very small, so the cognitive overhead is minimal.

Heck, I've even been known to use a break with a label from time to time.
Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
Hi,

on the basis of if condition you can give break or continue.

The purpose of break is to return out from loop at that point.

And for continue is it will not execute code which is after continue .it will go directly to for loop.


Prabhat Shankar Consumer Court,Consumer Court India,Consumer Complaints,Complaint India, Web Value
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213


Cool ?


SCJP 6, SCWCD 5, SCBCD 5
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
Campbell Ritchie wrote:You can also add something like "&& count < 5" to the middle part of the for loop header.


Added this statement will stop the loop from reaching 10 ... not skip the 5 ... hope I'm right
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Lee Kian Giap wrote:Cool ?

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Lee Kian Giap wrote:Cool ?


This is the kind of stuff you get when you try to avoid using break and continue -- precisely the reason why I think break and continue, used responsibly, are fine.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Lee Kian Giap wrote:

Cool ?

Do you find this clear? readable?

Do you think that someone who didn't set this up would be able to decipher its intent by looking at it? Will you know what it's doing when you look at in six months?

(Hint: if you were on my team and I saw such code, we'd be having a closed-door discussion.)

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
Bear Bibeault wrote:
(Hint: if you were on my team and I saw such code, we'd be having a closed-door discussion.)


haha , I don't mind if you really want to have closed-door discussion. Come on expert, it is just an idea to cater the requirements from the first post. Definitely when doing real world project, there will not have such requirements on how to write code, but one programmer must try the best to write readable, maintainable, reusable, high cohesive, loosely couple .... etc. property and behavior encapsulation ... (you can argue till the end on how much you want to state ... even until Inversion of Control, Dependency Injection, Aspect ... or even until design pattern)

you want another code which does not catering for the first post ???



Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
Bear Bibeault wrote:
Do you find this clear? readable?

Do you think that someone who didn't set this up would be able to decipher its intent by looking at it? Will you know what it's doing when you look at in six months?



Clear but Not Readable.

If the programmer in your team can understand it, they need to refactor it. If they can't understand it, you need to change your programmer, a programmer who doesn't prepared well to handle/refactor ugly code ... I have no idea why they become programmer ... or they think the world is so ideal.

And the programmer who write this unreadable code need to be fired ... and there is no time for you to read all the code developed. You can use Checkstyle, PMD, FindBug and other plugins to help you. All The Best
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Lee Kian Giap wrote:Added this statement will stop the loop from reaching 10 ... not skip the 5 ... hope I'm right
Yes, you are correct.

Just an example of how to avoid "break" or "continue", but "continue" is harder to avoid. As you have seen from the examples shown over the weekend to try to avoid it
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Well, except for the option suggested early on by Christophe and EFH. That's a very easy way to avoid continue in this case.

Personally I see nothing wrong with using break or continue in moderation, where warranted. But it's also a useful exercise for students to force themselves to find alternatives too, just to learn flexibility.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Agree: an "if" is a neat way to avoid "continue" and is fully compliant with structured programming.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For Loops Continue Break;