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; } } }
12 Respuestas
+ 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
+ 4
Hi. Remove break and all cases after matched will be printed.
+ 3
Remove `break` statement from all sections, then you will see all the day names.
+ 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 👍
+ 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 👍
+ 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..🌹");
}
}
}
+ 1
Ok
And thank you
+ 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..
0
Ipang i have tried removing break but it wont work
0
Chek this
0
This one is working without brek but the above code which i gave in starting is not working
0
This may be helpful👇👇 https://code.sololearn.com/cFUl436eGRqi/?ref=app