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

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

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.