Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 530abf2

Browse files
committedAug 10, 2023
Update docs to add switch statement/expression override operators
1 parent 953ce88 commit 530abf2

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed
 

‎docs/reference/switch.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,40 @@ Amun has both switch statement and expression
22

33
## Switch Statement
44

5-
Switch statement is the same like in C/C++ it execute block if his case value
6-
match the condition value, if no value match it will execute an optional default block
5+
By Switch statement is used to execute the branch if his case value is equal to the argument,
6+
but you can also override the equality checks with any comparisons operator such as (==, !=, >, >=, <, <=).
7+
and if no value match it will return the value of default block.
78

89
```
910
switch (10) {
1011
1 -> printf("One");
1112
2, 4 -> printf("Two or four");
1213
else -> printf("Else");
1314
}
15+
16+
var x = 10;
17+
switch (x, >) {
18+
1 -> printf("x > 1");
19+
2, 4 -> printf("x > 2 or x > 4");
20+
else -> printf("Else");
21+
}
1422
```
1523

1624
## Switch Expression
1725

18-
Switch expression is used to return a value of branch if his case match the condition value, if no value match it will return the value of default block
26+
Switch expression is used to return a value of branch if his case value is equal to the argument, but like the switch staement you can also override the operator by any comparisons operator such as (==, !=, >, >=, <, <=).
1927

2028
```
2129
var result = switch (10) {
2230
2, 4, 6, 8 -> true;
2331
else -> false;
2432
};
33+
34+
var x = 10;
35+
var result2 = switch (x, <) {
36+
2, 4, 6, 8 -> true;
37+
else -> false;
38+
};
2539
```
2640

2741
## Compile time switch expression

0 commit comments

Comments
 (0)
Please sign in to comment.