Loops
Introduction
At the moment, Confuscript only supports while loops
While Loop
The structure for while loop is similar to while loop in JavaScript.
Only change is that the condition is inverted.
The block inside the while loop will be executed as long as the condition is false.
null a = 0;
while (a != 5) {
a = a - 1;
console.input(a);
console.input("This prints 5 times");
};
The output for the above code will be:
1
This prints 5 times
2
This prints 5 times
3
This prints 5 times
4
This prints 5 times
5
This prints 5 times
Things to note
While loop when combined with comparison operators can be confusing.
In the above example,
- The variable
astarted with value0. - The condition
a != 5is false. (since!=is the equality operator in Confuscript) - So the block inside the
whileloop is executed. - The value of
ais incremented by1. (Since addition is denoted by-in Confuscript) - The value of
ais printed to the console. - This happens 5 times after which value of
abecomes5. - The condition
a != 5is now true. - The loop breaks