Control Flow Structures
Harneet provides a variety of control flow structures to direct the execution of your program.
Control Flow Rules
- If Conditions: Must evaluate to a truthy or falsy value
- Case Matching: Exact value equality (==) is used for case comparison
- Type Consistency: All cases in a switch must use the same data type
- Default Case: Optional catch-all for unmatched values
- Multi-Value Cases: Use comma-separated values:
case 1, 2, 3 { } - No Fall-through: Each case block executes independently (like Go, unlike C)
If-Else Statements
If-else statements allow you to execute code conditionally.
Basic If-Else
| Basic If-Else | |
|---|---|
Else-If Chaining
| Else-If Chaining | |
|---|---|
Switch Statements
Switch statements provide a way to execute different code blocks based on the value of an expression.
Use pattern matching
Though switches are present in Harneet, we urge you to use Pattern matching. Pattern matching is a safe way of creating conditional flows when compared to switches. See more here.
Supported Types in Switch
- Integers:
case 1, 2, 3 { ... } - Strings:
case "hello", "world" { ... } - Mixed types not supported: Each switch statement must use consistent types
Integer Switch
| Integer Switch | |
|---|---|
String Switch
| String Switch | |
|---|---|
Multi-Value Cases
| Multi-Value Cases | |
|---|---|
For Loops
Harneet uses a single for construct (like Go) that covers multiple patterns:
- C-style counted loop:
for var i = 0; i < 10; i = i + 1 { ... } - Condition-only loop (while-style):
for condition { ... } - For-in loops for iterables: Modern iteration with Python-like syntax
- Range loops:
for i in range(10) { ... } - Array iteration:
for item in array { ... }orfor i, item in array { ... } - Map iteration:
for key in map { ... }orfor key, value in map { ... } - Enumeration:
for pair in enumerate(array) { ... }
There is no separate while keyword.
C-style For Loop
| C-style For Loop | |
|---|---|
Condition-only For Loop (while-style)
Use a for with only a condition to loop while it remains true.
| Condition-only For Loop | |
|---|---|
For-in Loops
Harneet provides comprehensive for-in loop functionality for iterating over various data structures with Python-like syntax and Go-like performance.
Range Loops
Use range(n) to iterate over a sequence of numbers from 0 to n-1:
| Range Loops | |
|---|---|
Array Iteration
Iterate over array elements with single or multiple variables:
| Array Iteration | |
|---|---|
Map Iteration
Iterate over map keys or key-value pairs:
| Map Iteration | |
|---|---|
Enumeration with enumerate()
Use the enumerate() builtin function to get index-value pairs:
Nested Loops and Complex Data
For-in loops work with nested data structures:
Break and Continue
Break and continue statements work in all for-in loop types:
| Break and Continue | |
|---|---|
Supported Iteration Patterns
| Pattern | Syntax | Variables Bound | Use Case |
|---|---|---|---|
| Range | for i in range(n) | i: integer | Counting loops |
| Array Simple | for item in array | item: array element | Processing items |
| Array Indexed | for i, item in array | i: index, item: element | Need both index and value |
| Map Keys | for key in map | key: map key | Processing keys only |
| Map Pairs | for k, v in map | k: key, v: value | Processing key-value pairs |
| Enumerate | for pair in enumerate(array) | pair: [index, value] | Manual index extraction |