Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

inner loop in switch

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Put the code in a method.
 
Gil Shoam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Put the code in a method.



isn't there a "redo case(a)"
or something similar?
 
Marshal
Posts: 67415
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

Methods are the accepted way to create reusable code, why do you need something different?
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 69773
277
  • Mark post as helpful
  • send pies
  • 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
  • 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: 69773
277
  • Mark post as helpful
  • send pies
  • 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: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 69773
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What’s Duff’s device? Is this it?
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Yup.

Winston
 
Rob Spoor
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic