Statements and Expressions

Nesma220
2 min readJul 8, 2023

--

In Dart, statements and expressions are fundamental concepts that form the building blocks of any program. Understanding the difference between the two is crucial for writing effective and efficient code.

  1. Statements:
  • Statements are lines of code that perform actions or execute a series of instructions.
  • They are often used to control the flow of execution in a program, such as loops (for, while) and conditional statements (if-else, switch).
  • Statements do not produce a value or result.
  • Examples of statements in Dart include variable declarations, function declarations, and control flow statements.
// Variable declaration statement
int a = 5;

// Function declaration statement
void printMessage(String message) {
print(message);
}

// If-else statement
if (a > 0) {
print('a is positive');
} else {
print('a is non-positive');
}

2. Expressions:

  • Expressions are combinations of variables, values, operators, and function calls that produce a value or result when evaluated.
  • They can be simple or complex, involving mathematical operations, logical comparisons, or function invocations.
  • Expressions can be used in assignments, method calls, conditionals, and more.
  • Examples of expressions in Dart include arithmetic expressions, logical expressions, and function invocations.
// Arithmetic expression
int sum = 2 + 3;

// Logical expression
bool isTrue = (a > 0) && (sum < 10);

// Function invocation expression
printMessage('Hello, World!');

Key points to remember:

  • Statements are executed for their side effects, such as modifying variables or controlling program flow.
  • Expressions are evaluated to produce a value that can be used in assignments or other expressions.
  • Expressions can be embedded within statements, but statements cannot be embedded within expressions.
  • Understanding the distinction between statements and expressions helps in writing clean and readable code.

In summary, statements and expressions are essential components of Dart programming. Statements control the flow of execution, while expressions produce values. By mastering the use of both, developers can write efficient and effective Dart code.

--

--

Nesma220
Nesma220

No responses yet