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. When null appears as an operand in an arithmetic expression, it propagates — the result of the operation is also null. See Null values in expressions for details and workarounds.
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 have the same functionality as a two-branch conditional, just in different notation. For multi-branch conditionals, the ifs() function can replace a chain of if/else if statements. See Special Built-in Functions for details on iff() and ifs().
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#.
You can also use return to exit the loop and the entire expression at once with a specific result value:
// Find the first element in a list greater than 5
list := (3; 7; 1; 9; 4);
i := 0;
while (i < Len(list))
{
if (list[i] > 5) { return list[i]; };
i += 1;
}
// returns 7 (the first element > 5)
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 comments and block comments. Comments are mostly useful in custom functions and multi-line expressions.
{
// This is a line comment — everything to the end of the line is ignored
1 + 2 /* this is a block comment; the expression produces 3 */
}
fn Hypotenuse(a; b)
{
// Pythagorean theorem
return Sqrt(a*a + b*b); /* returns the hypotenuse length */
};
Triple-slash documentation comments (///) are a special form used to attach a description to a custom function. See Documentation Comments for details.
Note on // and integer division: In some expression languages, // is used for Python-style integer division. In ABCalc, // always starts a line comment, so this operator is not available. Use \\ or div for integer division instead:
10 \\ 3 // 3 (integer division — the \\ is the operator, not a comment)
10 div 3 // 3 (same result)