Skip to content

Latest commit

 

History

History
44 lines (34 loc) · 926 Bytes

File metadata and controls

44 lines (34 loc) · 926 Bytes

While

One way to make a loop in code is to use while.

~void main() {
int x = 5;
while (x != 0) {
    IO.println(x);
    x--;
}
~}

You write while followed by a condition inside of ( and ) and some code inside of { and }.

while (CONDITION) {
    <CODE HERE>
}

If the condition evaluates to true then the code inside of { and } will run. After that code runs, the condition will be evaluated again. If it still evaluates to true then the code { and } will run again.

This will continue until the code in the condition evaluates to false.

~void main() {
int glassesOfMilk = 99;
while (glassesOfMilk > 0) {
    IO.println(
        glassesOfMilk + " glasses of milk left"
    );

    glassesOfMilk--;
}
~}

If a loop is made with while we call it a "while loop."1

Footnotes

  1. "We called him Tortoise because he taught us." - Lewis Carroll