While Loop

The while loop in Zard repeatedly executes a block of code as long as a condition evaluates to true.


Basic Syntax


while (condition) {
    // executed repeatedly while condition is true
}
      

The condition is evaluated before each iteration. If the condition is false, the loop stops.


While with Simple Variables

The most common use of while is counting or iterating using a variable.


int i = 0;

while (i < 5) {
    println(i);
    i = i + 1;
}
      

While with a Decreasing Counter


int i = 10;

while (i > 0) {
    println(i);
    i--;
}
      

While with Lists

Lists are commonly iterated using an index variable.


int i = 0;

while (i < pessoas.size()) {
    println(pessoas.get(i).nome);
    i++;
}
      

The loop runs until all elements in the list are processed.


While with Structs

Inside a loop, struct fields can be accessed normally.


int i = 0;

while (i < pessoas.size()) {
    Pessoa p = pessoas.get(i);
    println(p.nome);
    println(p.idade);
    i = i + 1;
}
      

While with Nested Structs


int i = 0;

while (i < pessoas.size()) {
    println(pessoas.get(i).endereco.rua);
    println(pessoas.get(i).endereco.cidade);
    i++;
}
      

Common Pitfalls


Key Rules