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.
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.
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.
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
doubleor - change the value from
1.5to1
Shadowing
Once a variable has been declared, it can be declared again but the value it was holding before will no longer be accessible.
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 (_).
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.
Activities
Variable Declaration and Assignment
- Declare and Assign (in one step): Create a variable called
temperatureof typedoubleand assign it the value25.5. - Declare then Assign (in two steps): Declare a variable called
- Print the results: Use
printlnto print both variables.
isLoggedIn of type bool.
On the next line, assign it the value true.
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.
Shadowing and Value Access
Examine the following code block, which uses variable shadowing.
- What is the final value and type of the variable
statusimmediately before theprintlncall?