Skip to content

Commit 3a972e8

Browse files
authored
Merge pull request #55 from matztom/master
The "switch" statement
2 parents 870b29e + 5e32abe commit 3a972e8

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed
Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# The "switch" statement
1+
# Die "switch" Anweisung
22

3-
A `switch` statement can replace multiple `if` checks.
3+
Eine `switch` Anweisung kann mehrere `if` Anweisungen ersetzen.
44

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.
66

7-
## The syntax
7+
## Die Syntax
88

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.
1010

11-
It looks like this:
11+
Das sieht wie folgt aus:
1212

1313
```js no-beautify
1414
switch(x) {
@@ -26,71 +26,73 @@ switch(x) {
2626
}
2727
```
2828

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).
3232

33-
## An example
33+
## Ein Beispiel
3434

35-
An example of `switch` (the executed code is highlighted):
35+
Ein Beispiel der `switch` Anweisung (der ausgeführte Code ist hervorgehoben):
3636

3737
```js run
3838
let a = 2 + 2;
3939

4040
switch (a) {
4141
case 3:
42-
alert( 'Too small' );
42+
alert( 'Zu klein' );
4343
break;
4444
*!*
4545
case 4:
46-
alert( 'Exactly!' );
46+
alert( 'Exakt!' );
4747
break;
4848
*/!*
4949
case 5:
50-
alert( 'Too big' );
50+
51+
alert( 'Zu gross' );
52+
5153
break;
5254
default:
53-
alert( "I don't know such values" );
55+
alert( "Ich kenne keinen solchen Werte" );
5456
}
5557
```
5658

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.
5860

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.
6062

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.**
6264

63-
An example without `break`:
65+
Ein Beispiel ohne `break`:
6466

6567
```js run
6668
let a = 2 + 2;
6769

6870
switch (a) {
6971
case 3:
70-
alert( 'Too small' );
72+
alert( 'Zu klein' );
7173
*!*
7274
case 4:
73-
alert( 'Exactly!' );
75+
alert( 'Exakt!' );
7476
case 5:
75-
alert( 'Too big' );
77+
alert( 'Zu gross' );
7678
default:
77-
alert( "I don't know such values" );
79+
alert( "Ich kenne keinen solchen Werte" );
7880
*/!*
7981
}
8082
```
8183

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:
8385

8486
```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" );
8890
```
8991

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.
9294

93-
For example:
95+
Zum Beispiel:
9496

9597
```js run
9698
let a = "1";
@@ -99,74 +101,74 @@ let b = 0;
99101
switch (+a) {
100102
*!*
101103
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");
103105
break;
104106
*/!*
105107

106108
default:
107-
alert("this doesn't run");
109+
alert("Wird nicht durchlaufen");
108110
}
109111
```
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.
111113
````
112114
113-
## Grouping of "case"
115+
## Gruppieren von "case"
114116
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.
116118
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:
118120
119121
```js run no-beautify
120122
let a = 3;
121123
122124
switch (a) {
123125
case 4:
124-
alert('Right!');
126+
alert('Richtig!');
125127
break;
126128
127129
*!*
128-
case 3: // (*) grouped two cases
130+
case 3: // (*) zwei Fälle gruppiert
129131
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?");
132134
break;
133135
*/!*
134136
135137
default:
136-
alert('The result is strange. Really.');
138+
alert('Das Resultat ist komisch. Wirklich.');
137139
}
138140
```
139141
140-
Now both `3` and `5` show the same message.
142+
Nun zeigen `3` und `5` die selbe Nachricht.
141143
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.
143145
144-
## Type matters
146+
## Der Typ spielt eine Rolle
145147
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.
147149
148-
For example, let's consider the code:
150+
Betrachten wir zum Beispiel folgenden Code:
149151
150152
```js run
151-
let arg = prompt("Enter a value?");
153+
let arg = prompt("Wert eingeben?");
152154
switch (arg) {
153155
case '0':
154156
case '1':
155-
alert( 'One or zero' );
157+
alert( 'Eins oder null' );
156158
break;
157159
158160
case '2':
159161
alert( 'Two' );
160162
break;
161163
162164
case 3:
163-
alert( 'Never executes!' );
165+
alert( 'Wird niemals ausgeführt!' );
164166
break;
165167
default:
166-
alert( 'An unknown value' );
168+
alert( 'Ein unbekannter Wert' );
167169
}
168170
```
169171
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

Comments
 (0)