summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2025-02-20 12:08:45 +0100
committerJo-Philipp Wich <jo@mein.io>2025-02-20 12:08:45 +0100
commit60f05ac60bfe5c4c5b013b7ae69b9f2913a6aa90 (patch)
tree49ec884b5875e3f4d32c301f5b44846559a6cdb7
parent9260bd5354bf2cf39e3c125f776ed832f28bc356 (diff)
docs: cover switch statement in syntax article
Ref: #277 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--docs/tutorials/02-syntax.md56
1 files changed, 53 insertions, 3 deletions
diff --git a/docs/tutorials/02-syntax.md b/docs/tutorials/02-syntax.md
index c140804..5510343 100644
--- a/docs/tutorials/02-syntax.md
+++ b/docs/tutorials/02-syntax.md
@@ -284,7 +284,56 @@ a counting `for` loop that is a variation of the `while` loop.
%}
```
-#### 3.3. Alternative syntax
+#### 3.3. Switch statement
+
+The `switch` statement selects code blocks to execute based on an expression's
+value. Unlike other control statements, it doesn't support alternative syntax
+with colons and end keywords.
+
+Switch statements use strict equality (`===`) for comparison. Case values can be
+arbitrary expressions evaluated at runtime. Without a `break` statement,
+execution continues through subsequent cases.
+
+The optional `default` case executes when no case matches. It's typically placed
+last but will only execute if no previous matching case was found.
+
+The entire switch statement shares one block scope. Variables declared in any
+case are visible in all cases. Curly braces may be used within cases to create
+case-specific variable scopes.
+
+```javascript
+{%
+ day = 3;
+ specialDay = 1;
+
+ switch (day) {
+ case specialDay + 2:
+ print("Wednesday\n");
+ break;
+
+ case 1:
+ let message = "Start of week";
+ print(message + "\n");
+ break;
+
+ case 2: {
+ let message = "Tuesday";
+ print(message + "\n");
+ break;
+ }
+
+ case 4:
+ case 5:
+ print("Thursday or Friday\n");
+ break;
+
+ default:
+ print("Weekend\n");
+ }
+%}
+```
+
+#### 3.4. Alternative syntax
Since conditional statements and loops are often used for template formatting
purposes, e.g. to repeat a specific markup for each item of a list, ucode
@@ -312,7 +361,8 @@ Printing a list:
{% endfor %}
```
-For each control statement type, a corresponding alternative end keyword is defined:
+For each control statement type except switch statements, a corresponding
+alternative end keyword is defined:
- `if (...): ... endif`
- `for (...): ... endfor`
@@ -629,4 +679,4 @@ in the table below.
Operators with a higher precedence value are evaluated before operators with a
lower precedence value. When operators have the same precedence, their
associativity determines the order of evaluation
-(e.g., left-to-right or right-to-left). \ No newline at end of file
+(e.g., left-to-right or right-to-left).