Nat
Nat

Reputation: 908

&& and || in switch statement

I've tried doing some research on this topic but theres a suprisingly low amount of information. I have a switch statement, and I want a certain block to trigger if one of 2 conditions is true. I could just have identical blocks checking each condition, but it looks kind of redundant.

case KeyEvent.VK_NUMPAD0:
     //Identical Code
     break;

case KeyEvent.VK_INSERT:
     //Identical Code
     break;

Is there a way to combine the two statements to avoid repeating identical code?

Upvotes: 0

Views: 218

Answers (2)

This is known as FallThrough Look at the fallthrough demos in the docs

case KeyEvent.VK_NUMPAD0:
case KeyEvent.VK_INSERT:
     //Identical Code
     break;

Upvotes: 4

Graham Borland
Graham Borland

Reputation: 60691

case KeyEvent.VK_NUMPAD0:
case KeyEvent.VK_INSERT:
     //Identical Code
     break;

Upvotes: 7

Related Questions