If Statement
The if statement in Zard controls the execution of code
based on a boolean condition.
It is used to express decisions and branching logic.
Basic Syntax
if (condition) {
// executed only if condition is true
}
The condition must evaluate to a boolean value. Zard allows conditions based on primitive values, struct fields, list elements, and nested data access.
If with Simple Variables
The most basic usage of if involves simple variables and numeric comparisons.
int x = 10;
if (x > 5) {
println("x is greater than 5");
}
If and Else
The else block runs when the condition is false.
int x = 3;
if (x >= 10) {
println("x is greater or equal to 10");
} else {
println("x is less than 10");
}
If, Else If and Else
Use else if to check multiple conditions in sequence.
Only the first true condition is executed.
int x = 7;
if (x > 10) {
println("x is greater than 10");
} else if (x > 5) {
println("x is greater than 5");
} else {
println("x is 5 or less");
}
If with String Variables
string name = "Alice";
if (name == "Bob") {
println("Hello Bob");
} else if (name == "Alice") {
println("Hello Alice");
} else {
println("Unknown user");
}
Condition Using Struct Fields
Conditions may access fields inside structs, including nested structs.
if (pessoa.idade >= 18) {
println("Maior de idade");
}
Condition with Nested Structs
Zard supports deep field access inside conditions without intermediate variables.
if (pessoa.endereco.cidade == "São Paulo") {
println("Mora em São Paulo");
}
Condition Using Lists
List elements can be accessed directly inside an if condition.
if (pessoas.get(1).idade < 18) {
println(pessoas.get(1).nome);
println(" é menor de idade");
}
Complete Example
This example demonstrates conditional logic using structs, lists, and nested field access.
main {
Shd Struct Endereco {
string rua;
string cidade;
}
Struct Pessoa {
string nome;
int idade;
Struct Endereco endereco;
}
Pessoa p1;
p1.nome = "Alice";
p1.idade = 25;
Endereco e1;
e1.rua = "Rua A";
e1.cidade = "São Paulo";
p1.endereco = e1;
if(p1.nome == "Alice")
{
println("sim o nome era alice");
}
if (p1.endereco.cidade == "São Paulo") {
println("A primeira pessoa mora em São Paulo");
}
List pessoas = (p1);
Pessoa p2;
p2.nome = "Bob";
p2.idade = 17;
Endereco e2;
e2.rua = "Av. Central";
e2.cidade = "Rio de Janeiro";
p2.endereco = e2;
pessoas.add(p2);
println(pessoas.get(0).nome);
println(pessoas.get(0).endereco.rua);
println(pessoas.get(0).endereco.cidade);
println(pessoas.get(1).nome);
println(pessoas.get(1).endereco.rua);
println(pessoas.get(1).endereco.cidade);
if (pessoas.get(0).endereco.cidade == "São Paulo") {
println("A primeira pessoa mora em São Paulo");
}
if (pessoas.get(1).idade < 18) {
println(pessoas.get(1).nome);
println(" é menor de idade");
}
int i = 0;
while (i < pessoas.size()) {
println("Pessoa:");
println(pessoas.get(i).nome);
println(pessoas.get(i).endereco.rua);
println(pessoas.get(i).endereco.cidade);
i = i + 1;
}
}
Key Rules
- The condition must evaluate to
trueorfalse - Conditions support string and numeric comparisons
- Struct and list access is allowed inside conditions
- Blocks are executed only when the condition is true