Skip to content

Commit d3bea6b

Browse files
authored
Translated
1 parent eba4025 commit d3bea6b

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed
Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# The "switch" statement
1+
# Instrucțiunea "switch"
22

3-
A `switch` statement can replace multiple `if` checks.
3+
O instrucțiune `switch` poate înlocui mai multe teste `if`.
44

5-
It gives a more descriptive way to compare a value with multiple variants.
5+
Reprezintă un mod mai descriptiv de a compara o valoare cu mai multe variante.
66

7-
## The syntax
7+
## Sintaxă
88

9-
The `switch` has one or more `case` blocks and an optional default.
9+
Instrucțiunea `switch` are unul sau mai multe blocuri `case` și un bloc opțional `default`.
1010

11-
It looks like this:
11+
Ea arată în felul următor:
1212

1313
```js no-beautify
1414
switch(x) {
@@ -26,71 +26,71 @@ 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+
- Valoarea variabilei `x` este comparată prin egalitate strictă cu valoarea din primul `case` (adică, `value1`), apoi cu a doua valoare (`value2`) și așa mai departe.
30+
- Dacă găsește egalitate într-unul din cazuri, `switch` începe să execute codul începând cu `case` corespunzător, până la cel mai apropiat `break` (sau până la sfârșitul instrucțiunii `switch`).
31+
- Dacă niciun caz nu satisface egalitatea, codul din blocul `default` este executat, dacă acest bloc există.
3232

33-
## An example
33+
## Un exemplu
3434

35-
An example of `switch` (the executed code is highlighted):
35+
Un exemplu de `switch` (codul executat este evidențiat):
3636

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

4040
switch (a) {
4141
case 3:
42-
alert( 'Too small' );
42+
alert( 'Prea mic' );
4343
break;
4444
*!*
4545
case 4:
46-
alert( 'Exactly!' );
46+
alert( 'Exact!' );
4747
break;
4848
*/!*
4949
case 5:
50-
alert( 'Too large' );
50+
alert( 'Prea mare' );
5151
break;
5252
default:
53-
alert( "I don't know such values" );
53+
alert( "Nu știu astfel de valori" );
5454
}
5555
```
5656

57-
Here the `switch` starts to compare `a` from the first `case` variant that is `3`. The match fails.
57+
Aici, `switch` începe să compare `a` cu valoarea din primul `case`, adică `3`. Egalitatea nu este îndeplinită.
5858

59-
Then `4`. That's a match, so the execution starts from `case 4` until the nearest `break`.
59+
Urmează `4`. Acesta se potrivește, deci execuția începe de la `case 4` până la cel mai apropiat `break`.
6060

61-
**If there is no `break` then the execution continues with the next `case` without any checks.**
61+
**Dacă instrucțiunea `break` lipsește, execuția continuă cu următorul bloc `case` fără a mai face vreo verificare.**
6262

63-
An example without `break`:
63+
Un exemplu fără `break`:
6464

6565
```js run
6666
let a = 2 + 2;
6767

6868
switch (a) {
6969
case 3:
70-
alert( 'Too small' );
70+
alert( 'Prea mic' );
7171
*!*
7272
case 4:
73-
alert( 'Exactly!' );
73+
alert( 'Exact!' );
7474
case 5:
75-
alert( 'Too big' );
75+
alert( 'Prea mare' );
7676
default:
77-
alert( "I don't know such values" );
77+
alert( "Nu știu astfel de valori" );
7878
*/!*
7979
}
8080
```
8181

82-
In the example above we'll see sequential execution of three `alert`s:
82+
În exemplul de mai sus vom vedea execuția în secvență a trei `alert`:
8383

8484
```js
85-
alert( 'Exactly!' );
86-
alert( 'Too big' );
87-
alert( "I don't know such values" );
85+
alert( 'Exact!' );
86+
alert( 'Prea mare' );
87+
alert( "Nu știu astfel de valori" );
8888
```
8989

