Difference between an Expression and Statement in Dart?
Asked Answered
C

3

6

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

Cogwheel answered 9/12, 2018 at 9:29 Comment(2)
I don't think there is much difference between different languages. https://mcmap.net/q/16820/-what-is-the-difference-between-an-expression-and-a-statement-in-pythonLivelong
See also github.com/dart-lang/sdk/blob/master/doexxec.tex#L9854-L9871 vs github.com/dart-lang/sdk/blob/master/docs/language/…Livelong
H
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.

Handrail answered 9/12, 2018 at 16:57 Comment(0)
C
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.

Conchiolin answered 27/6, 2020 at 9:57 Comment(5)
Is int a = 10 + 20 an expression or a statement?Pondweed
@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).Conchiolin
I found example contradictory to 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
@Przemo. Good question. I opened an issue about it here.Conchiolin
@Planter In the "switch expressions" chapter, the documentation talks about "Expression statements". I guess that it talks about this kind of things.Cerallua
H
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.

Handrail answered 9/12, 2018 at 16:57 Comment(0)
S
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) }

Sphygmoid answered 9/12, 2018 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.