Similar case in switch with little differences

switch case
switch statement
switch case c
r case switch vs if-else
javascript switch multiple case
difference between if and switch
javascript switch case range
switch case c++

I want to execute a switch case where the case's body is similar with another's. I split the two cases and the editor informed that it is a similar case with another one and had the option of merging with the other. When I did the first case was:

...
case 3: {
   case 7:
      System.out.println("Print something: ");
   ...
}

However, I want at the case of 3 to print a different message than the one at case 7.

...
case 3: {
   System.out.println("Print something different");
   case 7:
      System.out.println("Print something: ");
   ...
}

But, when case 3 is executing both messages are printed, whereas when case is 7 only the message of case 7 is printed. Is there any way to print only the 3rd's when case is 3?

If you want the two cases (3 and 7) to perform common logic, with the only difference being the printed output, you can add a condition:

case 3:
case 7: {
   System.out.println(v == 3 ? "something" : "something else");
   ... common code that should run for both 3 and 7 cases ...
}

Where v is the switch statement variable.

CASE Versus SWITCH, The CASE and SWITCH statements are similar in function, but differ in the If there are no matches within a CASE statement and there is no ELSE clause, IDL � The CASEand SWITCHstatements are similar in function, but differ in the following ways: Execution exits the CASE statement at the end of the matching statement. By contrast, execution within a SWITCH statement falls through to the next statement. The following table illustrates this difference:

I want the body of 3 and 7 to be the same, thus executing for both 3 and 7, but when the case is 3, print only the first message and not the one of case 7 too..

String stuff ="";

switch (test){
   case 7: stuff = "something Else.\n";  //flow down to 3
   case 3: stuff = "print something.\n" + stuff;  //prepend with print something.
           break;
}

System.out.print(stuff);

That said... this way of coding is not really intuitive for the coder. you are better off just separating both cases with a break.

String stuff ="";

switch (test){
   case 7: stuff = "something Else.";   
           break;
   case 3: stuff = "print something.\nsomething Else.";   
           break;
}

System.out.println(stuff);

The "switch" statement, If no case is matched then the default code is executed (if it exists). Here the switch starts to compare a from the first case variant that is 3 . If I want to create a menu with options, so e.g If I have two cases, A and B, within those they have the option to write C. A C and BC as input should be different things, and not conflict.

After each statement, add a break. Otherwise, it will execute both.

        case 3:
            System.out.println("Print something different");
        case 7:
            System.out.println("Print something: ");
            break;
   ...

What is the difference between switch and if else statements? Which , Let's see some differences below, IF-ELSE * statement will be executed It uses a boolean expression to decide which of the alternative should be executed. of the two statements, and the difference between switch case and else if ladder. The use of break statement in switch is essential but there is no need of use of� "Soft" means the case is mostly fabric. "Semi-hard" means the case is tougher, but can flex a little bit. "Hard" means the case doesn't flex. Note that this guide is just a reference. Please make sure you do your research on the case you want, since I can't be liable if you lose money or get a crappy case that breaks your Switch or something.

The JavaScript Switch Statement (With Examples), The switch case statement may trip up even the most seasoned Today I am sharing a similar article on how the switch statement works in JavaScript. Use the The comparison value will match either a statement value or trigger a default code block. If no cases match, the default code block is executed. Switch Case: The switch case statement is similar to the else-if ladder as it provides multiple branching or multi-conditional processing. But, the basic difference between switch case and else if ladder is that the switch case statement tests the value of variable or expression against a series of different cases or values, until a match is found. Then, the block of code with in the match case is executed.

Best Practice discussion: Difference between SWITCH and IF/ELSE , Aside from that, switch comes with a few built-in safety features: If statements are very practical in many cases, and switch isn't here to� Compare systems. It's always fun to have options. Learn more about the Nintendo Switch and Nintendo Switch Lite systems to see which one (or two) works for you.

switch vs if else, If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too. If a� Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too. If a switch contains more than five items, it’s implemented using a lookup table or a hash list. This means that all items get the same access time, compared to a list of if:s where the last item takes much more time to reach as it has to evaluate every

Comments
  • With break under print statement of case 3, results in not executing the body under those lines. So it will only print "Print something different"
  • Extract the common (repeated) code to a method and call it inside the cases.
  • Yes, thank you! I thought I tried that but the switch statement variable was a function called, so I had to now store it first.
  • Thank you for replying. If I use break the rest of the case 3 won't be executed when case is 3. Sorry if I wasn't clear; I want the body of 3 and 7 to be the same, thus executing for both 3 and 7, but when the case is 3, print only the first message and not the one of case 7 too..
  • Nope. The 2 prints showing there; one is for when case is 3 and the other for when case is 7. The body of both cases is the same, which will be under the lines I wrote. But when case is 3, only the printing under case 3: must be printed, but both are now.
  • @DemetrisShimitras It is unclear what you are trying to achieve. It is either case 3 or 7. It can't be both.
  • I believe the code provided by @Eran achieves what you need.