90-
````smart header="Any expression can be a `switch/case` argument"
91-
Both `switch` and `case` allow arbitrary expressions.
90+
````smart header="Orice expresie poate fi un argument pentru `switch/case`"
91+
Atât `switch`, cât și `case` permit expresii arbitrare.
9292

93-
For example:
93+
De exemplu:
9494

9595
```js run
9696
let a = "1";
@@ -99,74 +99,74 @@ let b = 0;
9999
switch (+a) {
100100
*!*
101101
case b + 1:
102-
alert("this runs, because +a is 1, exactly equals b+1");
102+
alert("acesta merge, deoarece +a este 1, exact cât este și b+1");
103103
break;
104104
*/!*
105105

106106
default:
107-
alert("this doesn't run");
107+
alert("asta nu merge");
108108
}
109109
```
110-
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
110+
Aici, `+a` este evaluat la `1`, care este comparat cu `b + 1` în `case` și codul corespunzător este executat.
111111
````
112112
113-
## Grouping of "case"
113+
## Grupări de "case"
114114
115-
Several variants of `case` which share the same code can be grouped.
115+
Mai multe instrucțiuni `case` care partajează același cod pot fi grupate.
116116
117-
For example, if we want the same code to run for `case 3` and `case 5`:
117+
De exemplu, dacă vrem să executăm același cod pentru `case 3` și `case 5`:
118118
119119
```js run no-beautify
120120
let a = 2 + 2;
121121
122122
switch (a) {
123123
case 4:
124-
alert('Right!');
124+
alert('Corect!');
125125
break;
126126
127127
*!*
128-
case 3: // (*) grouped two cases
128+
case 3: // (*) douș cazuri grupate
129129
case 5:
130-
alert('Wrong!');
131-
alert("Why don't you take a math class?");
130+
alert('Greșit!');
131+
alert("De ce nu urmezi un curs de matematică?");
132132
break;
133133
*/!*
134134
135135
default:
136-
alert('The result is strange. Really.');
136+
alert('Acest rezultat e ciudat. Pe bune.');
137137
}
138138
```
139139
140-
Now both `3` and `5` show the same message.
140+
Acum atât `3` cât și `5` arată același mesaj.
141141
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`.
142+
Posibilitatea de a ȚgrupaȚ cazuri este un efect secundar al modului în care funcționează `switch/case` fără `break`. Execuția blocului `case 3` începe de la linia marcată `(*)` și continuă în `case 5`, pentru că nu există niciun `break`.
143143
144-
## Type matters
144+
## Tipul contează
145145
146-
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
146+
Să accentuăm faptul că egalitatea este întotdeauna strictă. Valorile trebuie să fie de același tip pentru a avea o potrivire.
147147
148-
For example, let's consider the code:
148+
De exemplu, să considerăm codul:
149149
150150
```js run
151-
let arg = prompt("Enter a value?");
151+
let arg = prompt("Introduceți o valoare?");
152152
switch (arg) {
153153
case '0':
154154
case '1':
155-
alert( 'One or zero' );
155+
alert( 'Unu sau zero' );
156156
break;
157157
158158
case '2':
159-
alert( 'Two' );
159+
alert( 'Doi' );
160160
break;
161161
162162
case 3:
163-
alert( 'Never executes!' );
163+
alert( 'Nu se execută niciodată!' );
164164
break;
165165
default:
166-
alert( 'An unknown value' );
166+
alert( 'Valoare necunoscută' );
167167
}
168168
```
169169
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.
170+
1. Pentru `0`, `1`, primul `alert` este executat.
171+
2. Pentru `2` se execută al doilea `alert`.
172+
3. Dar pentru `3`, rezultatul instrucțiunii `prompt` este șirul `"3"`, care nu este strict egal `===` cu numărul `3`, deci am obținut cod mort pentru `case 3`! Blocul `default` va fi executat în acest caz.

0 commit comments

Comments
 (0)