Expressions

Expressions in Zard represent computations that produce a value. They can combine literals, variables, and operators such as +, -, *, and /. Expressions are evaluated from left to right, respecting operator precedence.

Basic Arithmetic

Zard supports all standard arithmetic operations between numeric types:


main {
  int a = 10;
  int b = 5;

  int sum = a + b;       # 15
  int diff = a - b;      # 5
  int product = a * b;   # 50
  double div = a / b;    # 2.0

  print(sum);
  print(diff);
  print(product);
  print(div);
}
      

Operator Precedence

Multiplication and division have higher precedence than addition and subtraction. You can use parentheses ( ) to explicitly define evaluation order.


main {
  int x = 10;
  int y = 5;
  int z = 2;

  int result1 = x + y * z;      # 10 + (5 * 2) = 20
  int result2 = (x + y) * z;    # (10 + 5) * 2 = 30

  print(result1);
  print(result2);
}
      

Using Parentheses

Parentheses can be nested to create complex expressions. Zard evaluates expressions inside parentheses first.


main {
  double result = (5 + 3) * (2 + (4 / 2));
  print(result); # (8) * (2 + 2) = 8 * 4 = 32
}
      

Mixed Types

When mixing numeric types, Zard automatically promotes values to the most precise type involved in the expression. For example, using an int and a double will produce a double result.


main {
  int a = 3;
  double b = 2.5;

  double result = a + b; # promoted to double
  print(result);         # 5.5
}
      

Invalid Operations

Zard enforces strict type checking. Attempting to apply arithmetic operators to incompatible types (e.g., strings or structs) results in a compile-time error.


main {
  string name = "Zard";
  int age = 5;

  # name + age; ❌ Invalid — cannot add string and int
}
      

Expressions with Print and Assignments

Expressions can appear inside print(), if() conditions, and assignments.


main {
  int x = 4;
  int y = 6;

  print(x + y * 2);  # 16
  int total = (x + y) * 2;
  print(total);      # 20
}
      

Full Example


main {
  int a = 10;
  int b = 3;
  double c = 2.5;

  double result = (a + b) * c - (b / 2.0);
  print(result); # (13 * 2.5) - 1.5 = 32.5 - 1.5 = 31.0
}