Monday, May 16, 2022
Envato Elements
HomeCodingDart Program To Count The Number Of Days Using Switch Case.

Dart Program To Count The Number Of Days Using Switch Case.

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 = JanuaryOutput = January has 31 days
Input = FebruaryOutput = February has 28 days Or 29 Days For Leap Year
Input = MarchOutput = March has 31 days
Input = AprilOutput = April has 30 days
Input = MayOutput = May has 31 days
Input = JuneOutput = June has 30 days
Input = JulyOutput = July has 31 days
Input = AugustOutput = August has 31 days
Input = SeptemberOutput = September has 30 days
Input = OctoberOutput = October has 31 days
Input = NovemberOutput = November has 30 days
Input = DecemberOutput = 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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments