0

It prints i= 5 when run, can you help me understand why it prints 5. also, case 'c'|'d' : is unreachable. it dosen't print anything for that.

        var ca = new char[]{'a', 'b', 'c', 'd'};
        var i = 0;
        for (var c : ca) {
            switch (c) {

                case 'a':
                    i++;
                    System.out.println("Case a : " + c + " : " + i);

                case 'b':
                    ++i;
                    System.out.println("Case b: " + c + " : " + i);

                case 'c'|'d' :
                         i++; 
                         System.out.println("Case c or d : "+c + " : "+i);
            }

The code shows following output.

Case a : a : 1

Case b: a : 2

Case c or d : a : 3

Case b: b : 4

Case c or d : b : 5

5

it means, in array, 3rd and 4th elements were never processed. Why??

  • What value do you expect `c` to assume to trigger the `case 'c'|'d' :` part? – Federico klez Culloca Jul 20 '22 at 08:07
  • 2
    More generally, you should re-read about [how the switch statement works](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) – Federico klez Culloca Jul 20 '22 at 08:08
  • To be able to read, understand and maintain code easily, consistency is very important. Both when it comes to indentation but also when you want to do seemingly simple things like increasing the value of a variable. Should you use `++i`, or `i++`? That doesn't really matter, but please pick one way and stick with it, in a consistent manner. – Some programmer dude Jul 20 '22 at 08:14
  • "it means, in array, 3rd and 4th elements were never processed. Why??" <- Not true. They were processed. You just don't have a fitting case for them. – OH GOD SPIDERS Jul 20 '22 at 09:00

1 Answers1

1

With 'c'|'d' you do a bitwise or between the two characters. That makes no sense and the result (which will be used for the case) will not be what you expect.

To use both for a case, remember that cases fall through if there's no break, which means you can do:

case 'c':
case 'd':
    // Code for both 'c' and 'd'...
    break;

And as noted, since your currently shown code have no break statements at all. The case for 'a' will fall through to 'b' and on to the 'c' and 'd' cases.

You need to spend more time with your text-books or tutorials to learn more about how switch works.


With the code as currently shown in the question, then you loop will use 'a' from the array, which will cause the following statements to execute:

i++;
System.out.println("Case a : " + c + " : " + i);
++i;
System.out.println("Case b: " + c + " : " + i);
i++;
System.out.println("Case c or d : "+c + " : "+i);

That increases i from 0 to 3.

Then the loop iterates and we use 'b' which leads to the statements:

++i;
System.out.println("Case b: " + c + " : " + i);
i++;
System.out.println("Case c or d : "+c + " : "+i);

That increases i two more times, from 3 to 5.

And since there's no cases matching 'c' or 'd' the loop will end with i equal to 5.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Also, there are no `break`s between the various `case`s – Federico klez Culloca Jul 20 '22 at 08:10
  • I know that we need to use break statement,and case should be used as you shown... but , I just want to understand how it prints i=5 and why 'c'|'d' is unreachable. it was asked in my OCP Exam. – Zarna Chirag Jul 20 '22 at 08:44
  • @ZarnaChirag: It isn't unreachable: https://ideone.com/fLUhx9 - In fact because of the missing break statements that case will also be reached when `case 'a':` or `case 'b':` are executed. And if you want a deep step-by-step explanation what exactly happens in the code and how it reaches the output of i=5: [That is what debuggers are for](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – OH GOD SPIDERS Jul 20 '22 at 08:58
  • @ZarnaChirag Added a simple explanation about the statements that will be executed, leading to `i` becoming equal to `5`. And since `('c'|'d' != 'c') && ('c'|'d' != 'd')` that case will never be executed for `'c'` or `'d'`. – Some programmer dude Jul 20 '22 at 09:24
  • Thanks a lot , Some programmer dude.. – Zarna Chirag Jul 20 '22 at 15:54