“Certified Zig Programming Expert” – Master the language of modern systems programming
Welcome, young programmer! Have you ever wondered how computer programs are made? Programs are written in special languages called programming languages. There are many languages, like Python, JavaScript, and C. Today, we are going to learn a very special language called Zig.
Zig is a modern programming language that is simple, fast, and safe. It is used to build operating systems, games, servers, and many other important programs. Zig was created to fix problems in older languages like C. It gives you control over your computer while keeping you safe from common mistakes.
In this module, we will start from the very beginning. You don't need any experience with programming. We will learn what Zig is, how to set it up, and how to write your first programs. We will learn about variables, data types, control flow, functions, structs, and more. By the end of this module, you will be able to write simple Zig programs with confidence.
Let's begin!
After finishing this module, you will be able to:
Chidi is 13 years old and lives in Lagos. He loves computers and wants to learn how to make his own programs. One day, his uncle, who is a software engineer, told him about a new language called Zig.
“Zig is amazing,” his uncle said. “It is simple, fast, and safe. It lets you build things that are very close to the computer's heart.” Chidi was curious. “Can I learn it?” he asked. “Of course!” his uncle replied. “Let's start today.”
Chidi’s uncle helped him install Zig on his computer. They opened a code editor and wrote a simple program. It printed the words “Hello, Zig!” on the screen. Chidi was so happy. He had written his first program!
Next, they learned about variables. Chidi created a variable called “name” and put his name in it. He created another variable called “age” and put his age in it. Then they learned about functions, structs, and arrays. Every new idea was like a new toy to play with.
By the end of the day, Chidi had written five small programs. He felt like a real programmer. “Zig is fun!” he said. His uncle smiled. “This is just the beginning. Keep learning, and you will build amazing things.”
Moral of the story: Programming is like learning a new language. Zig is a simple, powerful language that anyone can learn. Start with small steps, and you will go far.
Definition: Zig is a modern programming language used to build fast, safe, and reliable software.
Why it is important: Zig is designed to fix problems in older languages and make programming simpler.
Simple explanation: Imagine you have a toolbox. Some tools are old and rusty. Zig is a new, shiny tool that works better.
Real-life example: Companies use Zig to build operating systems, web servers, and games.
School example: A student uses Zig to build a simple calculator program.
Home example: A family uses a Zig program to organize their shopping list.
Nigerian example: A Nigerian startup uses Zig to build a fast payment system.
Illustration:
Zig = Modern Programming Language
|
V
Simple, Fast, Safe
|
V
Used for:
- Operating systems
- Web servers
- Games
- Tools
Mini summary: Zig is a modern programming language. It is simple, fast, and safe. It is used to build many kinds of software.
Definition: Learning Zig means you can build powerful programs that run very fast.
Why it is important: Zig gives you control and safety at the same time.
Simple explanation: Imagine driving a car that is both fast and safe. That is Zig.
Real-life example: Game developers use Zig for high-performance games.
School example: Students learn Zig to understand how computers really work.
Home example: Hobbyists use Zig to build small tools and games.
Nigerian example: Nigerian developers use Zig for efficient software.
Illustration:
Why Learn Zig? +---------------------+ | Fast | +---------------------+ | Safe | +---------------------+ | Simple | +---------------------+ | Powerful | +---------------------+ | Modern | +---------------------+
Mini summary: Zig is fast, safe, simple, and powerful. It is a great language to learn.
Definition: Installing Zig means putting the Zig compiler on your computer.
Why it is important: You need the compiler to turn your code into a program.
Simple explanation: Like installing a new app on your phone before you can use it.
Real-life example: Developers install Zig to build software.
School example: Students install Zig on school computers.
Home example: Families install Zig on home computers.
Nigerian example: Nigerian developers install Zig to build tools.
Illustration:
Installing Zig: 1. Go to ziglang.org 2. Download the version for your computer 3. Extract the file 4. Add Zig to your PATH 5. Open a terminal and type: zig version 6. You should see the version number 🎉
Step-by-step:
zig version.Mini summary: Install Zig by downloading it and adding it to your PATH. Check with zig version.
Definition: A Zig program is a set of instructions that tells the computer what to do.
Why it is important: Writing your first program is the start of your journey.
Simple explanation: Like writing your first sentence in a new language.
Real-life example: Programmers start with “Hello, World!”
School example: Students write “Hello, World!” in class.
Home example: Families write simple programs for fun.
Nigerian example: Nigerian students write “Hello, Nigeria!”
Illustration:
hello.zig:
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, Zig!\n", .{});
}
Run it:
zig run hello.zig
Output:
Hello, Zig!
Step-by-step:
hello.zig.zig run hello.zig.Mini summary: Your first Zig program prints “Hello, Zig!” to the screen. Run it with zig run.
Definition: A variable is a box that holds a value that can change. A constant holds a value that cannot change.
Why it is important: Variables and constants let you store and use data.
Simple explanation: Imagine a labeled box. You can put things in and take them out (variable). A sealed box cannot be changed (constant).
Real-life example: A bank stores your balance in a variable.
School example: A teacher stores a student's score in a variable.
Home example: A family stores the number of eggs in a variable.
Nigerian example: A trader stores the price of rice in a variable.
Illustration:
Variables and Constants: var age: u8 = 13; // can change const pi: f32 = 3.14; // cannot change age = 14; // OK pi = 3.15; // ERROR
Step-by-step:
var to create a variable.const to create a constant.u8 for small numbers).=.Mini summary: Variables can change; constants cannot. Use var and const.
Definition: Data types describe what kind of value a variable holds: numbers, text, true/false, etc.
Why it is important: Knowing data types helps you store data correctly.
Simple explanation: Like different containers for different things: bottles for water, boxes for books.
Real-life example: Banks use numbers for money and text for names.
School example: Teachers use numbers for scores and text for names.
Home example: Families use numbers for ages and text for addresses.
Nigerian example: Businesses use numbers for prices and text for product names.
Illustration:
Common Zig Types: +----------+---------------------------+ | Type | What It Holds | +----------+---------------------------+ | u8 | Small positive number | | i32 | Whole number (positive/neg)| | f64 | Decimal number | | bool | true or false | | []u8 | Text (string) | +----------+---------------------------+
Mini summary: Zig has many data types: numbers (u8, i32, f64), booleans (bool), and text ([]u8).
Definition: Control flow lets your program make decisions.
Why it is important: Decisions make programs smart.
Simple explanation: Like choosing what to wear based on the weather.
Real-life example: A bank checks if you have enough money before you withdraw.
School example: A teacher gives a pass or fail based on a score.
Home example: A family decides to buy food based on the budget.
Nigerian example: A trader decides to sell based on the price.
Illustration:
if statement:
const score: u8 = 85;
if (score >= 70) {
std.debug.print("Pass!\n", .{});
} else {
std.debug.print("Fail.\n", .{});
}
switch statement:
const day: u8 = 3;
switch (day) {
1 => std.debug.print("Monday\n", .{}),
2 => std.debug.print("Tuesday\n", .{}),
3 => std.debug.print("Wednesday\n", .{}),
else => std.debug.print("Other day\n", .{}),
}
Step-by-step:
if to test a condition.else for the other case.switch to choose between many options.Mini summary: Use if and switch to make decisions in your program.
Definition: Loops let you repeat a block of code many times.
Why it is important: Loops save time and let you process lists.
Simple explanation: Like doing push-ups 10 times in a row.
Real-life example: A bank processes many transactions in a loop.
School example: A teacher adds up the scores of all students.
Home example: A family counts all items in a shopping list.
Nigerian example: A trader calculates total sales for the day.
Illustration:
while loop:
var i: u8 = 1;
while (i <= 5) : (i += 1) {
std.debug.print("{d}\n", .{i});
}
for loop:
const numbers = [_]u8{10, 20, 30};
for (numbers) |n| {
std.debug.print("{d}\n", .{n});
}
Step-by-step:
while to repeat while a condition is true.for to go through a list of items.Mini summary: Use while and for to repeat code. Loops are powerful tools.
Definition: A function is a block of code that does a specific job and can be reused.
Why it is important: Functions make programs cleaner and easier to reuse.
Simple explanation: Like a recipe you can use again and again.
Real-life example: A bank uses a function to calculate interest.
School example: A teacher uses a function to calculate averages.
Home example: A family uses a function to calculate budget totals.
Nigerian example: A trader uses a function to calculate prices.
Illustration:
Function Example:
fn add(a: i32, b: i32) i32 {
return a + b;
}
pub fn main() void {
const result = add(3, 4);
std.debug.print("Sum: {d}\n", .{result});
}
Step-by-step:
fn to start a function.return.Mini summary: Functions are reusable blocks of code. Use fn to define them and call them by name.
Definition: A struct groups related data together. An enum is a list of named values.
Why it is important: They help you organize your data clearly.
Simple explanation: A struct is like a bag with labeled pockets. An enum is like a list of choices on a menu.
Real-life example: A bank uses structs for customer info and enums for account types.
School example: A teacher uses structs for student info and enums for grades.
Home example: A family uses structs for family members and enums for roles.
Nigerian example: A trader uses structs for products and enums for categories.
Illustration:
Struct and Enum:
const Color = enum { red, green, blue };
const Person = struct {
name: []const u8,
age: u8,
favorite: Color,
};
const ada = Person{
.name = "Ada",
.age = 13,
.favorite = .green,
};
Step-by-step:
struct to group related data.enum to list named values.ada.name.Mini summary: Structs group data; enums list named values. They make your code organized and clear.
Definition: An array is a fixed-size list. A slice is a view into a part of an array.
Why it is important: Arrays and slices let you work with many values at once.
Simple explanation: An array is like a row of boxes. A slice points to some of those boxes.
Real-life example: A bank uses an array for transaction history.
School example: A teacher uses an array for class scores.
Home example: A family uses an array for shopping items.
Nigerian example: A trader uses an array for daily sales.
Illustration:
Array and Slice:
const numbers = [_]u8{1, 2, 3, 4, 5}; // array
const part = numbers[1..4]; // slice
for (part) |n| {
std.debug.print("{d}\n", .{n});
}
Step-by-step:
[_]type{values}.array[start..end].for to loop through them.Mini summary: Arrays hold fixed lists; slices point to parts of lists. Both are used with loops.
Definition: A string is a list of characters. In Zig, strings are slices of bytes: []const u8.
Why it is important: Strings let you work with text.
Simple explanation: Like writing a sentence using letters.
Real-life example: Banks store customer names as strings.
School example: Teachers store student names as strings.
Home example: Families store addresses as strings.
Nigerian example: Businesses store product names as strings.
Illustration:
String Example:
const name: []const u8 = "Ada";
std.debug.print("Hello, {s}!\n", .{name});
Output:
Hello, Ada!
Step-by-step:
const name = "text";.{s} in print to show it.Mini summary: Strings are slices of bytes. Use {s} to print them.
Definition: Compiling means turning your code into a program the computer can run.
Why it is important: You need to compile before you can run your program.
Simple explanation: Like baking a cake. You mix ingredients (write code), then bake (compile), then eat (run).
Real-life example: Developers compile their code into apps.
School example: Students compile their first program.
Home example: Families compile small tools.
Nigerian example: Businesses compile their software.
Illustration:
Compile and Run: zig build-exe hello.zig // compile ./hello // run (Linux/Mac) hello.exe // run (Windows) Or simply: zig run hello.zig // compile and run
Step-by-step:
zig run hello.zig.zig build-exe to create an executable.Mini summary: Use zig run to compile and run in one step, or zig build-exe to create a program.
Definition: Mistakes happen. Knowing them helps you avoid them.
Why it is important: A small mistake can stop your program.
Simple explanation: Like a spelling mistake in a sentence.
Real-life example: Developers use tools to find mistakes.
School example: Students learn from errors in their code.
Home example: Families check their code twice.
Nigerian example: Businesses test their code carefully.
Table of common mistakes:
| Mistake | What Happens | How to Fix |
|---|---|---|
| Missing semicolon | Error message | Add ; at the end |
| Wrong type | Error message | Use the correct type |
Forgetting const/var | Error message | Add the keyword |
| Unused variable | Warning/error | Remove or use it |
| Forgetting imports | Unknown name | Add @import |
| Mismatched types | Error | Convert types properly |
Mini summary: Common mistakes include missing semicolons and wrong types. Read error messages carefully.
Definition: Best practices are good habits that make your code clean and safe.
Why it is important: Good habits prevent bugs and save time.
Simple explanation: Like keeping your room tidy. It is easier to find things.
Real-life example: Developers write clean code for their teams.
School example: Students write neat code for projects.
Home example: Families keep their tools organized.
Nigerian example: Businesses write clear code for maintenance.
List of best practices:
const by default.defer to clean up resources.zig fmt.Mini summary: Best practices: use const, clear names, short functions, handle errors, test often.
| Word | Simple Definition |
|---|---|
| Zig | A modern programming language. |
| Compiler | A tool that turns code into a program. |
| Variable | A box that holds a value that can change. |
| Constant | A box that holds a value that cannot change. |
| Data type | What kind of value a variable holds. |
| Function | A reusable block of code. |
| Struct | A group of related data. |
| Enum | A list of named values. |
| Array | A fixed-size list. |
| Slice | A view into part of an array. |
| String | A list of characters. |
| Loop | Code that repeats. |
| Condition | A test that is true or false. |
| Parameter | An input to a function. |
| Return | The result a function gives back. |
zig version.var and const.fn.zig run.zig version.hello.zig.zig run hello.zig.var or const.=.;.fn.return.while or for.comptime for compile-time code.try and catch for errors?std?var for variables and const for constants.if, switch, while, and for for control flow.zig run.var or const.const by default.defer for cleanup.zig fmt.
hello.zig
+---------------------------+
| @import("std") |
+---------------------------+
| pub fn main() void { |
| print("Hello, Zig!") |
| } |
+---------------------------+
var age: u8 = 13; // Can change const pi: f32 = 3.14; // Cannot change
if (score >= 70) {
Pass
} else {
Fail
}
while (i <= 5) : (i += 1) {
print(i)
}
Input → Function → Output
3, 4 add() 7
Module 1: Zig Basics
|
V
Module 2: Memory & Errors
|
V
Module 3: Advanced Features
|
V
Module 4: Real Projects
|
V
Zig Expert 🎉
| Feature | Zig | Python | C |
|---|---|---|---|
| Speed | Very Fast | Slow | Very Fast |
| Safety | High | High | Low |
| Memory | Manual | Automatic | Manual |
| Learning | Medium | Easy | Hard |
| Feature | var | const |
|---|---|---|
| Can change? | Yes | No |
| Use for | Changing values | Fixed values |
| Best practice | Only when needed | Default choice |
| Feature | Array | Slice |
|---|---|---|
| Size | Fixed | Can vary |
| Owns data | Yes | No |
| Best for | Fixed lists | Passing parts |
| Feature | Struct | Enum |
|---|---|---|
| Purpose | Group data | List choices |
| Example | Person { name, age } | Color { red, green } |
| Use | Complex objects | Named values |
Lesson 1: Zig is a modern programming language.
Lesson 2: Zig is fast, safe, simple, and powerful.
Lesson 3: Install Zig from ziglang.org and check with zig version.
Lesson 4: Your first program prints “Hello, Zig!”.
Lesson 5: Use var for variables and const for constants.
Lesson 6: Zig types include u8, i32, f64, bool, []u8.
Lesson 7: Use if and switch for decisions.
Lesson 8: Use while and for for loops.
Lesson 9: Functions are reusable blocks of code.
Lesson 10: Structs group data; enums list named values.
Lesson 11: Arrays hold lists; slices view parts.
Lesson 12: Strings are slices of bytes.
Lesson 13: Compile and run with zig run.
Lesson 14: Common mistakes include missing semicolons.
Lesson 15: Best practices: use const, clear names, test often.
Congratulations! You have finished Module One of the Certified Zig Programming Expert course. You learned what Zig is and why it is special. You learned how to install Zig and write your first program. You learned about variables, constants, and data types. You learned control flow with if, switch, while, and for. You learned about functions, structs, enums, arrays, slices, and strings. You learned how to compile and run Zig programs. You learned common mistakes and best practices. Most importantly, you now have a solid foundation in Zig. In the next module, you will learn about memory management and error handling. Keep practising, and you will become a Zig expert!
zig run file.zig.fn with parameters and return type.try and catch (in later modules).Match the term to its meaning.
| Term | Meaning |
|---|---|
| 1. Zig | A. A group of related data |
| 2. Variable | B. A modern programming language |
| 3. Function | C. A box that can change |
| 4. Struct | D. A reusable block of code |
| 5. Enum | E. A list of named values |
Answers: 1-B, 2-C, 3-D, 4-A, 5-E
const name = "Ada";var score: u8 = 70;if statement.for loop with a function.enum.Title: “Write a Simple Calculator Together”
Instructions: In groups of 3–4, write a Zig program that adds, subtracts, multiplies, and divides two numbers. One person writes the code, one person tests it, one person fixes errors, and one person presents. Share your calculator with the class.
Goal: Practice using functions, variables, and control flow.
Task: Write a Zig program that:
if statement to check if your age is above 12.Hint: Use std.debug.print with {s} and {d}.
Project: “My First Zig Program”
Write a Zig program that does the following:
Example output:
Hello, my name is Ada. I am 13 years old. I am a teenager. Count: 1 2 3 4 5 Sum: 7 Person: Ada, age 13
Assignment: Write a Zig program that acts as a simple quiz. The program should:
if statements to check answers.Submit: Your .zig file and a screenshot of the output.
var and const for variables and constants.zig run.In Module Two, we will learn about memory management and error handling in Zig. We will cover:
To prepare, make sure you have completed the practical assignment and have your Zig environment ready. Review the key vocabulary. Think about how memory works in a computer. Bring your curiosity!
See you in Module Two!
End of Module One – Certified Zig Programming Expert