No break in case 1 but why enters case 2 and also prints tuesday? Since Day equals 1 and is not equal 2 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

No break in case 1 but why enters case 2 and also prints tuesday? Since Day equals 1 and is not equal 2 ?

int day = 1; switch(day) { case 1: System.out.println("Monday"); case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; }

10th Mar 2019, 4:07 PM
αλφας βήτας
αλφας βήτας - avatar
4 Answers
+ 2
case 1: print monday (day = 1) case 1 has no break --> print tuesday case 2 has a break --> don't print wednesday If you want only one case (when day = 1) use break I am not sure if there exits good examples for a switch without breaks. The only example I know was to print the lyrics of a song. Input = verse Switch (verse) Case 1 ... Case 2 ... and so on Start where case = input --> print also the rest of all verses. (I think I saw it in "Java for Dummies")
10th Mar 2019, 4:31 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
This discussion should be useful for you: https://www.sololearn.com/Discuss/1694248/?ref=app
10th Mar 2019, 4:38 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
αλφας βήτας Yes. The break is only to stop the switch statement. day = 2 case 1: don't print (2 !=2) case 2: print (2 = 2) break (jump out of the switch statement) no break --> do the code inside case 3
10th Mar 2019, 4:48 PM
Denise Roßberg
Denise Roßberg - avatar
0
Yes.. i mean should day be equal to case in order to enter and execute ??? even without break?
10th Mar 2019, 4:41 PM
αλφας βήτας
αλφας βήτας - avatar