Control Flow & Matching
SP supports standard branching constructs, loops, and modern pattern matching.
Conditional If-Else
sp
1if (score >= 90) {2 println("Grade: A");3} else if (score >= 75) {4 println("Grade: B");5} else {6 println("Grade: C");7}
Pattern Matching (`match`)
The match construct evaluates an expression against multiple branch patterns. It is an expression that yields a value:
sp
1let statusCode = 200;23let message = match (statusCode) {4 200 => "OK - Request successful",5 404 => "Not Found",6 500 => "Internal Server Error",7 _ => "Unknown status"8};910println(message);
Loops (`for`, `for-in`, `while`)
sp
1// 1. Classic for loop2for (int i = 0; i < 5; i = i + 1) {3 println("Index: ${i}");4}56// 2. Collection for-in loop7let fruits = ["Apple", "Orange", "Banana"];8for (string f in fruits) {9 println("Fruit: ${f}");10}1112// 3. While loop13var n = 3;14while (n > 0) {15 n = n - 1;16}