Nox

Tutorial

3. Control flow

if#

Nox
temperature: int = 23
if temperature > 30:
    print("hot")
elif temperature > 15:
    print("pleasant")
else:
    print("cold")
Output
pleasant

Combine conditions with and, or, not; chained comparisons work as in Python:

Nox
x: int = 5
if 0 < x < 10 and not x == 7:
    print("in range")
label: str = "even" if x % 2 == 0 else "odd"
print(label)
Output
in range
odd

while#

Nox
n: int = 1
while n < 100:
    n = n * 3
print(n)
Output
243

for and range#

range(stop), range(start, stop) and range(start, stop, step) produce integers:

Nox
for i in range(3):
    print(i)
for i in range(10, 0, -4):
    print(i)
Output
0
1
2
10
6
2

A for loop also walks the characters of a string, the elements of a list and the keys of a dictionary:

Nox
for ch in "héy":
    print(ch)
for word in ["red", "green"]:
    print(word.upper())
Output
h
é
y
RED
GREEN

break and continue#

Nox
numbers: list[int] = [4, -1, 7, 200, 3]
total: int = 0
for n in numbers:
    if n < 0:
        continue       # skip negatives
    if n > 100:
        break          # stop at the first big value
    total += n
print(total)
Output
11

Variables live in the whole function#

A variable created inside an if or a loop is visible after it, because Nox has function scope, not block scope:

Nox
for i in range(3):
    last: int = i * i
print(last)
Output
4

A complete example: FizzBuzz#

Nox
for n in range(1, 16):
    if n % 15 == 0:
        print("FizzBuzz")
    elif n % 3 == 0:
        print("Fizz")
    elif n % 5 == 0:
        print("Buzz")
    else:
        print(n)
Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz