Introduction Welcome Getting Started My First Program     Troubleshooting and Distributing Transforming Data Interactive Programs     Programming Exercises     Making Games
Documentation Declarations     Procedures     Variables Statements     Flow Control Expressions Libraries     Print     IO     Random     Game Engine
Tutorials Complete Tutorials     Introduction to Programming     Quick Start     Programming Exercises     Programming Games

EXPRESSIONS


All About the Expressions in Vizzcode





About Expressions

An expression is a combination of one or more operations and values that resolve to a single value.

For example, the number 1 in the following code is a literal integer expression:
main
:: () {
    var :=
1
;
}

What is an Expression

You can combine several expressions together to create a single value.
The following example contains a plus arithmetic expression of the literal integer expression of value 10 and a procedure call expression.
main
:: () {
    var :=
10
+
scan_int
(
"Type a number :"
);
}

Combining Expressions in the Visual Panel

Note that the Visual panel always displays the expressions as a tree-like structure, branching from its single output to other expressions:

Expressions nodes are connected via non-execution line. The execution lines are the thicker, white ones. So the other lines (usually colored) represent the different types being used as inputs of outputs of expressions:

Expressions are executed in a certain order, according to their precedence.




Kinds of Expressions

Here are all the kinds of expressions and a basic explanation of each kind.
You can navigate to their link to learn more about each kind:

A name expression is a reference to a variable.
In the following example, some_var is a name expression that references the value stored inside that variable:
var := some_var;

What is a name expression (get variable) in Vizzcode


A procedure call expression executes the code of that procedure and possibly uses the return value of that procedure, when available.
For example:
var :=
some_procedure_that_returns_something
();
A Procedure Call expression


A literal integer expression is a whole number that is explicitly entered in the code, as opposed to a number that came from a variable (name expression) or a procedure call return.
var :=
10
;
A literal integer expression


Similarly, a literal float expression is a fractional number that is explicitly entered in the code.
var :=
10.5
;
A literal float expression


A literal string expression is a text that's entered in the code. It's always specified between double quotes " ", like so:
var :=
"Hello!"
;
A literal string expression


A literal bool expression is a true or false value entered in the code.
var :=
true
;
A literal bool expression


Arithmetic expressions can add, subtract, multiply, divide, mod and negate values.
var := (
4
+
6
) * -(
26
/
2
);
Arithmetic expressions


Comparison expressions test two values together and produce a bool. The comparison could be one of the following: equal to, not equal to, greater than, greater than or equal to, less than or less than or equal to.
var :=
56
>=
10
;
Comparison expressions


Boolean expressions can do specific logic on bools. There are three operators: and, or and not.
var :=
true
&& (
false
|| !
true
);
Boolean expressions


Array expressions can be used to access individual elements in the array (through the array subscript expression) and the size (or count) of the array with the array count expression.
arr : [
10
]
int
;
var1 := arr.count;
var2 := arr[
0
];
Array expressions





Node Expressions

Expressions exist in several different visual nodes in the Visual Panel.
They can be arbitrarily combined based on their type to make complex expressions:
Combining multiple expressions in the Visual Panel

Understanding the kinds of expressions you can use and how you can use them is very important to express your ideas and solutions in programming.
You can read more about the node execution order here and check out this great info on precendence for help reading more complex expressions.

You can also type a text expression inside the textbox of a node. For example:

Typing a text expression inside a node in Vizzcode

These text expressions can be arbitrarily complex, and are evaluated as if there were typed in the Text Panel. It can be faster to type somes expressions than to create nodes for them:

Program faster by typing expressions in Vizzcode





Procedure Calls

Like we've talked before, procedures are blocks of codes that can be called to have their code executed, possibly returning values to the caller.
When the return value of a procedure is ignored (or doesn't exist), that call works as a simple statement. If, however, that return value is used, it becomes an expression, possibly being combined into other expressions:

proc
:: () ->
float
{
    
return
5.6
;
}

main
:: () {
    
proc
();
    var :=
proc
();
    var =
proc
() + (
5
*
proc
());
}

Procedure calls in the Visual Panel are discussed in the next section.




Node Procedure Call

Procedure calls work the same way in the Visual Panel as in the Text Panel (described in the previous section).
When a procedure's return value is not used (or doesn't exist), the procedure must be called as a statement, using the thicker, white connection in the node:
When to connect the white execution pin in a procedure call

