• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

inner loop in switch

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to do an inner loop using the switch option.
I want to do something like this:

I know I can just copy paste the code.. but is there a more elegant way to do it?

Thanks

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the code in a method.
 
Gil Shoam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Put the code in a method.



isn't there a "redo case(a)"
or something similar?
 
Sheriff
Posts: 67754
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
No.

Methods are the accepted way to create reusable code, why do you need something different?
 
Sheriff
Posts: 22853
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you need to redo case(a) twice if both conditions are true? If not you can reorder your case statements and use fall through:
I'd still prefer a separate method though.
 
Marshal
Posts: 80764
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote: . . . I'd still prefer a separate method though.

Agree. Fall-through will confuse the next person who reads that code.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob - that code is wrong for when somethingElse is true - notice that OP wanted to redo case b in that situation.
 
Campbell Ritchie
Marshal
Posts: 80764
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Rob - that code is wrong for when somethingElse is true - notice that OP wanted to redo case b in that situation.

so it confused the first person who read the code too?

Edit: change last to first
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Rob Spoor wrote: . . . I'd still prefer a separate method though.

Agree. Fall-through will confuse the next person who reads that code.


Seconded. I think they could have saved us all a lot of bother (and break statements) by just making each section an independent block. After all, you can't use Duff's device, which is why a lot of us old C-ers liked the original syntax.

Winston
 
Campbell Ritchie
Marshal
Posts: 80764
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What’s Duff’s device? Is this it?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What’s Duff’s device? Is this it?


Yup.

Winston
 
Rob Spoor
Sheriff
Posts: 22853
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Rob - that code is wrong for when somethingElse is true - notice that OP wanted to redo case b in that situation.


You're very right, I missed that. Another reason to use methods
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gil Shoam wrote:I know I can just copy paste the code.. but is there a more elegant way to do it?


I hesitate to suggest this, because you need to be careful and it is rather brittle; but you could use recursion, viz:
Winston
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess i make it unanimous. method calls are the best way to go. i have written quite a few if-else and switch statements and put all the code in them. most of the time when i read them later i see i should have created a method or two and called them instead
reply
    Bookmark Topic Watch Topic
  • New Topic