Contents hide
Switch Case In Dart
In this program, we will see how we can use a switch case statement to display the number of days in an entered month. We will be using String “month” to accept the month name, which can be in any case (i.e. upper case, lower case, or mixed case). Next, we will define another String “mon” the use of this string will be to convert the String “month” to lower case. Now, will start with a switch statement with the expression “mon” and we will write the cases to execute the statement when the condition is satisfied.
For Example:-
Input = January | Output = January has 31 days |
Input = February | Output = February has 28 days Or 29 Days For Leap Year |
Input = March | Output = March has 31 days |
Input = April | Output = April has 30 days |
Input = May | Output = May has 31 days |
Input = June | Output = June has 30 days |
Input = July | Output = July has 31 days |
Input = August | Output = August has 31 days |
Input = September | Output = September has 30 days |
Input = October | Output = October has 31 days |
Input = November | Output = November has 30 days |
Input = December | Output = December has 31 days |
To run dart program open www.dartpad.dev
Program
void main(){
String month = "January"; //Enter the month name in any case (upper, lower or mixed)
String mon = month.toLowerCase(); //The String month will be converted to lower case
switch (mon) {
case "january": //Case for month with 31 Days
case "march":
case "may":
case "july":
case "august":
case "october":
case "december": {
print("$month has 31 Days"); //Statement
}
break;
case "april": //Case for month with 30 Days
case "june":
case "september":
case "november": {
print ("$month has 30 Days"); //Statement
}
break;
case "february": { //Case for February
print ("$month has 28 days Or 29 days for leap year"); //Statement
}
break;
default: { //Default Case
print ("Invalid Month Name"); //Statement
}
break;
}
}
OUTPUT:
January has 31 days