Conditional Constructs and Expressions

Some languages (such as Rust) treat conditional constructs as expressions. Dart only does this when they are inside a list literal; in other places you’d need to use a conditional expression with the conventional C-like syntax using the ternary ?: operator.

The if-else Construct

The if-else construct in Dart is very similar to that of many other languages; it can be expressed using curly braces:

 if​(condition) {
 // Instructions to execute when the condition is true
 } ​else​ {
 // Instructions to execute when the condition is false
 }

and, if there is only one instruction to execute for each, without curly braces (executing true_instruction when the condition is true and false_instruction when it is false), usually written without line breaks:

 if​(condition) true_instruction;
 else​ false_instruction;

else if

If there is a condition to check inside the else clause you can use the else if construct:

 int​ a;
 // (...)
 if​(a>10) {
 // (...)
 } ​else​ ​if​(a == 10) {
 // (...)
 } ​else​ {
 // (...)
 }

Conditional Expressions

If you need to test a condition to decide between two values for an assignment, an expression or a return statement you can use a conditional expression. It is an expression containing the ?: ternary operator, and it can be used in the following way:

 var​ a = condition ? true_value : false_value;

in which the value of a will be true_value if the condition is true and false_value if the condition is false. This is extremely useful for replacing simple functions such as the following:

 num​ ​max​(​num​ a, ​num​ b) {
 if​(b > a) ​return​ b;
 else​ ​return​ a;
 }

with even simpler one-liners:

 num​ ​max​(​num​ a, ​num​ b) => b > a ? b : a;

Switch

Switch is one of the features of Dart that might look most archaic: it works just like C’s or Java’s. They are used when you need to compare a variable to a certain number of constant values and are not recommended for use in Flutter apps. Its structure is the following:

 var​ a;
 // (...)
 
 switch​(a) {
 case​ 1:
 // instructions to execute if a is equal to 1
 break​;
 case​ 2:
 // instructions to execute if a is equal to 2
 break​;
 default​:
 // instructions to execute if a is neither equal to 1 nor 2
 }

If you omit the break instructions, the cases defined after the case triggered by the value of the variable will be executed as well. This effect (fall-through) allows for multiple values to be handled by a single case:

 switch​(a) {
 case​ 1: ​// this is supposed to fall through
 case​ 2:
 // instructions for values 1 and 2
 break​;
 case​ 3:
 // instructions for the value 3
 break​;
 default​:
 // (...)
 }

Fall-through obtained by omitting break is not allowed if you have instructions in the case and will generate an error. If you mean to do that, you should use the continue instruction with a label:

 switch​(a) {
 case​ 1:
 // additional instructions for 1
 continue​ caseTwo;
  caseTwo:
 case​ 2:
 // instructions for 1 and 2
 break​;
 default​:
 // (...)
 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset