How to print all the cases | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to print all the cases

public class Program { public static void main(String[] args) { int day = 3; switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; } } }

15th Nov 2019, 7:06 AM
anurag rajpoot
anurag rajpoot - avatar
12 Answers
+ 3
Remove case 1 and case 2 and its break, then copy and paste the Monday and tuesday on case 3 Then you will get a output
16th Nov 2019, 4:18 AM
Gouse Basha
Gouse Basha - avatar
+ 4
Hi. Remove break and all cases after matched will be printed.
15th Nov 2019, 7:08 AM
Dmytro Novak
Dmytro Novak - avatar
+ 3
Remove `break` statement from all sections, then you will see all the day names.
15th Nov 2019, 7:08 AM
Ipang
+ 2
What did you mean it won't work? that's odd. Well, can you post your code without the `break` statements? also tell if you got error message; it helps to isolate the issue 👍
15th Nov 2019, 7:13 AM
Ipang
+ 2
anurag rajpoot Yes this last code works. The initial code in your original question actually works, just not as you wanted it to. And it was the `break` statement that cause only one (the matching choice) is printed. Because `break` takes you out of the switch block ( between the { and the } ). Without the `break` statement, all the cases will be evaluated, no matter whether they match the condition or not. Hope this clears your doubt 👍
15th Nov 2019, 7:29 AM
Ipang
+ 1
public class WhatsAppClone { public static void main(String[] args) { int winner = 1; switch(winner) { case 1: System.out.println("🏆Sick Line B̶r̶o̶🌡️[#Reincarnated] "); case 2: System.out.println("🏆JaseemAkhtar "); case 3: System.out.println("🏆viknesh vikky"); case 4: System.out.println("🏆Saiful Alam Piyal "); case 5: System.out.println("🏆Farhan🏁[Traveling] "); default: System.out.println("\n🎊🎉 Congtrasulation to all winners and participants...🎊🎉 \n & Nice code Aakaanksha💕 sis..🌹"); } } }
15th Nov 2019, 7:14 AM
anurag rajpoot
anurag rajpoot - avatar
+ 1
Ok And thank you
15th Nov 2019, 7:30 AM
anurag rajpoot
anurag rajpoot - avatar
+ 1
Without break statement, first matching case evaluated, "from there onwards fall through occurs" means next all cases will be evaluated, no matter whether they match condition or not. In your 1st code, if you remove break, case 3 executed, because day=3, there are no more cases after that hence comes out switch, if you give day 1, without break all cases will be executed. In second code, you given value 1, first case matches, and no breaks, hence all cases will executed..
15th Nov 2019, 8:40 AM
Jayakrishna 🇮🇳
0
Ipang i have tried removing break but it wont work
15th Nov 2019, 7:11 AM
anurag rajpoot
anurag rajpoot - avatar
0
Chek this
15th Nov 2019, 7:14 AM
anurag rajpoot
anurag rajpoot - avatar
0
This one is working without brek but the above code which i gave in starting is not working
15th Nov 2019, 7:14 AM
anurag rajpoot
anurag rajpoot - avatar
17th Nov 2019, 7:00 AM
PANKAJ NAIK
PANKAJ NAIK - avatar