🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators minus
This page was created by Charles on 2025-11-18. Last edited by Wikiadmin on 2026-07-29.

You use the binary minus operator (-) in OCL to subtract one numeric value from another and to return the elements in one collection that are not in another collection.

Syntax

leftValue - rightValue

The meaning depends on the values on each side of -:

Values Meaning Example
Two numeric values Subtract the right value from the left value. Budget - Spent
Two collections Return the difference: the elements that remain from the first collection after the second collection is subtracted. collectionOne - collectionTwo

Subtract numeric values

Use - when an expression needs a numeric result, such as a calculated value, a derived attribute, or a constraint.

For example, assume that Department has these attributes:

Attribute Type Example value
Budget Double 1000.0
Spent Double 650.0

Calculate the remaining budget with:

Budget - Spent

The result is 350.0. In a Department context, the equivalent explicit form is:

self.Budget - self.Spent

The left operand is the starting value. Reversing the operands changes the result: Spent - Budget returns -350.0 for the same values.

Find the difference between collections

When both operands are collections, - returns what is left in the first collection after subtracting the second collection. This is useful when you need to identify items present in one selection but absent from another.

For example:

collectionOne - collectionTwo

If collectionOne contains {1, 5} and collectionTwo contains {5}, the result contains {1}. The value 5 does not appear in the result because it occurs in both collections. If 5 is removed from collectionTwo, subtracting that collection no longer removes 5 from collectionOne.

Use excluding when you want to remove one specified object from a collection rather than subtracting one collection from another.

Precedence and negative values

Binary minus is evaluated after multiplication and division, and at the same precedence level as addition. Parentheses change the evaluation order. For example:

10 - 2 * 3

returns 4, because multiplication is evaluated first. Write (10 - 2) * 3 when you need the result 24.

A leading minus sign is a different operation: unary minus negates one value. For example, -3 + 2 is interpreted as (-3) + 2, which returns -1. See OCL precedence rules for the complete evaluation order.

See also