DartLang

🎯 Dart (DartLang) Introduction: switch conditional statement

When if/else statements have too many blocks, it is easier to use switch/case statements. switch/case statement in Dart looks just like JavaScript syntax.

Uday Hiwarale
RunDart
Published in
2 min readOct 6, 2019

--

When if/else statements have too many blocks, it is easier to use switch/case statements. switch/case statement in Dart looks just like JavaScript syntax. We can optionally use {} curly braces to define a statements block.

break keyword is necessary to terminate switch block when a case is matched. It can also be terminated with return statement in a case when switch statement is used inside a function that returns a value.

In a case when we need to execute multiple case blocks, Dart allows falling through multiple switch-cases using continue keyword. But first, we need to label case statements which continue keyword is going to fall through.

A label of a case block is an identifier used by continue keyword. This label should be given on the case block and without case keyword. When a case block is going to continue to another case block, it should not be terminated with break or return keywords.

The only drawback with switch/case statement in Dart is, a case block must be identified with a compile-time constant value. Which means case block can not be identified by an expression that yields a value.

--

--