Difference between an Expression and Statement in Dart?

Issue

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

Solution

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.

Answered By – Dinko Pehar

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.