Alternatively, when a call's return values are used, they must be connected as an expression (with the other lines):

Using procedure call as expressions

Either way, the procedure's code is executed.
You can read more about return value here, including how to receive multiple return values from a procedure.







Constant Expressions

Some expressions' values are constant, which means it does not depend on the execution of the program and thus can be known by the compiler at compilation time.
For example, when you create a variable, by definition, its value can be changed during program execution, so it is not constant.
Literals, however, (literal ints, floats, strings and bools), are always constant.

Operating on constant values also result in a constant value.
For example, in the following code, the expression assigned to var1 is constant (because it is an arithmetic expression of two constant expressions), but the expression assigned to var2 is not, because it uses the value of a variable:
main
:: () {
    var1 :=
4
+
5
*
8
;
    var2 := var1 *
2
;
}


When declaring global variables the value you attribute to them at declaration must be constant. This is because the global scope is order-independent, allowing you to use a variable before its declarations.
For this reason, you can't assign non-constant values (like another variable) to a global variable declaration.

Another case where constant expressions are needed is in an array count specification at declaration (because array sizes must be known at compile-time):
main
:: () {
    array : [
10
]
int
;
}





Arithmetic Expressions

Arithmetic Expressions are expressions that perform some mathematical operation on values.
They can be:
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulo
All arithmetic operators (besides modulo) are pretty self explanatory.

These operators work on ints and floats only - with the exception of the modulo operator, that works only on ints.
You can only operate on values of the same type.

Here are a few examples of these operators in action:
main
:: () {
    a :=
4
+
6
;
    b := a * (
8
-
2
);
    a += b-
2
;
}
.
Doing math in Vizzcode

One important note is that integer division outputs an int, ignoring the decimal part. For example:
#import
"Print"


main
:: () {
    div :=
13
/
5
;
    
if
div ==
2
  
print
(
"True"
);
}





Modulo

The modulo operator calculates the rest (or remainder) of the division between two integers.
The modulo is expressed by the % symbol.

For example, if you divide 13 by 5 you get 2 and the remainder is 3.
In this example, the modulo of 13 by 5 would be 3 (the remainder of the division).

#import
"Print"


main
:: () {
    division :=
13
/
5
;
    modulo :=
13
%
5
;
    
    
if
division ==
2
  
print
(
"True"
);
    
if
modulo ==
3
  
print
(
"True"
);
}

The modulo is very useful to know if a number is divisible by another. In case the number is divisible, the result of the modulo would be 0.
This leads to another common use: to know if the number is even or odd.
Because all even numbers are divisible by 2, you can know if the number is even by testing if its modulo by 2 is zero:
#import
"Print"


main
:: () {
    
if
(
4
%
2
==
0
)  
print
(
"4 is even"
);
    
if
(
7
%
2
!=
0
)  
print
(
"7 is odd"
);
    
if
(
180
%
2
==
0
)  
print
(
"180 is even"
);
    
if
(
351
%
2
!=
0
)  
print
(
"351 is odd"
);
}





Assignable Expressions

Assignable expressions (also known as left-hand side expressions or lvalues) are expressions that result in values that can be assigned to.

The simplest case is a name expression: a simple reference to a variable.
In this case, you can assign a value to it by typing the variable name in the left-hand side of the assignment and the value on the right-hand side:
var =
5
;

In the Visual Panel, the assignable expression is the first expression in the Assign Node:
Assignable expressions in the Visual Panel

However, there could be more elaborate expressions that result in assignable values.
For example, the subscript expression is used to access an array element, which is an assignable value. The following code shows an assignment of an array subscript expression (the array arr, indexed at the position some_int + 5):
main
:: () {
    some_int :=
4
;
    arr : [
10
]
int
;
    
    arr[some_int +
5
] =
10
;
}

More complex assignable expressions





Comparison Expressions

Comparison Expressions are expressions that compare two different values of the same type, resulting in a boolean.
Here are the possible comparisons:

Equal to
The equal to operator is represented by the double equals == symbol. (Not to be confused with the single equals = operator that represents assignments).
#import
"Print"


main
:: () {
    a :=
45
;
    b :=
45
;

    
if
a == b  
print
(
"True"
);
}

