Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.
Austin Lehman edited this page Oct 23, 2022 · 1 revision

Loops are used to run the same code over and over again or to iterate over a group of items. Aussom features two types of loops, the for loop and the while loop.

While Loop:

The while loop is a simple loop that continues as long as the provided conditional statement is true. The while loop checks the conditional statement first, then executes the code within it's block, and then it does it again indefinitely. Here are just a few examples.

while(true) {
	// Infinite loop, will never stop because true is always true. Avoid infinite loops!
}

while(1) {
	// Also infinite loop.
}

while(0) {
	// Will never execute
}

i = 0;
while(i < 10) {
	// This code will run 10 times as i goes from 0 to 9.
	i++;	// Increment i by one.
}

For Loop:

The for loop is much more common and can be used a few different ways. The first 3 examples below demonstrate the classic for loop where you define a variable, check a condition, and then do some operation (normally increment or decrement a variable). The bottom two versions use the for loop as an iterator for a list and a map. When iterating a list, each item is placed into the first variable. In the case of a map, the map key is placed into the first variable.

// Classic for loop with definition, condition and operator.
for(i = 0; i < 10; i++) {
	// Will run 10 times as i goes from 0 to 9.
}

for(i = 50; i < 55; i++) {
	// Will run 5 times as i goes from 50 to 54.
}

i = 0;
for(;i < 10;) {
	// This is basically a while loop. This is valid syntax though.
	i++;
}

// For loop as item iterator.
Assuming 'lst' is a list with 10 numbers, 1-9 in it.
for(num : lst) {
	// num equals the item in the list.
}

Assuming 'mp' is a map with 5 items in it.
for(itemKey : mp) {
	// itemKey is the key portion of the key value pair in the map. You can then get
	// the value for that key by doing this.
	// value = mp[itemKey];
}

Clone this wiki locally