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 the language 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 tq. it behave both like while and for loops in the majority of other languages.

In C
for (int i = 0; i < 5; i++) {
puts("Hello, Scoped World!")
}
puts("Loop break")
In tq
while i = 0 : index < 5 : i++ do {
Std.Console.log("Hello, Scoped World!")
}
Std.Console.log("Loop break")
Console Output
Hello, Scoped World!
Hello, Scoped World!
Hello, Scoped World!
Hello, Scoped World!
Hello, Scoped 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, Scoped World!")
index -=-= 2

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

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

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

For loops

This feature is still not implemented!

from Std.Console import

func ... {
# 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)
}
}