Fallthrough Condition in Dart
Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add break statement and in that case flow of control jumps to the next line.
“If no break appears, the flow of control will fall through all the cases following true case
until the break is reached or end of the switch is reached.”
So, it is clear that the most basic way of creating the situation of fall through is skipping the break statements in Dart, but in the dart, it will give a compilation error.
Example 1: Skipping break statements
However, it allows skipping of break statement in the case when there is only one case statement defined.
Note: It must be noted that Dart allows empty cases.
Example 2: Providing an empty case.
GeeksforGeeks number 3
In the above code if we initialize the value of gfg = 3 than the output will not change. Another way to achieve fall through is via using continue instead of break statement in switch-case.
Example 3: Using continue instead of break
GeeksforGeeks number 1 GeeksforGeeks number 2
Note: It must be noted that if we will not declare label with continue in the above code than the code will display error.