Comparing if values are equal in Vizzcode

Not Equal to
#import
"Print"


main
:: () {
    a :=
45
;
    b :=
19
;

    
if
a != b  
print
(
"True"
);
}

Comparing if values are not equal in Vizzcode

Greater Than
#import
"Print"


main
:: () {
    a :=
45
;
    b :=
19
;

    
if
a > b  
print
(
"True"
);
}

Comparing if a value is greather than another in Vizzcode

Greater Than or Equal to
#import
"Print"


main
:: () {
    a :=
45
;
    b :=
19
;

    
if
a >= b  
print
(
"True"
);
}

Comparing if a value is greather than or equal to another in Vizzcode

Less Than
#import
"Print"


main
:: () {
    a :=
7
;
    b :=
20
;

    
if
a < b  
print
(
"True"
);
}

Comparing if a value is less than another in Vizzcode

Less Than or Equal to
#import
"Print"


main
:: () {
    a :=
7
;
    b :=
20
;

    
if
a <= b  
print
(
"True"
);
}

Comparing if a value is less than or equal to another in Vizzcode

Here is all of the comparison operators together:
#import
"Print"


main
:: () {
    
if
(
9
==
4
)  
print
(
"false"
);
    
if
(
9
!=
4
)  
print
(
"true"
);
    
if
(
9
>  
4
)  
print
(
"true"
);
    
if
(
9
>=
4
)  
print
(
"true"
);
    
if
(
9
<  
4
)  
print
(
"false"
);
    
if
(
9
<=
4
)  
print
(
"false"
);

    
if
(
5
==
5
)  
print
(
"true"
);
    
if
(
5
!=
5
)  
print
(
"false"
);
    
if
(
5
>  
5
)  
print
(
"false"
);
    
if
(
5
>=
5
)  
print
(
"true"
);
    
if
(
5
<  
5
)  
print
(
"false"
);
    
if
(
5
<=
5
)  
print
(
"true"
);
}

All the comparison operators work on ints and floats.
The equal and not equal operators also work on strings and bools.




Comparing Strings

In some other programming languages, there are special ways to test the equality of strings.
In Vizzcode, it's as easy as using the equality comparison operator to test it.

You may compare any string type (literal or variable) just the same:
main
:: () {
    message :=
scan
(
"Hello? "
);
    
if
(message ==
"hi"
)  
print
(
"How are you?"
);
    
else
if
(message ==
"who are you?"
)  
print
(
"I am a computer program!"
);
}

How to compare strings in Vizzcode

The not equals operators also work on strings:
main
:: () {
    message :=
scan
(
"Hello? "
);
    
if
(message !=
"exit"
) {
        
print
(
"Let's do some programming!"
);
    }
}


Using the not equals operator on strings


The other comparison expressions (<, <=, > and >=) don't work on strings.




Boolean Operators

There are logic operators (or boolean operators) that work specifically on booleans.
They are very commonly used to create more complex conditions.
There are three boolean operators:
  • AND &&: the result is true only if both operands are true
  • OR ||: the result is true if any operand is true
  • NOT !: the result is true only if the operand is false
These are the nodes for these operators in Vizzcode:
Boolean expressions in the Visual Panel

Here are a few examples:
main
:: () {
    true1 :=
true
&&
true
;
    false1 :=
true
&&
false
;

    true2 :=
true
||
false
;
    false2 :=
false
||
false
;

    true3 := !
false
;
    false3 := !
true
;
}

Boolean expressions in action

They can be combined to create arbitrarily complex boolean expressions:
main
:: () {
    true1 := (
5
==
4
) || (!(
7
>
10
) && (
"Dog"
!=
"Wolf"
));
    true2 := (
true
||
false
) && !(!
true
||
false
);

    false1 := (
8.8
<
10
||
8.8
<
-50.0
) && (!(
5
!=
5
)) && (
5
<
5
||
8
>
10
);
    false2 := (
true
&& (
false
||
true
)) || !(
false
&& !
false
);
}

It's important to note that the right side of an AND or OR expression is only evaluated if necessary. See the following cases:
if
(
false
&&
some_proc_that_returns_bool
()) {
    ...
}

if
(
true
||
some_proc_that_returns_bool
()) {
    ...
}

In both if statements, the some_proc_that_returns_bool procedure is not called because the expression's result does not depend of its value. This is also known as short-circuting.




