What is the default statement in Dart?

Overview

In the switch case, the default statement is utilized when no condition matches.

Syntax

switch(variable_expression) { 
   case constant_expr1: { 
      // statements; 
   } 
   break; 
   default: { 
      //statements;  
   }
   break; 
} 

Code

The following code shows how to implement the default statement in Dart:

// dart program
void main()
{
int num = 3;
switch (num) {
case 1: {
print("Shot 1");
} break;
case 4: {
print("Shot 4");
} break;
default: {
print("This is default case");
} break;
}
}

Explanation

  • Lines 2 to 16: We create the main() function. In the main():
    • Line 4: We declared a variable num of int type and assigned value to it.
    • Lines 5 to 15: We use a switch statement. The value of the num is tested against cases in the switch. If the value of num matches one of the cases, the corresponding code block is executed. Otherwise, the code within the default block is executed.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources