+ 2

Break keyword in Switch statement

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner number = new Scanner(System.in); int day = number.nextInt(); switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); case 3: System.out.println("Wednesday"); break; } } } for instance i want to put the input as 2, and there's no "break" in case 2. I thought it will only print the case which is the same as the expression we want so why the 3rd case is printed too even tho it's not the same??

10th Aug 2020, 3:57 AM
M.O.HONOR
M.O.HONOR - avatar
2 Antworten
+ 3
When you don't include"breaks" then computer does not know when to Break... So it just continue down the cases until it finds the keyword "breaks" that's the reason why computer printed case 3...
10th Aug 2020, 4:11 AM
KingDaniel2004
KingDaniel2004 - avatar