So i am doing a simple menu in main class where it has 10 options from 0 to 9, i am using a switch case to get the option and then execute a certain code, and number 9 is to do all the options in the menu. How can that if option is 9, it does me all the cases before.

public static void main(String[] args) {
    switch (option) {
        case 1:
            do A;
            break;
        case 2:
            do B;
            break;
        case 3:
            do C;
            break;
        case 4:
            do C;
            break;
        case 5:
            do C;
            break;
        case 6:
            do C;
            break;
        case 9:
            do case 1 ;
            do case 2 ;
            do case 3 ;
            do case 4 ;
            do case 5 ;
            do case 6 ;
    }
}

I am expecting that when option is 9, it executes all the cases before;

2 Answers

2
Synergi On

There are various ways to achieve what you need. I'll outline a few.

Use methods

This is simple, and somewhat clean and easy to maintain. All you need to do is wrap the code you would like to execute in each case in a separate method then call those methods from within the switch. This is exactly what functions are for.

switch(option){
    case 1:
        doA();
        break;
    case 2:
        doB();
        break;
    ...
    // other cases
    ...
    case 9:
        doA();
        doB();
        ...
        // other method calls
        ...
        break;
}

Switch to if statements

This is pretty self explanatory, just check if the option is each different case or option 9.

if(option == 1 || option == 9){
    do A;
}
if(option == 2 || option == 9){
    do B;
}
...
// other cases
...

(Mis)use breaks

This is fairly ugly and I wouldn't recommend it but it's really up to personal preference (and how easy to read and maintain you want the code to be in the future).

If option is 9, then we flip a flag to turn off all breaks in the switch statement. This effectively makes all other cases below it just execute linearly (as the breaks to leave the switch are disabled).

boolean isCase9 = false;
switch(option){
    case 9:
        isCase9 = true;
    case 1:
        doA();
        if(!isCase9){
            break;
        }
    case 2:
        doB();
        if(!isCase9){
            break;
        }
    ...
    // other cases
    ...
0
galando On

I suggest defining a method (let's call it switchMethod) with the switch case - when all options are only 0 to 8.

public void switchMethod(option) {
   switch (option) {
   case 1:
       do A;
       break;
   case 2:
      do B;
      break;
   case 3:
      do C;
      break;
   ....
   case 8:
      do X;
      break;
  }
}

In the main:

if (option < 9) {
    switchMethod(option)
} else {
   for (int i = 0; i <9; i++)
       switchMethod(i)
}

That way you won't write duplicate code and it will do the logic you requested.

Another way is doing duplicate code like that:

switch (option) {
    case 1:
        do A;
        break;
    case 2:
        do B;
        break;
    case 3:
        do C;
        break;
    case 4:
        do C;
        break;
    case 5:
        do C;
        break;
    case 6:
        do C;
        break;
    case 9:
        do A;
        do B;
        do C;
        ....
        break;
}