exprv4:tutorials:first_expression



First Expression

An expression is a piece of Expr code that produces a value.

The simplest expressions are numbers, strings, and calculations:

5;
2 + 3;
'Hello';

The expression `2 + 3` produces the value `5`.

Most Expr statements end with a semicolon `;`.


Calculator Style

Expr can be used like a calculator:

2 + 3;
10 - 4;
6 * 7;
20 / 5;

Operators follow normal calculation rules:

2 + 3 * 5;      // 17
(2 + 3) * 5;    // 25

Parentheses make the order explicit. Use them when the expression should be easy to read, especially in machine logic.


Strings

Text values are written as strings.

'Hello';
'Tool change';
'Probing started';

Strings are useful for names, messages, labels, and values passed to functions that expect text.


Variables

A variable stores a value so it can be reused later.

a = 10;
b = 20;
 
result = a + b;
result;

This script stores `10` in `a`, stores `20` in `b`, calculates `a + b`, and stores the result in `result`.

The final line evaluates to `30`.


One Script Can Have Many Statements

Expr scripts usually contain several statements.

toolDiameter = 6;
radius = toolDiameter / 2;
 
radius;

The last expression is often the value you are interested in. In this example, the final value is `3`.

Some Expr scripts are used mainly for their actions, such as setting variables, calling functions, showing dialogs, or running machine workflow logic. In those scripts, the final value may be less important than what the script does.


Comments

Comments are notes for the person reading the script. They are ignored when the script runs.

// Calculate tool radius
toolDiameter = 6;
radius = toolDiameter / 2;

Use comments to explain why something is done, especially in probing, ATC, safety, or machine-specific scripts.


Common First Mistakes

Missing Semicolon

Most statements need a semicolon:

a = 10;
b = 20;
a + b;

If a script reports an error near the next line, check the previous line for a missing `;`.

Unclear Calculation Order

This is valid:

2 + 3 * 5;

But this is often clearer:

2 + (3 * 5);

When a calculation controls machine behavior, prefer the clearer version.


Try This

Read this script and predict the final value:

width = 40;
height = 25;
area = width * height;
area;

The final value is `1000`.

Next: Values, variables, and assignment

exprv4/tutorials/first_expression.txt · Last modified: by 127.0.0.1

Page Tools