6

Since different languages have different definitions for an expression and a statement, what is the difference between them in Dart?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
  • 1
    I don't think there is much difference between different languages. https://stackoverflow.com/a/4728162/217408 – Günter Zöchbauer Dec 09 '18 at 12:27
  • See also https://github.com/dart-lang/sdk/blob/master/doexxec.tex#L9854-L9871 vs https://github.com/dart-lang/sdk/blob/master/docs/language/dartLangSpec.tex#L5040-L5042 – Günter Zöchbauer Dec 09 '18 at 12:33

3 Answers3

12

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 1
    Is `int a = 10 + 20` an expression or a statement? – iDecode Apr 11 '21 at 14:55
  • 1
    @iDecode, Since `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`). – Suragch Apr 12 '21 at 01:53
  • I found example contradictory to `myInt = 1;` being statement. It's `lambda() => myInt = 1;`. Why? Look at fat arrow explanation in the docs: https://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. – PrzemekTom Feb 03 '23 at 11:32
  • @Przemo. Good question. I opened an issue about it [here](https://github.com/dart-lang/site-www/issues/4582). – Suragch Feb 07 '23 at 06:05
5

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 of expr1 or expr2.
  • 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 of expr1 or expr2. 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.

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
1

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) }

mezoni
  • 10,684
  • 4
  • 32
  • 54