• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner loop in switch  RSS feed

 
Gil Shoam
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

 
Jeff Verdegan
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java 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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • 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.
 
Joanne Neal
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: 56570
172
  • 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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate 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: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What’s Duff’s device? Is this it?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate 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: 21135
87
Chrome Eclipse IDE Java 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: 10575
66
Eclipse IDE Hibernate 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
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!