Variables
In Zard, variables are statically typed β their type must be explicitly declared at compile time. This ensures type safety and consistent behavior across all stages of compilation and execution.
Declaration
The general form for declaring a variable is:
<type> <name> = <value>;
Examples:
int age = 25;
double pi = 3.1415;
float ratio = 0.75;
string name = "Zard";
boolean active = true;
char letter = 'Z';
Supported Primitive Types
- int β whole numbers (e.g.,
42,-10) - double β double-precision floating-point numbers
- float β single-precision floating-point numbers
- string β text enclosed in quotes
- char β single character (e.g.,
'A') - boolean β logical values:
trueorfalse
Uninitialized Variables
You can declare a variable without an initial value, but it must be initialized before use:
int count;
count = 10;
print(count);
Reassignment
Variables in Zard are mutable β you can freely reassign new values after declaration.
string word = "hello";
word = "world";
print(word);
Input
Zard provides the built-in function input() to read values from the user at runtime.
The input text is automatically converted to the type of the variable receiving it.
Basic Usage
int a = input();
double b = input();
string name = input();
Type Conversion Rules
- int β expects an integer (e.g.,
10) - double β accepts decimals with
. - float β like double, converted to float
- boolean β expects
trueorfalse - char β takes only the first character
- string β returns the text exactly
Error Handling
If the user enters a value incompatible with the variableβs type, the runtime will throw a conversion error:
int x = input(); // user types: "abc"
# Error:
cannot convert "abc" to int
Example
main {
print("Enter your age: ");
int age = input();
print("Enter your height: ");
double height = input();
print("Enter your name: ");
string name = input();
print("=== User Data ===");
print(age);
print(height);
print(name);
}
β To continue learning how to use variables in computations, proceed to Expressions.