This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For Loops Continue Break;

 
Nolan Bradshaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that you are asked to do the same thing, without using "continue", right ? What about if ( count != 5 ) {...} inside the for loop ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nolan Bradshaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Cool ?
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Kian Giap wrote:Cool ?

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: an "if" is a neat way to avoid "continue" and is fully compliant with structured programming.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic