You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A`switch`statement can replace multiple `if`checks.
3
+
Eine`switch`Anweisung kann mehrere `if`Anweisungen ersetzen.
4
4
5
-
It gives a more descriptive way to compare a value with multiple variants.
5
+
Sie bietet eine anschauliche Möglichkeit, einen Wert mit mehreren Varianten zu vergleichen.
6
6
7
-
## The syntax
7
+
## Die Syntax
8
8
9
-
The`switch`has one or more `case`blocks and an optional default.
9
+
Die`switch`Anweisung hat eine oder mehrere `case`Blöcke und einen optionalen default Block.
10
10
11
-
It looks like this:
11
+
Das sieht wie folgt aus:
12
12
13
13
```js no-beautify
14
14
switch(x) {
@@ -26,71 +26,73 @@ switch(x) {
26
26
}
27
27
```
28
28
29
-
-The value of`x`is checked for a strict equality to the value from the first `case`(that is,`value1`) then to the second (`value2`) and so on.
30
-
-If the equality is found, `switch`starts to execute the code starting from the corresponding `case`, until the nearest `break`(or until the end of`switch`).
31
-
-If no case is matched then the `default`code is executed (if it exists).
29
+
-Der Wert von`x`wird auf strikte Gleichheit mit dem Wert aus dem ersten `case`verglichen, (das ist`value1`) dann mit dem zweiten (`value2`) und so weiter.
30
+
-Wenn eine Übereinstimmung gefunden wurde, führt `switch`den Code, ausgehend vom entsprechenden `case`, bis zum nächsten `break`aus (oder bis zum Ende der`switch` Anweisung).
31
+
-Wenn kein `case` zutrifft, wird der Code im `default`Block ausgeführt (falls dieser existiert).
32
32
33
-
## An example
33
+
## Ein Beispiel
34
34
35
-
An example of`switch`(the executed code is highlighted):
35
+
Ein Beispiel der`switch`Anweisung (der ausgeführte Code ist hervorgehoben):
36
36
37
37
```js run
38
38
let a =2+2;
39
39
40
40
switch (a) {
41
41
case3:
42
-
alert( 'Too small' );
42
+
alert( 'Zu klein' );
43
43
break;
44
44
*!*
45
45
case4:
46
-
alert( 'Exactly!' );
46
+
alert( 'Exakt!' );
47
47
break;
48
48
*/!*
49
49
case5:
50
-
alert( 'Too big' );
50
+
51
+
alert( 'Zu gross' );
52
+
51
53
break;
52
54
default:
53
-
alert( "I don't know such values" );
55
+
alert( "Ich kenne keinen solchen Werte" );
54
56
}
55
57
```
56
58
57
-
Here the `switch`starts to compare `a`from the first`case`variant that is `3`. The match fails.
59
+
`switch`beginnt `a`mit der ersten`case`Alternative, welche `3` ist, zu vergleichen. Der Vergleich schlägt fehl.
58
60
59
-
Then `4`. That's a match, so the execution starts from `case 4`until the nearest`break`.
61
+
Dann wird mit `4` verglichen. Übereinstimmung. Der Code zwischen `case 4`bis zum nächsten`break` wird ausgeführt.
60
62
61
-
**If there is no `break`then the execution continues with the next`case` without any checks.**
63
+
**Wenn es keinen `break`gibt, wird die Ausführung mit dem nächsten`case`, ohne jegliche Überprüfung, fortgesetzt.**
62
64
63
-
An example without`break`:
65
+
Ein Beispiel ohne`break`:
64
66
65
67
```js run
66
68
let a =2+2;
67
69
68
70
switch (a) {
69
71
case3:
70
-
alert( 'Too small' );
72
+
alert( 'Zu klein' );
71
73
*!*
72
74
case4:
73
-
alert( 'Exactly!' );
75
+
alert( 'Exakt!' );
74
76
case5:
75
-
alert( 'Too big' );
77
+
alert( 'Zu gross' );
76
78
default:
77
-
alert( "I don't know such values" );
79
+
alert( "Ich kenne keinen solchen Werte" );
78
80
*/!*
79
81
}
80
82
```
81
83
82
-
In the example above we'll see sequential execution of three`alert`s:
84
+
Im obigen Beispiel sehen wir die sequentielle Ausführung von drei`alert`s:
83
85
84
86
```js
85
-
alert( 'Exactly!' );
86
-
alert( 'Too big' );
87
-
alert( "I don't know such values" );
87
+
alert( 'Exakt!' );
88
+
alert( 'Zu gross' );
89
+
alert( "Ich kenne keinen solchen Werte" );
88
90
```
89
91
90
-
````smart header="Any expression can be a `switch/case`argument"
91
-
Both `switch`and`case`allow arbitrary expressions.
92
+
````smart header="Jeder Ausdruck kann ein `switch/case`Argument sein"
93
+
`switch`und`case`erlauben beliebige Ausdrücke.
92
94
93
-
For example:
95
+
Zum Beispiel:
94
96
95
97
```js run
96
98
let a ="1";
@@ -99,74 +101,74 @@ let b = 0;
99
101
switch (+a) {
100
102
*!*
101
103
case b +1:
102
-
alert("this runs, because +a is 1, exactly equals b+1");
104
+
alert("Das funktioniert, weil +a gleich 1 ist, und damit genau gleich wie b+1");
103
105
break;
104
106
*/!*
105
107
106
108
default:
107
-
alert("this doesn't run");
109
+
alert("Wird nicht durchlaufen");
108
110
}
109
111
```
110
-
Here `+a`gives `1`, that's compared with `b + 1`in `case`, and the corresponding code is executed.
112
+
Hier ergibt `+a`den Wert `1`, welcher im `case` mit `b + 1`verglichen wird, worauf der entsprechende Code ausgeführt wird.
111
113
````
112
114
113
-
## Grouping of "case"
115
+
## Gruppieren von "case"
114
116
115
-
Several variants of `case` which share the same code can be grouped.
117
+
Mehrere Varianten von `case`, die den gleichen Code teilen, können gruppiert werden.
116
118
117
-
For example, if we want the same code to run for `case 3` and `case 5`:
119
+
Wenn wir zum Beispiel denselben Code für `case 3` und `case 5` ausführen wollen:
118
120
119
121
```js run no-beautify
120
122
let a = 3;
121
123
122
124
switch (a) {
123
125
case 4:
124
-
alert('Right!');
126
+
alert('Richtig!');
125
127
break;
126
128
127
129
*!*
128
-
case 3: // (*) grouped two cases
130
+
case 3: // (*) zwei Fälle gruppiert
129
131
case 5:
130
-
alert('Wrong!');
131
-
alert("Why don't you take a math class?");
132
+
alert('Falsch!');
133
+
alert("Warum besuchst du nicht einen Mathekurs?");
132
134
break;
133
135
*/!*
134
136
135
137
default:
136
-
alert('The result is strange. Really.');
138
+
alert('Das Resultat ist komisch. Wirklich.');
137
139
}
138
140
```
139
141
140
-
Now both `3` and `5` show the same message.
142
+
Nun zeigen `3` und `5` die selbe Nachricht.
141
143
142
-
The ability to "group" cases is a side effect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
144
+
Die Fähigkeit, Fälle "gruppieren" zu können, ist ein Nebeneffekt davon, wie `switch/case` ohne `break` funktioniert. Hier beginnt die Ausführung von `case 3` in der Zeile `(*)` und durchläuft `case 5`, weil es kein `break` gibt.
143
145
144
-
## Type matters
146
+
## Der Typ spielt eine Rolle
145
147
146
-
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
148
+
Wichtig ist, dass die Gleichheitsprüfung immer streng ist. Die Werte müssen vom gleichen Typ sein, damit sie übereinstimmen.
147
149
148
-
For example, let's consider the code:
150
+
Betrachten wir zum Beispiel folgenden Code:
149
151
150
152
```js run
151
-
let arg = prompt("Enter a value?");
153
+
let arg = prompt("Wert eingeben?");
152
154
switch (arg) {
153
155
case '0':
154
156
case '1':
155
-
alert( 'One or zero' );
157
+
alert( 'Eins oder null' );
156
158
break;
157
159
158
160
case '2':
159
161
alert( 'Two' );
160
162
break;
161
163
162
164
case 3:
163
-
alert( 'Never executes!' );
165
+
alert( 'Wird niemals ausgeführt!' );
164
166
break;
165
167
default:
166
-
alert( 'An unknown value' );
168
+
alert( 'Ein unbekannter Wert' );
167
169
}
168
170
```
169
171
170
-
1. For `0`, `1`, the first `alert` runs.
171
-
2. For `2` the second `alert` runs.
172
-
3. But for `3`, the result of the `prompt` is a string `"3"`, which is not strictly equal `===` to the number `3`. So we've got a dead code in `case 3`! The `default` variant will execute.
172
+
1. Für `0`, `1`, wird der erste `alert` ausgeführt.
173
+
2. Für `2` wird der zweite `alert` ausgeführt.
174
+
3. Aber für `3`, ist das Resultat des `prompt` ein String `"3"`, welcher nicht streng gleich `===` der Zahl `3` ist. Also haben wir ungenutzten Code in `case 3`! Die `default` Variante wird ausgeführt.
0 commit comments