Control Flow in V (Vlang)
Learn conditionals, match expressions, and loops in V with practical Docker-ready examples covering if/else, match, the unified for loop, and loop control
Control flow determines the order in which your program’s statements run. It is how a program makes decisions and repeats work. V takes a deliberately minimal approach: where most languages scatter switch, while, and do-while across several keywords, V provides a single match construct and a single loop keyword — for. This fits V’s design philosophy of being a small language with no hidden control flow that you can learn in a weekend.
As a multi-paradigm language with imperative roots, V uses familiar structured control flow, but with its own opinions. There are no parentheses around conditions, braces are always required, match does not fall through, and there is deliberately no ternary operator. Instead, both if and match are expressions — they evaluate to a value — so a one-line conditional assignment reads naturally without ? :.
In this tutorial you will learn how to branch with if/else, match values and ranges with match, iterate with the versatile for loop, range over arrays, and control loops with break and continue — including V’s labeled loops.
Conditionals: if / else
V’s if statement needs no parentheses around the condition, but the braces are mandatory. Because if is an expression, it can also produce a value directly — V’s idiomatic replacement for the missing ternary operator.
Create a file named conditionals.v:
| |
The expression form (grade := if ... { } else { }) keeps short conditional assignments compact while staying explicit. Since V has no null and requires every branch to return a value of the same type, the compiler guarantees grade is always initialized.
Match Expressions
V’s match replaces the C-style switch. It never falls through, so there is no break to forget, and a single branch can list several values separated by commas. Like if, match is an expression that can return a value, and it can match against inclusive integer ranges written with ....
Create a file named match_expr.v:
| |
Because V’s match requires an else branch (or exhaustive coverage of every possible value), you cannot accidentally leave a case unhandled. This is the same safety idea behind switch in Go, taken a step further by making the match an expression.
Loops: the for Statement
V has a single looping keyword: for. It covers every looping need through several forms — the classic three-component loop, a condition-only “while” form, and a bare infinite loop. Remember that V is immutable by default, so any variable a loop reassigns must be declared mut.
Create a file named loops.v:
| |
There is no while keyword in V — for condition { } does the job. Likewise, for { } with no clauses is an infinite loop that you exit with break. One keyword, three behaviors.
Ranging Over Collections and Loop Control
The in clause iterates over ranges and arrays. A range written 0 .. 5 is half-open (it includes 0 but stops before 5). Combined with break and continue — and V’s labeled loops — this handles most real-world iteration.
Create a file named range_loops.v:
| |
The for index, value in array form is the idiomatic way to walk an array in V. Labeled loops (outer:) work with both break and continue, giving you precise control over nested loops without resorting to flag variables.
Running with Docker
You can run every example without installing V locally by using the official thevlang/vlang:alpine image.
| |
Expected Output
Running conditionals.v:
The weather is pleasant
Grade: B
Running match_expr.v:
Saturday is weekend
Good afternoon
Two
Running loops.v:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
n is now 5
n is now 2
n is now 1
Total reached 30
Running range_loops.v:
0 1 2 3 4
0: apple
1: banana
2: cherry
Even: 2
Even: 4
Even: 6
Stopping at 2 x 3
Key Concepts
- No parentheses, mandatory braces — Conditions are written without surrounding
(), but{ }is always required, even for a single statement. ifis an expression —x := if cond { a } else { b }assigns a value directly; this is V’s replacement for the ternary operator it deliberately omits.matchnever falls through — Branches do not leak into the next, so there is nobreakto forget. A branch can list several comma-separated values.matchis exhaustive — You must cover every case or provide anelse, so the compiler catches unhandled values.- One loop keyword —
forcovers C-style loops, while-style loops (for cond {}), infinite loops (for {}), andin-based iteration. There is nowhileordo-while. - Ranges and
in—for i in 0 .. 5iterates a half-open range, whilefor i, v in arryields each index and value. - Immutable by default — A variable a loop reassigns must be declared
mut; otherwise the compiler rejects the program. breakandcontinuewith labels — Labeled loops (outer:) let you break out of or continue an outer loop directly, avoiding flag variables.
Running Today
All examples can be run using Docker:
docker pull thevlang/vlang:alpine
Comments
Loading comments...
Leave a Comment