• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

For Loops Continue Break;

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80091
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Cool ?
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lee Kian Giap wrote:Cool ?


 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80091
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 5088
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80091
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree: an "if" is a neat way to avoid "continue" and is fully compliant with structured programming.
 
You've gotta fight it! Don't give in! Read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic