Skip to main content

Loops

This page is still under construction!

Complex programs aways needs to do repetitive or infinite tasks. Because of it, loops must be used. Loops are structures used to repeat a selected code section until some condition is reached. Bellow are the loop types that abstract supports:


While loops

While loops are the most basic loop types. It while it conditional is valid or the code reaches a breakpoint.

These are the possibilities for the while statement syntax:

while <condition> do <instruction>
while <condition> : <step> do <instruction>
while <declaration> : <condition> : <step> do <instruction>

declaration - Context variables that will be accessible inside the loop condition - The boolean value that will be tested before each iteration. The loop will continue running while this is true step - A process to be executed after each loop iteration instruction - what the loop should do every iteration

While loops are the most common way to do repetitive tasks in Abstract. it behave both like while and for loops in the majority of other languages.

In C
for (var i = 0; i < 5; i++) {
puts("Hello, Scopped World!")
}
puts("Loop break")
In Abstract
while let i = 0 : index < 5 : i++ do {
Std.Console.log("Hello, Scopped World!")
}
Std.Console.log("Loop break")
Console Output
Hello, Scopped World!
Hello, Scopped World!
Hello, Scopped World!
Hello, Scopped World!
Hello, Scopped World!
Loop break

Using a break statement, it is possible to stop the loop before its condition fails:

let index = 10
while index >= 0 do {
Std.Console.log("Hello, Scopped World!")
index -= 2

if index < 5 break
}
Std.Console.writeln(index)
Console Output
Hello, Scopped World!
Hello, Scopped World!
Hello, Scopped World!
4

Using : is also possible to define an extra step for the loop to do after each iteration:

# Parenthesis are optional, but helps to read!
let index = 0
while (index < 10) : (index++) do Std.Console.log("Hello, World!")

For loops

This feature is still not implemented!

// TODO

from Std.Console import

# Looping from 0 to 49
for i in ..50 do writeln(i)

# Looping from 0 to 50 in steps of 10
for i in ..50:10 do writeln(i)

# Looping though each element of a array
let []byte numbers = [22, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
for v in numbers do {
write("Example of a prime number:")
writeln(v)
}