-2

Can I use the

goto or fall-through

in a Switch statement in Java like you can in C#?

Here is an example of the code I modified in C# in order to use the goto statement in a conditional Switch, pay attention where it says SAM:

      // add 1 to appropriate counter for specified grade
       private void IncrementLetterGradeCounter( int grade )
       {
          // determine which grade was entered
          switch ( grade / 10 )
          {
             case 9: // grade was in the 90s
             case 10: // grade was 100 
                ++aCount; // increment aCount
                    goto case 8; // SAM: in this case I omitted the break 
                //by commenting and use the "goto case <number>;" 
                //which let me continue without errors
                //break; // necessary to exit switch
             case 8: // grade was between 80 and 89
                ++bCount; // increment bCount    
                break; // exit switch
             case 7: // grade was between 70 and 79
                ++cCount; // increment cCount    
                break; // exit switch
             case 6: // grade was between 60 and 69
                ++dCount; // increment dCount    
                break; // exit switch
             default: // grade was less than 60
                ++fCount; // increment fCount       
                break; // exit switch
          } // end switch
       } // end method IncrementLetterGradeCounter
S. Mayol
  • 2,565
  • 2
  • 27
  • 34
  • **Please** tell me that isn't legal c#. – Elliott Frisch Feb 28 '18 at 04:53
  • What do you want to achieve here? Increment both a and b counts when you get a 9 or a 10? – Mick Mnemonic Feb 28 '18 at 04:54
  • @ElliottFrisch Unfortunately, it is. – Ron Beyer Feb 28 '18 at 04:57
  • Elliott goto is legal in C#, the code that I posted runs with no issue in C#, however if I do not specify the goto and I do not have the break which I comment it, it will case an error: "Control cannot fall through from one case label". I just want to know if there is something equivalent to do in Java. I provided the code as example. – S. Mayol Feb 28 '18 at 05:03
  • Mick the code as it is case 9 and case 10 is fine, I just post it as example to see if somebody know something equivalent in Java like the goto in C# or fall-through statement. – S. Mayol Feb 28 '18 at 05:04
  • @S.Mayol I don't think so. And that's hideous. Please don't do that. Even if it's legal. [Go to statement considered harmful](https://en.wikipedia.org/wiki/Considered_harmful) is very nearly 50 years old now. – Elliott Frisch Feb 28 '18 at 05:07
  • this maybe the answer to your question try to read that first, you cant do that but you can reuse the switch by calling a functions from the cases. https://stackoverflow.com/questions/31498571/can-we-call-a-case-inside-another-case-in-the-same-switch-statement-in-java – Adiro Feb 28 '18 at 04:58
  • I know 'goto' is a 'reserved keyword' in Java, and it is an unused keyword. Meaning that cannot be used in code for names of variables or other constructs. My purpose here was to find any help providing me the equivalent of 'goto' or 'fall-through' using C# Switch statement. I did a search and found these links: http://www.blackwasp.co.uk/CSharpGoto.aspx http://www.dummies.com/programming/java/how-to-use-fall-through-to-your-advantage-in-java-programming/ – S. Mayol Jul 02 '19 at 12:49

1 Answers1

0

goto is not used in Java, though the Keywords list does have an entry for goto, it is not used. Instead, you can use break or continue

  • Or neither, if you want to fall through. – Kevin Krumwiede Feb 28 '18 at 04:55
  • I know 'goto' is a 'reserved keyword' in Java, and it is an unused keyword. Meaning that cannot be used in code for names of variables or other constructs. My purpose here was to find any help providing me the equivalent of 'goto' or 'fall-through' using C# Switch statement. I did a search and found these links: http://www.blackwasp.co.uk/CSharpGoto.aspx http://www.dummies.com/programming/java/how-to-use-fall-through-to-your-advantage-in-java-programming/ – S. Mayol Jul 02 '19 at 12:48