exprv4:how-to:use_if_and_else

How To Use If And Else

Use `if` when a script needs to make a decision.

Examples:

  • choose a feed rate
  • check whether a value is too high
  • show a warning message
  • stop when required data is missing
  • choose one result from several possible results

Basic if

An `if` condition is written inside parentheses. The code to run is usually written inside braces.

tool_length = 60;
safe_z = 5;
 
if(tool_length > 50)
{
    safe_z = 10;
};
 
safe_z;

The final result is `10`.

The condition `tool_length > 50` is true, so the code inside the block runs.


Use else For The Other Case

Use `else` when you want one action for true and another action for false.

probe_error = 0.03;
 
if(probe_error <= 0.05)
{
    print('probe result ok');
}
else
{
    print('probe error too large');
};

Only one branch runs.


Store The Result Of if

In Expr, `if` can produce a value. This is useful when you want to choose one value and store it.

material = 'aluminum';
 
feed = if(material == 'aluminum')
{
    900;
}
else
{
    600;
};
 
feed;

The final result is `900`.

When you use `if` as a value, include an `else` branch unless `none()` is acceptable.


Use else if For Several Choices

Use `else if` when you need to test several conditions in order.

probe_error = 0.07;
 
status = if(probe_error <= 0.01)
{
    'excellent';
}
else if(probe_error <= 0.05)
{
    'acceptable';
}
else if(probe_error <= 0.10)
{
    'warning';
}
else
{
    'failed';
};
 
status;

The final result is `'warning'`.

The conditions are tested from top to bottom. The first true branch runs and the rest are skipped.


Compare Values

Common comparison operators:

Operator Meaning Example
== equal tool == 3
!= not equal tool != 0
> greater than feed > 100
>= greater than or equal z >= safe_z
< less than depth < 0
<= less than or equal error <= 0.05

Examples:

tool = 3;
feed = 100;
depth = -2;
 
if(tool == 3)
{
    print('tool 3 selected');
};
 
if(feed >= 100 && depth < 0)
{
    print('ready to probe');
};

Use == for comparison. Use `=` only for assignment.


Combine Conditions

Use logical operators when more than one condition matters.

Operator Meaning Example
&& and ready && !blocked
|| or manual_mode || auto_mode
! not !blocked

Use `&&` when all conditions must be true.

tool_loaded = true;
probe_ready = true;
blocked = false;
 
if(tool_loaded && probe_ready && !blocked)
{
    print('can start');
};

Use `||` when at least one condition is enough.

manual_mode = false;
auto_mode = true;
 
if(manual_mode || auto_mode)
{
    print('machine mode selected');
};

Check For Missing Values

An unknown variable returns `none()`. You can check for it before using the value.

if(tool_diameter.is_none())
{
    print('tool diameter is missing');
}
else
{
    radius = tool_diameter / 2;
    print('radius ', radius);
};

This avoids using a missing value in a calculation.


Use if From MDI

In MDI, start with `=` to evaluate Expr.

=feed=120;
if(feed > 100)
{
    'fast';
}
else
{
    'normal';
};

This outputs:

fast

Common Mistakes

Using = Instead Of ==

This assigns a value:

tool = 3;

This compares a value:

tool == 3;

Use == in conditions when you want to test equality.

Forgetting The Semicolon After if

When `if` is used as a statement, end it with `;` after the closing brace.

if(feed > 100)
{
    print('fast');
};

Forgetting The else Value

Without `else`, a false `if` produces `none()`.

result = if(false)
{
    123;
};
 
result.is_none();    // true

If you need a number, provide an `else` branch.

result = if(false)
{
    123;
}
else
{
    0;
};

Making Conditions Too Clever

This works, but is harder to read:

if(tool > 0 && feed > 0 && depth < 0 && !blocked)
{
    print('ready');
};

For important machine logic, split checks when it makes the script clearer.

tool_ok = tool > 0;
feed_ok = feed > 0;
depth_ok = depth < 0;
 
if(tool_ok && feed_ok && depth_ok && !blocked)
{
    print('ready');
};

Try This

In MDI, try:

=material='steel';
feed = if(material == 'aluminum')
{
    900;
}
else if(material == 'steel')
{
    300;
}
else
{
    500;
};
feed;

The final result is `300`.


See Also

exprv4/how-to/use_if_and_else.txt · Last modified: by 127.0.0.1

Page Tools