Your First Program¶
Let's write a complete CacaoLang program from scratch and explore the fundamentals.
Hello World¶
Create a file named hello.choco:
Run it:
Output:
That's it! Every CacaoLang program is just a series of statements.
Adding Variables¶
Let's make it more interesting by adding variables:
// Variables hold data
let name = "Alice";
let age = 25;
print "Hello, #{name}!";
print "You are #{age} years old.";
Output:
Interactive Program¶
Let's create an interactive program that asks for user input:
// greeting.choco
print "=== Greeting Program ===";
print "";
// Get user's name
let name = input("What is your name? ");
// Get user's age
let age_str = input("How old are you? ");
let age = int(age_str);
// Greet the user
print "";
print "Nice to meet you, #{name}!";
// Determine if adult or minor
if age >= 18 {
print "You are an adult.";
} else {
print "You are a minor.";
}
// Calculate birth year (approximately)
let current_year = 2026;
let birth_year = current_year - age;
print "You were born around #{birth_year}.";
Run it:
Example interaction:
=== Greeting Program ===
What is your name? Bob
How old are you? 30
Nice to meet you, Bob!
You are an adult.
You were born around 1996.
Number Guessing Game¶
Let's build a simple game:
// guess.choco
print "=== Number Guessing Game ===";
print "";
// Generate random number between 1 and 100
let secret = random_int(1, 100);
let attempts = 0;
let guessed = false;
print "I'm thinking of a number between 1 and 100.";
print "";
// Game loop
while guessed == false {
let guess_str = input("Your guess: ");
let guess = int(guess_str);
attempts = attempts + 1;
if guess < secret {
print "Too low! Try again.";
} else {
if guess > secret {
print "Too high! Try again.";
} else {
guessed = true;
}
}
}
print "";
print "Correct! You guessed it in #{attempts} attempts!";
print "The number was #{secret}.";
Working with Functions¶
Let's create a temperature converter:
// temperature.choco
// Function to convert Celsius to Fahrenheit
fn celsius_to_fahrenheit(celsius) {
let fahrenheit = (celsius * 9 / 5) + 32;
return fahrenheit;
}
// Function to convert Fahrenheit to Celsius
fn fahrenheit_to_celsius(fahrenheit) {
let celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
print "=== Temperature Converter ===";
print "";
print "1. Celsius to Fahrenheit";
print "2. Fahrenheit to Celsius";
print "";
let choice_str = input("Choose option (1 or 2): ");
let choice = int(choice_str);
if choice == 1 {
let temp_str = input("Enter temperature in Celsius: ");
let celsius = float(temp_str);
let fahrenheit = celsius_to_fahrenheit(celsius);
print "#{celsius}°C = #{fahrenheit}°F";
} else {
if choice == 2 {
let temp_str = input("Enter temperature in Fahrenheit: ");
let fahrenheit = float(temp_str);
let celsius = fahrenheit_to_celsius(fahrenheit);
print "#{fahrenheit}°F = #{celsius}°C";
} else {
print "Invalid choice!";
}
}
Simple Todo List¶
A console-based todo list:
// todo.choco
let todos = [];
let running = true;
fn show_menu() {
print "";
print "=== Todo List ===";
print "1. Add todo";
print "2. View todos";
print "3. Exit";
print "";
}
fn add_todo() {
let task = input("Enter task: ");
todos = push(todos, task);
print "Added: #{task}";
}
fn view_todos() {
print "";
let count = len(todos);
if count == 0 {
print "No todos yet!";
} else {
print "Your todos:";
for i in 0..count {
let num = i + 1;
let task = todos[i];
print "#{num}. #{task}";
}
}
}
// Main loop
while running == true {
show_menu();
let choice_str = input("Choose: ");
let choice = int(choice_str);
if choice == 1 {
add_todo();
} else {
if choice == 2 {
view_todos();
} else {
if choice == 3 {
running = false;
print "Goodbye!";
} else {
print "Invalid choice!";
}
}
}
}
File Processing¶
Reading and writing files:
// notes.choco
print "=== Simple Notes App ===";
print "";
let filename = "notes.txt";
// Check if notes file exists
if file_exists(filename) == true {
print "Current notes:";
print "-------------";
let content = read_file(filename);
print content;
print "-------------";
print "";
}
let action = input("Add note or clear all? (add/clear): ");
if action == "add" {
let note = input("Enter your note: ");
append_file(filename, note);
append_file(filename, "\n");
print "Note saved!";
} else {
if action == "clear" {
write_file(filename, "");
print "All notes cleared!";
} else {
print "No action taken.";
}
}
Best Practices¶
1. Use Comments¶
// This is a single-line comment
// Explain what your code does
let name = "Alice"; // Variable to store user's name
2. Use Meaningful Names¶
3. Keep Functions Small¶
// Good - single responsibility
fn calculate_total(price, quantity) {
return price * quantity;
}
fn apply_discount(total, discount_percent) {
return total * (1 - discount_percent / 100);
}
4. Handle Errors¶
let age_str = input("Enter age: ");
let age = int(age_str);
if age < 0 {
print "Error: Age cannot be negative!";
} else {
print "Age: #{age}";
}
5. Use String Interpolation¶
// Good
let message = "Hello, #{name}! You are #{age} years old.";
// Avoid concatenation when interpolation works better
let message = "Hello, " + name + "! You are " + str(age) + " years old.";
Program Structure¶
A well-organized program:
// calculator.choco
// ===== FUNCTIONS =====
fn add(a, b) {
return a + b;
}
fn subtract(a, b) {
return a - b;
}
fn multiply(a, b) {
return a * b;
}
fn divide(a, b) {
if b == 0 {
print "Error: Division by zero!";
return 0;
}
return a / b;
}
// ===== MAIN PROGRAM =====
print "=== Simple Calculator ===";
print "";
let num1_str = input("Enter first number: ");
let num1 = float(num1_str);
let operation = input("Enter operation (+, -, *, /): ");
let num2_str = input("Enter second number: ");
let num2 = float(num2_str);
let result = 0;
if operation == "+" {
result = add(num1, num2);
} else {
if operation == "-" {
result = subtract(num1, num2);
} else {
if operation == "*" {
result = multiply(num1, num2);
} else {
if operation == "/" {
result = divide(num1, num2);
} else {
print "Invalid operation!";
}
}
}
}
print "";
print "Result: #{result}";
Next Steps¶
Now that you've written your first programs:
- Learn more about data types
- Explore control flow
- Master functions
- Try GUI development