• Overview - Expressions

    Expressions are the basis of Kipper, as every operation in a program is made up of single expressions that perform certain actions, like reading a value, calculating something, calling a function, comparing values…

    A simple example for expressions can be a simple calculation, like this:

    val1 + val2 * val3; // Mathematical/Arithmetic expression

    Notice how in this case we are not assigning the result to anything? This is an important fact of expressions that they are usually independent and do a single task, meaning if we wanted to assign this result to a variable, we would have to use an assignment expression like this:

    // Assign and arithmetic expression
    var result: num = val1 + val2 * val3;

    In this case, we also now created an expression statement (ends in ;), which wraps an expression and allows it to be used inside a program. Expression statements will be explained further in the docs page Statements.

    Kipper Operators and Expressions Precedence

    The following table shows all valid operators and expressions in the Kipper language, with their respective precedence. The higher the precedence the higher the importance of the item, and the order of evaluation.

    Precedence Operator / Expression Description Associativity
    14 ( ... ) Tangled expression - Forced increased precedence n/a
    13 ++ ... / -- ...
    call ... ( ... )
    ... [ ... ]
    ... . ...
    Prefix increment and decrement (Suffix)
    Function Call
    List subscripting
    Object member accessing
    Left-To-Right
    12 ... ++/ ... --
    Postfix increment and decrement
    n/a
    11 + ... / - ...
    ! ...
    Unary plus and minus
    Logical NOT
    Right-To-Left
    10 ... as ... Type conversion Left-To-Right
    9 ... * ...
    ... / ...
    ... % ...
    ... ** ...
    Multiplication
    Division
    Modulus
    Power-To
    8 ... + ... / ... - ... Addition and subtraction
    7 ... < ... / ... <= ...
    ... > ... / ... => ...
    More than / More or Equal to
    Less than / Less or Equal to
    6 ... == ... / ... != ... Relational comparison (Equal / Not equal to)
    5 ... && ...
    Logical AND
    4 ... || ... Logical OR
    3 ... ? ... : ...
    Ternary expression
    Right-To-Left
    2 ... = ...
    ... += ... / ... -= ...
    ... *= ...
    ... /= ...
    ... %= ...
    Simple assignment
    Increment or Decrement assignment
    Multiplicative assignment
    Divisional assignment
    Rest of Division / Remainder assignment
    1 ... , ... Comma Left-To-Right

    How does precedence of operators and expressions affect a program?

    The order of precedence is a very vital concept in programming languages, as they define how expressions are going to be evaluated. This directly can affect how your code runs, as certain parts might be evaluated first before others and change the entire course of your program.

    A simple example of this are arithmetic expressions + - * / %, which have per mathematical conventions their own order of precedence. For example, multiplication and division signs always come first before plus and minus, unless you explicitly use brackets, like these ( ). The same can be done here in Kipper as well, where you can forcefully increase the order of precedence using brackets:

    15 + 4 * 6; // -> (15 + (4 * 6)) -> (15 + 24) -> 39

    Forced higher precedence using brackets:

    (15 + 4) * 6; // -> ((15 + 4) * 6) -> (19 * 6) -> 114

    What does associativity mean?

    As you should have already seen in the table above, there is an extra column defining the so-called associativity. What does that mean? It's relatively simple and means whether the following (( ) meaning the expression is read first):

    a OPR b OPR c

    is evaluated as (left-associative - Reads from left to right):

    (a OPR b) OPR c

    or as (right-associative - reads from right to left):

    a OPR (b OPR c)

    This is especially important as it can change how certain things are evaluated, and also cause unwanted errors! For example the following would be interpreted as (left-associative):

    32 / 4 / 4; // -> ((32 / 4) / 4) -> (8 / 4) -> 2

    List of expressions in the Kipper language

    This is a concise list of all expressions in the Kipper language that may be used: