Learn Vala - 10 Code Examples & CST Typing Practice Test
Vala is a high-level, object-oriented programming language that provides modern language features while targeting the GObject type system of the GNOME platform. It compiles to C, enabling native performance and seamless integration with existing C libraries and GNOME APIs.
Learn VALA with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Vala
What is Vala?
Vala is a high-level, object-oriented programming language that provides modern language features while targeting the GObject type system of the GNOME platform. It compiles to C, enabling native performance and seamless integration with existing C libraries and GNOME APIs.
What are the primary use cases for Vala?
Desktop application development for GNOME. Library development with GObject integration. System utilities and tools. Cross-platform C code generation. Rapid prototyping with native performance
What are the strengths of Vala?
Native performance from compiled C code. Simplifies GNOME application development. Reduces boilerplate compared to plain C/GObject. Modern language features like interfaces and generics. Good integration with existing C libraries
What are the limitations of Vala?
Primarily focused on GNOME and GObject ecosystem. Smaller community compared to mainstream languages. Limited IDE support and tooling. Dependent on C toolchain for compilation. Not ideal for web or cross-platform non-GNOME projects
How can I practice Vala typing speed?
CodeSpeedTest offers 10+ real Vala code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.