How does a switch statement work? Is it just like a goto statement? or Does it go for each case and verifies which is case is true? And then executes true statement?
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1: case 3: case 5:
case 7: case 8: case 10:
case 12:
numDays = 31;
break;
case 4: case 6:
case 9: case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) && !(year % 100 == 0))|| (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = " + numDays);
}
In the code we can see that inside parentheses'()' we have given switch a variable month.
I wanted to know if user inputs 2 for month, then what happens.
Does it directly jump to case 2 or does it evaluate all preceding cases i.e. (case 1: case 3: case 5: case 7: case 8: case 10: case 12: case 4: case 6: case 9: case 11:) and then it finds case 2 and executes it?
What does it do?