Expressions and calculation

ABCalc uses the NCalc library for expression evaluation. Below, you will find the basic information and the description of how ABCalc handles optional behavior supported by the NCalc library. You may also want to check the NCalc documentation, which provides the detailed information about the nuances of the expression syntax.

Expressions and statements

An expression is composed of some values and operators. Expressions may take place of values in larger expressions.

Examples:

1 - this is a simple expression that will yield 1 as a result
2 + 2 - an expression that includes an operator
(2+5)*10 - more complex expression
(3-2)% - also a valid expression

In ABCalc, every valid expression yields some result. In some cases, null may be returned as a result.

Groups of Expressions

ABCalc handles expressions which include assignment statements (which are also expressions as they return the assigned value) and groups of expressions.

Expressions in a group are separated with a semicolon. One or several expressions may be wrapped with curly braces '{' and '}' to make a single expression, which is useful when a group of expressions should be treated as one expression (e.g., in function parameters).

Example:

a := 1; a // will yield number 1 read from the memory cell (variable) named 'a'
{ a := 1; a } // same as above

Conditional Statements

ABCalc supports conditional (if) statements with a common C-style syntax.

Example:

a := 1; if (a < 5) { a += 1; } // this puts 2 to 'a'

if (1 > 2) 
  true 
else 
  false // yields the 'false' value

if (1 > 2) {true} else {false} // this can also be used for better readability.

Note that unlike C-like languages, you do not put a semicolon after expressions in if/else statements.

Additionally, there exists a built-in iff() function and a ternary operator that can be used as alternatives to the if statement. Both the function and the ternary operator have the same functionality, just in different notation.

Loops

ABCalc supports while loops with a common C-style syntax. A memory cell (variable) can be used for a counter.

Example:

a := 1; 
while (a < 5) { a += 1; } // this puts 5 to 'a'

Loops are restricted to 65635 iterations to prevent infinite looping.

Loop Flow Control

You can use break and continue keywords in loops; they do the same job as in C/C++ and C#.

Returning of Value

An expression always evaluates to some value. In complex expressions which include conditional statements and loops, it may be desired to return a value without evaluating the expression completely. For this, one can use a common return keyword followed by the value to return.

Comments

ABCalc supports C-style line and block comments, which are mostly useful in custom functions.

{ 
    // this comment is skipped
    1 /* this produces 1 as a result */ 
}