While Loops

Last updated: Mar 4th, 2018

Introduction

While loops loop though a block of code as long as the specified condition holds true. Its syntax is as follows.


while (condition) {
    // code block to be executed
}
                                    

Example

In the following example, the code in the loop will run over and over again, as long as the variable i is less than 10.


while (i < 10) {
    text = text + "The number is " + i;
    i = i + 1;
}
                                    

If you forget to increase the variable used in the condition, the loop will never end, resulting in an infinite loop.

What's next?

The next part discusses for loops in Cuneiform.