1

How does a switch statement work? Is it just like a goto statement? or Does it go for each case and verifies which is case is true? And then executes true statement?

int month = 2;
int year = 2000;
int numDays = 0;

switch (month) {
  case 1: case 3: case 5:
  case 7: case 8: case 10:
  case 12:
            numDays = 31;
            break;

  case 4: case 6:
  case 9: case 11:
            numDays = 30;
            break;

  case 2:
  if (((year % 4 == 0) && !(year % 100 == 0))|| (year % 400 == 0))

            numDays = 29;
            else
            numDays = 28;
            break;
   default:
           System.out.println("Invalid month.");
           break;
        }

           System.out.println("Number of Days = " + numDays);
        }

In the code we can see that inside parentheses'()' we have given switch a variable month.

I wanted to know if user inputs 2 for month, then what happens.

Does it directly jump to case 2 or does it evaluate all preceding cases i.e. (case 1: case 3: case 5: case 7: case 8: case 10: case 12: case 4: case 6: case 9: case 11:) and then it finds case 2 and executes it?

What does it do?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Geek Online
  • 53
  • 1
  • 8
  • Your code looks completely fine to me. Have you tried running it? If you test yourself it would be more effective than asking a question here. – Tim Biegeleisen Oct 06 '18 at 13:50
  • 4
    The workings of a switch statement is fully described [in the language spec](https://docs.oracle.com/javase/specs/jls/se9/html/jls-14.html#jls-14.11). No need to repeat it here. – Andy Turner Oct 06 '18 at 13:52
  • I am asking how will it run in stack ? how the transfer of control will occur – Geek Online Oct 06 '18 at 13:53
  • Just like a goto, it goes to the correct label and runs from there. – matt Oct 06 '18 at 13:54
  • @matt "Otherwise, execution continues by comparing the value of the Expression with each case constant" suggests otherwise. – Andy Turner Oct 06 '18 at 13:55
  • Are you sure @matt?? i have risked all on this that it works as a goto statement – Geek Online Oct 06 '18 at 13:56
  • @AndyTurner (and op) I am missing the distinction, they're constant expressions so it isn't going to evaluate any code. – matt Oct 06 '18 at 13:57
  • 2
    Some samples output from java compiler https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10 – Charles Oct 06 '18 at 13:58
  • @matt let's say you had (in this order) `case 0:`, then `case 12345:`, then `default:`, then `case -1:`: how would you "jump" to one of those, without comparing to each case in turn? – Andy Turner Oct 06 '18 at 13:59
  • I haven't tried but I believe another `case` after `default` is not legal in Java. – Christoffer Soop Oct 06 '18 at 14:04
  • @AndyTurner By successive comparison of the case to the labels and then begins running from there. I think you're distinguishing how an actual goto works, with a jump table, vs I was trying to say it acts like a goto and will find the correct label and run from there. I suppose if you're using something like a String then .equals code will be called. – matt Oct 06 '18 at 14:04
  • 1
    @AndyTurner It really depends, if I'm not mistaken, the compiler can also generate a lookup / jump table. IIRC this can depend on the size of the switch, the datatypes involved and maybe some aspects of the constants used. On top of that, the JIT might do some additional magic at runtime. – Mark Rotteveel Oct 06 '18 at 14:05
  • @ChristofferSoop https://ideone.com/0EKO8X. It's legal because Java requires all case labels to be distinct, so only one case can match. – Andy Turner Oct 06 '18 at 14:07
  • The comment I replied to changed while I typed it, but given @AndyTurner s paradox, it will take the correct case and run from there. So if it doesn't match any of the other cases, then it will run from the default. – matt Oct 06 '18 at 14:11
  • What do you mean about 'evaluating cases'? How does this have an effect on what you're trying to achieve. – matt Oct 06 '18 at 14:15
  • @AndyTurner thanks, I learned something new today! This said I would never let it pass a code review given the Java conventions I'm using... In particular if you splice the `default` somewhere in the middle instead of the end. – Christoffer Soop Oct 06 '18 at 14:26
  • 1
    @ChristofferSoop I generally agree; but never say never. Whilst I have never used it in this way, I can imagine it being useful if you want to fall through from the default case to other cases. – Andy Turner Oct 06 '18 at 14:32

1 Answers1

0

I think the answer is it depends. In this particular example a tableswitch would be performed and it would jump to the particular case you are looking for.

There are 2 ops the switch statement can become a tableswitch and lookupswitch

The differences of these 2 can be found at Difference between JVM's LookupSwitch and TableSwitch?

This can also be found in the spec from oracle

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10

Where the cases of the switch are sparse, the table representation of the tableswitch instruction becomes inefficient in terms of space. The lookupswitch instruction may be used instead.

Charles
  • 944
  • 5
  • 9