Code Examples

Learn Bloa with practical examples and best practices

Hello World

The classic first program to get started:

say("Hello, World!")

Variables and Expressions

x = 42
y = x * 2 + 10
name = "Bloa"
is_cool = true

say("x = " + str(x))
say("y = " + str(y))
say("Language: " + name)
say("Is cool? " + str(is_cool))

Functions

function factorial(n) {
  if n <= 1 {
    return 1
  } else {
    return n * factorial(n - 1)
  }
}

function greet(name, age) {
  say("Hello, " + name + "! You are " + str(age) + " years old.")
}

result = factorial(5)
say("5! = " + str(result))

greet("Alice", 25)

Classes and Inheritance

class Shape {
  function __init__(self, color) {
    self.color = color
  }

  function describe(self) {
    say("A " + self.color + " shape")
  }
}

class Circle extends Shape {
  function __init__(self, color, radius) {
    super().__init__(color)
    self.radius = radius
  }

  function area(self) {
    return 3.14159 * self.radius * self.radius
  }

  function describe(self) {
    say("A " + self.color + " circle with radius " + str(self.radius))
  }
}

circle = Circle("red", 5)
circle.describe()
say("Area: " + str(circle.area()))

Lists and Loops

# Create a list
numbers = [1, 2, 3, 4, 5]

# Add items
append(numbers, 6)
append(numbers, 7)

# Iterate
for num in numbers {
  say("Number: " + str(num))
}

# List comprehension style (manual)
squares = []
for i in range(1, 6) {
  append(squares, i * i)
}

say("Squares: " + str(squares))

File I/O

import io

# Write to file
io.write_file("example.txt", "Hello from Bloa!")

# Read from file
content = io.read_file("example.txt")
say("File content: " + content)

# Check if file exists
if io.exists("example.txt") {
  say("File exists!")
} else {
  say("File does not exist.")
}

Math Operations

import math

# Basic math
a = 16
sqrt_a = math.sqrt(a)
say("Square root of " + str(a) + " is " + str(sqrt_a))

# Trigonometry
angle = 45
sin_val = math.sin(angle * math.pi() / 180)
cos_val = math.cos(angle * math.pi() / 180)
say("sin(45°) = " + str(sin_val))
say("cos(45°) = " + str(cos_val))

# Power
result = math.pow(2, 8)
say("2^8 = " + str(result))

Exception Handling

function divide(a, b) {
  if b == 0 {
    throw "Division by zero!"
  }
  return a / b
}

try {
  result = divide(10, 0)
  say("Result: " + str(result))
} except error {
  say("Error: " + error)
}

try {
  result = divide(10, 2)
  say("Result: " + str(result))
} except error {
  say("Error: " + error)
}