In the previous post we are already looked into Conditional (If Else) and nested but, its not an efficient way to have a lot of nested if-else as it's cumbersome to understand.
In some cases, decision-making can be easier when you use a switch instead of if-else logic. switch statements in Dart compare integers, strings, or compile-time constants using the double equal sign (==) behind the scenes; it maintains the rule, though, that the compared objects must be instances of the same class and not of any of its subtypes.

The General Syntax:
switch(variable_expression) {
case constant_expr1: {
// statements;
}
break;
case constant_expr2: {
//statements;
}
break;
default: {
//statements;
}
break;
}
The value of the variable_expression is tested against all cases in the switch. If the variable matches one of the cases, the corresponding code block is executed. If no case expression matches the value of the variable_expression, the code within the default block is associated.
The following rules apply to a switch statement −
- There can be any number of case statements within a switch.
- The case statements can include only constants.
- It cannot be a variable or an expression.
- The data type of the variable_expression and the constant expression must match.
- Unless you put a break after each block of code, the execution flows into the next block.
- The case expression must be unique. The default block is optional.
The Example:
We are going to See a simple Example to implement a marking system using switch case.
void main() {
var marks = "A";
switch(marks) {
case "A": { print("Very Good"); }
break;
case "B": { print("Good"); }
break;
case "C": { print("Fair"); }
break;
case "D": { print("Poor"); }
break;
default: { print("Fail"); }
break;
}
}
The significance of break is that if the condition is met, the switch statement ends, and the program continues. We have used a default clause to execute the code when no case clause matches.
Conclusion:
We have seen an alternative to the nested If else statement. The Switch cases are more easily readable and easy to understand as per the developer perspective.