File: selections.md

package info (click to toggle)
surgescript 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,200 kB
  • sloc: ansic: 15,748; sh: 61; javascript: 38; makefile: 13
file content (125 lines) | stat: -rw-r--r-- 4,307 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Switch statement
================

Introduction
------------

The `switch` statement evaluates an expression and matches it against a sequence of `case` clauses. The statements following the matching `case` clause will be executed. Example:

```cs
// variable 'name' will be compared to "Surge", "Neon" and "Tux"
// the full name of the character will be printed to the screen
switch(name)
{
    case "Surge":
        Console.print("Surge the Rabbit");
        break;

    case "Neon":
        Console.print("Neon the Squirrel");
        break;

    case "Tux":
        Console.print("Tux the Penguin");
        break;
}
```

If no matching `case` clause is found, then the statements following the `default` clause, if present, will be executed. Example:

```cs
// we're checking if variable 'amount' is 1, 5, 20 or none of these
// depending on the amount, we'll print a different message to the screen
switch(amount)
{
    case 1:
        Console.print("Green rupee");
        break;

    case 5:
        Console.print("Blue rupee");
        break;

    case 20:
        Console.print("Red rupee");
        break;

    default:
        Console.print("Unknown rupee");
        break;
}
```
If no matching `case` clause is found and no `default` clause is present either, then all `case` sections will be skipped and the first statement following the `switch` block will be executed.

There must not be more than one `default` clause per `switch` block. It's usual practice to write the `default` section at the end of the `switch` block, though it's not a requirement.

*Note:* `switch` statements are supported since SurgeScript 0.6.1.

Breaking out
------------

In the above examples, there is a `break` statement at the end of each section. Use the `break` statement to break out of the body of the `switch` statement. Execution will continue at the first statement following the `switch` block. Example:

```cs
one = 1;

switch(one)
{
    case 1:
        Console.print("Number one");
        break;

    default:
        Console.print("This line will never be executed");
        break;
}

Console.print("We're done!");

//
// Output:
//
// Number one
// We're done!
//
```

!!! tip

    `break` statements placed before the end of a section have the same effect.

Multiple matching
-----------------

Sometimes it's useful to compare an expression to multiple values and run the same code if there is a match to any of these values. This may be done by writing multiple `case` clauses, one immediately after the other, as in the example below:

```cs
// let's check if amount is 1, 5, 20 or none of these
switch(amount)
{
    case 1:
    case 5:
    case 20:
        Console.print("This is a green, a blue or a red rupee.");
        break;

    default:
        Console.print("This is an unknown rupee.");
        break;
}
```

!!! info "Empty sections"

    Any `case` clause immediately followed by another `case` clause defines an empty section. In the above example, `case 1:` and `case 5:` define empty sections. It's not useful to have a `case` clause immediately followed by a `default` clause or vice-versa, though these too define empty sections.

Additional remarks
------------------

In a `switch` statement, the `case` keyword must be followed by a constant expression[^1], typically a number or a string literal. No two `case` expressions may evaluate to the same value.

The expression of the `switch` statement is first evaluated and then compared to the expressions of the `case` clauses. There is a match between these expressions if they are evaluated to the same value, in a [strong equality](expressions.md#comparison-operators) sense. This means that both the content and the type of the evaluated expressions must be the same for a match to occur.

In SurgeScript, all `case` and `default` sections **must** be terminated by a `break` or by a `return` statement, except when they are empty. While other languages such as C/C++ do not have this requirement, forgetting to terminate a section is often a source of bugs due to the [fallthrough behavior](https://en.wikipedia.org/wiki/Switch_statement#Fallthrough). In SurgeScript, the fallthrough behavior is disallowed, except in [empty sections](#multiple-matching).

[^1]: As of SurgeScript 0.6.1, only literals are supported.