Array Expressions

There are two expressions made to work with arrays.

Subscript is an expression used to access an array element at a certain index.
The subscript operator is represented by an integer inside brackets [ ] after an array name.
In the example below, arr is an array of 10 integers. If you want to change the reference of any of the elements in the array, you need to subscript that array like so:
main
:: () {
    arr : [
10
]
int
;
    arr[
0
] =
5
;
    arr[
5
] =
8
;
}

What are array subscripts and how to use them

The first subscript in the previous example (arr[0]) accesses the first element of the array (the element at the index 0). The second subscript accesses the element at the index 5 (the sixth element).

The subscript expression can be used to fill arrays manually (or inside for loops) and also query or change the values in the array. The programming exercises contain several examples and explanations of their uses.
Because the subscript is used to access the memory of the array in a specific location, it can be used to access an element out of the array bounds. For instance, in the array of 10 ints example, if you try accessing the array with an invalid index (<= -1 or >= 10, for example) you risk accessing invalid memory and crashing the program. You can read more about that here.


The other array expression is the Array Count expression. It's also referred to as array size or length.
It can be created by typing the array name and adding .count, like so
#import
"Print"


main
:: () {
    arr : [
15
]
string
;
    count := arr.count;
    
if
count ==
15
  
print
(
"True"
);
}

Getting the size of the array in Vizzcode

If you would like to know the last index from the count, all you need to do is subtract one from the count (because array indexes start from zero).
For example, an array of 10 ints can be index from 0 all the way (and including) 9.
This is very useful when iterating through the array, because by using the .count expression you make sure that if the array size is changed later on, the loop still iterates correctly to the array's end, like so:
#import
"Random"


main
:: () {
    arr : [
10
]
float
;

    
for
0
.. arr.count-
1
{
        arr[it] =
random_float_in_range
(
0
,
1
);
    }
}

main
:: () {
    arr : [
1000
]
float
;

    
for
0
.. arr.count-
1
{
        arr[it] =
random_float_in_range
(
0
,
1
);
    }
}





Precedence

Just like in math an expression is solved based on the operator precedence.
For example, because multiplications have higher precedence than additions, the following expression will resolve to 13 (First, 5 * 2 is resolved, than 3 is added to the result):
#import
"Print"


main
:: () {
    expr :=
3
+
5
*
2
;
    
if
expr ==
13
  
print
(
"True"
);
}

In the Visual Panel, the order of operation follow from the statement node all that way to the left.
Understanding in what order the operators are executed in Vizzcode

For example, the previous expression should be read as:

Add:
  • 3
  • To the result of the multiplication of 5 and 2
Another example:

Reading complex expressions naturally

Add
  • The result of the multiplication of:
    • The division of 10 by 2
    • And 5
  • To the result of the subtraction of 6 by 7;
You can use parenthesis to enforce a specific order of operation.
If you add parenthesis around the 3 + 5, part of the expression 3 + 5 * 2, the addition will be resolved first, resulting in 16;
#import
"Print"


main
:: () {
    expr := (
3
+
5
) *
2
;
    
if
expr ==
16
  
print
(
"True"
);
}

Note that in the Visual Panel, the operator nodes switch places compared to the first example in the section.

Changing the order of operations

Even if you know which operation will be executed first, it may be useful to add parenthesis in expressions for added clarity:
main
:: () {
    first :=
5
*
2
+
7
*
3
;
    second := (
5
*
2
) + (
7
*
3
);
    
if
(first == second)  
print
(
"True"
);
}

You can enter an arbitrary complex text expression in the text box of a node and it will be resolved just as if you had typed it in the Text Panel:

Typing complex expressions directly in the textbox


Here is full precedence of operators in Vizzcode, showing the order in which the expressions will be resolved from the ones that are resolved first to the ones that are resolved last:
(Operators in the same level of precedence are resolved from left-to-right, as they are found in the expression)
  • Literal Values and Name Expressions
  • Procedure Calls, Array Subscripts and Array Counts
  • Negations and Boolean Nots
  • Multiplications, Divisions and Modules
  • Additions and Subtractions
  • Comparisons (Equal To, Not Equal To, Greater Than, Greater Than or Equal To, Less Than or Equal To)
  • Ands
  • Ors