Learn VALA with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
Vala Counter and Theme Toggle
int count = 0;
bool isDark = false;
void updateUI() {
stdout.printf("Counter: %d\n", count);
stdout.printf("Theme: %s\n", isDark ? "Dark" : "Light");
}
void increment() {
count++;
updateUI();
}
void decrement() {
count--;
updateUI();
}
void reset() {
count = 0;
updateUI();
}
void toggleTheme() {
isDark = !isDark;
updateUI();
}
// Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
Demonstrates a simple counter with theme toggling using Vala variables and methods.
2
Vala Random Number Generator
Random rng = new Random();
for (int i = 1; i <= 3; i++) {
int num = rng.next_int() % 100 + 1;
stdout.printf("Random %d: %d\n", i, num);
}
Generates random numbers between 1 and 100 and prints them.
3
Vala Todo List
List<string> todos = new List<string>();
void addTask(string task) {
todos.add(task);
stdout.printf("%s\n", string.Join(", ", todos));
}
void removeTask(int index) {
todos.remove_at(index);
stdout.printf("%s\n", string.Join(", ", todos));
}
// Simulate actions
addTask("Buy milk");
addTask("Write Vala code");
removeTask(0);
Maintains a simple todo list with add and remove functionality.
4
Vala Dice Roller
Random rng = new Random();
for (int i = 1; i <= 3; i++) {
int roll = rng.next_int() % 6 + 1;
stdout.printf("Roll %d: %d\n", i, roll);
}
Rolls a six-sided dice three times.
5
Vala Countdown Timer
int count = 5;
while (count >= 0) {
stdout.printf("Countdown: %d\n", count);
count--;
}
stdout.printf("Done!\n");
Counts down from 5 to 0.
6
Vala Prime Checker
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i*i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int[] nums = {7, 10, 13};
foreach (int n in nums) {
stdout.printf("%d is %s\n", n, isPrime(n) ? "Prime" : "Not Prime");
}
Checks if numbers are prime.
7
Vala Temperature Converter
float cToF(float c) { return c*9/5 + 32; }
float fToC(float f) { return (f-32)*5/9; }
stdout.printf("25°C = %.1f°F\n", cToF(25));
stdout.printf("77°F = %.1f°C\n", fToC(77));
Converts Celsius to Fahrenheit and Fahrenheit to Celsius.
8
Vala Shopping Cart
List<string> cart = new List<string>();
List<int> prices = new List<int>();
void addItem(string item, int price) {
cart.add(item);
prices.add(price);
stdout.printf("Cart: %s\n", string.Join(", ", cart));
stdout.printf("Total: %d\n", prices.sum());
}
void removeItem(int index) {
cart.remove_at(index);
prices.remove_at(index);
stdout.printf("Cart: %s\n", string.Join(", ", cart));
stdout.printf("Total: %d\n", prices.sum());
}
// Simulate actions
addItem("Apple", 2);
addItem("Banana", 3);
removeItem(0);
Adds and removes items in a shopping cart with total cost.
9
Vala Name Greeting
void greet(string name) {
stdout.printf("Hello, %s! Welcome!\n", name);
}
// Simulate actions
greet("Saurav");
greet("Alice");
greet("Bob");
Greets users by name.
10
Vala Stopwatch
int time = 0;
while (time < 5) {
stdout.printf("Stopwatch: %d seconds\n", time);
time++;
}
stdout.printf("Done!\n");
Simulates a stopwatch incrementing seconds.