landscape

ZEN Documentation

Variables

Variables are spaces in the computer's memory where information can be stored and retrieved while the program is running. In Zen each variable has a name which identifies itself and a type that describes the kind of information stored inside of it.

Categories of variable types

Zen variable types are categorized in two groups:

  • Primitive types - they only store values, such as numbers and booleans.
  • Object types - they store a set of related variables.

Declaring variables

Variables must be declared before they are used. You declare variables by specifying their name followed by a type annotation.

Downloading SDK...

The code above can be read as:

"Declare a new variable called year of type int"

When declaring a variable, it's possible to assign a value to it. But it's also possible to declare it first and assign a value to it later.

Downloading SDK...

In this example the number of steps is assigned upon declaration, while distance in kilometers is declared first and then assigned.

Types and Safety

A variable of a specific type cannot be given a value of an unrelated type; this assures that no unintended operations happen without the knowledge of the developer.

Downloading SDK...

In this example, the Zen compiler will raise a semantic error because of the assignment of a value of type double to a variable of type integer.

How to fix

  • change the type of result to double or
  • change the value from 1.5 to 1

Shadowing

Once a variable has been declared, it can be declared again but the value it was holding before will no longer be accessible.

Downloading SDK...

The code above can be read as:

"Declare a new variable called result of type int with value 1, then redeclare the variable result being now of type string with value one"

Naming rules

Variable names can contain English letters, numbers, and underscore (_) characters but they must start with a letter or underscore (_).

Downloading SDK...

Variable names cannot contain spaces, arrows, dots or any other special character and also cannot be a reserved keyword.

Reserved keywords

Zen reserved keywords are:

  • for
  • while
  • using
  • if
  • else
  • true
  • false
  • class
  • static

Printing variables

Variables can be printed out with the functions print and println.

Downloading SDK...


Activities

Variable Declaration and Assignment

  • Declare and Assign (in one step): Create a variable called temperature of type double and assign it the value 25.5.
  • Declare then Assign (in two steps):
  • Declare a variable called isLoggedIn of type bool. On the next line, assign it the value true.
    • Print the results: Use println to print both variables.

    Downloading SDK...

    Type Safety and Error Correction

    The code below is attempting to store a precise calculation of the area of a circle into a variable named area. However, it contains a type error.

    • What is the specific error being raised by the Zen compiler in this block?
    • Apply one of the suggested fixes from the guide to correct the type error.

    Downloading SDK...

    Shadowing and Value Access

    Examine the following code block, which uses variable shadowing.

    • What is the final value and type of the variable status immediately before the println call?

    Downloading SDK...