switch & case problem

my doubt is , why it is printing 2 & 3 . why is the use of then case keyword ( I know by using break keyword we can remove this problem ) but I want to the logic behind it .

thanks in advance . pooja .

:d

Liang Anmian

Ranch Hand

Posts: 119

posted 12 years ago

When you have multiple cases in a switch block, you are only indicating to the JVM where it should BEGIN execution. Once the JVM finds a matching case (meaning the JVM has found the correct entry point), it will just execute all the code below as normal, as though the case keywords are not there.

So to conclude, the "case" keyword is NOT meant for instructing the JVM to perform only this particular snippet of the switch block. It only serves as an entry point of the switch block. Nothing else. If you DO want to force the JVM to perform only this particular code snippet (meaning only statements linked to the matching case), the ONLY way is to use "break".