Since different languages have different definitions for an expression and a statement, what is the difference between them in Dart?
I'm still new to Dart but with previous knowledge (and reading dart language tour):
- Expressions usually evaluate to something, for example when using conditional expression,
condition ? expr1 : expr2
has a value ofexpr1
orexpr2
. - Statements have no value or usually do nothing to change values directly.
A statement
contains expressions
, but an expression
can’t contain a statement
.
Above is my explanation of bullet point I tried to simplify for you, found when reading language tour on category important concepts that goes like this:
Dart has both expressions (which have runtime values) and statements (which don’t). For example, the conditional expression
condition ? expr1 : expr2
has a value ofexpr1
orexpr2
. Compare that to an if-else statement, which has no value. A statement often contains one or more expressions, but an expression can’t directly contain a statement.
Short answer
Expressions are values, and statements do things.
Examples
This makes more sense if you can see examples.
Expressions
An expression has a value at runtime.
42
true
hello
1 + 1
x
myObject
myInt + 1
k++
p > 0
condition ? expr1 : expr2
'hello'.toUpperCase()
myObject.toString()
myObject.someMethod()
myObject?.someProperty?.someMethod()
myString.isEmpty
[1, 2, 3]
[...list1]
<String, String>{...a, ...b}
Statements
A statement does something and in itself doesn't have a value at runtime. Statements are not expressions, but they can contain expressions.
myInt = 1;
print('hello');
return null;
if (name != null) { return name; } else { return 'Guest'; }
for (var i = 0; i < 5; i++) { message.write('!'); }
break;
while (!isDone()) { doSomething(); }
yield k++;
assert(text != null);
throw FormatException('Expected at least 1 section');
void distanceTo(Point other) => throw UnimplementedError();
Note: Most of the examples here came by searching the documentation for the keywords expression and statement.
int a = 10 + 20
an expression or a statement? –
Pondweed int a = 10 + 20;
does something (declares an integer variable and then assigns it a value), it is a statement. The right-hand side 10 + 20
is an expression (with a value of 30
). –
Conchiolin myInt = 1;
being statement. It's lambda() => myInt = 1;
. Why? Look at fat arrow explanation in the docs: dart.dev/guides/language/language-tour#functions. It says "Only an expression—not a statement—can appear between the arrow (=>) and the semicolon (;)". The aforementioned example compiles, linter doesn't complain, so it's an expression, not a statement. –
Planter I'm still new to Dart but with previous knowledge (and reading dart language tour):
- Expressions usually evaluate to something, for example when using conditional expression,
condition ? expr1 : expr2
has a value ofexpr1
orexpr2
. - Statements have no value or usually do nothing to change values directly.
A statement
contains expressions
, but an expression
can’t contain a statement
.
Above is my explanation of bullet point I tried to simplify for you, found when reading language tour on category important concepts that goes like this:
Dart has both expressions (which have runtime values) and statements (which don’t). For example, the conditional expression
condition ? expr1 : expr2
has a value ofexpr1
orexpr2
. Compare that to an if-else statement, which has no value. A statement often contains one or more expressions, but an expression can’t directly contain a statement.
From Wikipedia:
In mathematics, an expression or mathematical expression is a finite combination of symbols that is well-formed according to rules that depend on the context. Mathematical symbols can designate numbers (constants), variables, operations, functions, brackets, punctuation, and grouping to help determine order of operations, and other aspects of logical syntax.
The same thing in Dart.
Statement in this case can be desribed as a combination of expressions and possible other symbols that required for correct notation of concrete statement.
In Dart, the statement can be empty which means that statement does not contains any expressions. Empty statement can be stated by well-formed notation or determined by context.
Example (in pseudo code) of if-else
statement.
if (expression) { statement(s) } else { statement(s) }
© 2022 - 2024 — McMap. All rights reserved.