Skip to content

The "switch" statement #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
To precisely match the functionality of `switch`, the `if` must use a strict comparison `'==='`.
Pentru a obține exact funcționalitatea instrucțiunii `switch`, `if` trebuie să folosească operatorul de egalitate strictă `'==='`.

For given strings though, a simple `'=='` works too.
Pentru șirurile date, merge și egalitatea simplă `'=='`.

```js no-beautify
if(browser == 'Edge') {
alert("You've got the Edge!");
if (browser == 'Edge') {
alert("Folosiți Edge!");
} else if (browser == 'Chrome'
|| browser == 'Firefox'
|| browser == 'Safari'
|| browser == 'Opera') {
alert( 'Okay we support these browsers too' );
alert( 'OK, suportăm și aceste browsere' );
} else {
alert( 'We hope that this page looks ok!' );
alert( 'Sperăm că această pagină arată bine!' );
}
```

Please note: the construct `browser == 'Chrome' || browser == 'Firefox' …` is split into multiple lines for better readability.
Observație: expresia `browser == 'Chrome' || browser == 'Firefox' …` este împărțită pe mai multe linii pentru a îmbunătăți lizibilitatea.

But the `switch` construct is still cleaner and more descriptive.
Totuși, construcția care folosește `switch` este mai curată și mai descriptivă.
10 changes: 5 additions & 5 deletions 1-js/02-first-steps/13-switch/1-rewrite-switch-if-else/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ importance: 5

---

# Rewrite the "switch" into an "if"
# Rescrieți instrucțiunea "switch" folosind "if"

Write the code using `if..else` which would correspond to the following `switch`:
Folosind `if..else`, scrieți codul care ar corespunde următorului `switch`:

```js
switch (browser) {
case 'Edge':
alert( "You've got the Edge!" );
alert( "Folosiți Edge!" );
break;

case 'Chrome':
case 'Firefox':
case 'Safari':
case 'Opera':
alert( 'Okay we support these browsers too' );
alert( 'OK, suportăm și aceste browsere' );
break;

default:
alert( 'We hope that this page looks ok!' );
alert( 'Sperăm că această pagină arată bine!' );
}
```

6 changes: 3 additions & 3 deletions 1-js/02-first-steps/13-switch/2-rewrite-if-switch/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The first two checks turn into two `case`. The third check is split into two cases:
Primele două verificări sunt transformate în două blocuri `case`. Al treilea test este împărțit în două cazuri:

```js run
let a = +prompt('a?', '');
Expand All @@ -21,6 +21,6 @@ switch (a) {
}
```

Please note: the `break` at the bottom is not required. But we put it to make the code future-proof.
Observație: instrucțiunea `break` de la sfârșit nu este necesară, dar putem să o adăugăm pentru a pregăti codul pentru viitor.

In the future, there is a chance that we'd want to add one more `case`, for example `case 4`. And if we forget to add a break before it, at the end of `case 3`, there will be an error. So that's a kind of self-insurance.
În viitor este posibil să vrem să mai adăugăm încă un `case`, de exemplu `case 4`. Dacă uităm să adăugăm un `break` înaintea lui, la sfârșitul blocului `case 3`, vom obține o eroare. Deci avem un fel de auto-asigurare.
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/13-switch/2-rewrite-if-switch/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 4

---

# Rewrite "if" into "switch"
# Rescrieți "if" folosind "switch"

Rewrite the code below using a single `switch` statement:
Rescrieți codul următor folosind o singură instrucțiune `switch`:

```js run
let a = +prompt('a?', '');
Expand Down
108 changes: 54 additions & 54 deletions 1-js/02-first-steps/13-switch/article.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# The "switch" statement
# Instrucțiunea "switch"

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

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

## The syntax
## Sintaxă

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

It looks like this:
Ea arată în felul următor:

```js no-beautify
switch(x) {
Expand All @@ -26,71 +26,71 @@ switch(x) {
}
```

- 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.
- 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`).
- If no case is matched then the `default` code is executed (if it exists).
- 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.
- 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`).
- Dacă niciun caz nu satisface egalitatea, codul din blocul `default` este executat, dacă acest bloc există.

## An example
## Un exemplu

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

```js run
let a = 2 + 2;

switch (a) {
case 3:
alert( 'Too small' );
alert( 'Prea mic' );
break;
*!*
case 4:
alert( 'Exactly!' );
alert( 'Exact!' );
break;
*/!*
case 5:
alert( 'Too large' );
alert( 'Prea mare' );
break;
default:
alert( "I don't know such values" );
alert( "Nu știu astfel de valori" );
}
```

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

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

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

An example without `break`:
Un exemplu fără `break`:

```js run
let a = 2 + 2;

switch (a) {
case 3:
alert( 'Too small' );
alert( 'Prea mic' );
*!*
case 4:
alert( 'Exactly!' );
alert( 'Exact!' );
case 5:
alert( 'Too big' );
alert( 'Prea mare' );
default:
alert( "I don't know such values" );
alert( "Nu știu astfel de valori" );
*/!*
}
```

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

```js
alert( 'Exactly!' );
alert( 'Too big' );
alert( "I don't know such values" );
alert( 'Exact!' );
alert( 'Prea mare' );
alert( "Nu știu astfel de valori" );
```

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

For example:
De exemplu:

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

default:
alert("this doesn't run");
alert("asta nu merge");
}
```
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
Aici, `+a` este evaluat la `1`, care este comparat cu `b + 1` în `case` și codul corespunzător este executat.
````

## Grouping of "case"
## Grupări de "case"

Several variants of `case` which share the same code can be grouped.
Mai multe instrucțiuni `case` care partajează același cod pot fi grupate.

For example, if we want the same code to run for `case 3` and `case 5`:
De exemplu, dacă vrem să executăm același cod pentru `case 3` și `case 5`:

```js run no-beautify
let a = 2 + 2;

switch (a) {
case 4:
alert('Right!');
alert('Corect!');
break;

*!*
case 3: // (*) grouped two cases
case 3: // (*) două cazuri grupate
case 5:
alert('Wrong!');
alert("Why don't you take a math class?");
alert('Greșit!');
alert("De ce nu urmezi un curs de matematică?");
break;
*/!*

default:
alert('The result is strange. Really.');
alert('Acest rezultat e ciudat. Pe bune.');
}
```

Now both `3` and `5` show the same message.
Acum, atât `3` cât și `5` arată același mesaj.

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

## Type matters
## Tipul contează

Let's emphasize that the equality check is always strict. The values must be of the same type to match.
Să accentuăm faptul că egalitatea este întotdeauna strictă. Valorile trebuie să fie de același tip pentru a avea o potrivire.

For example, let's consider the code:
De exemplu, să considerăm codul:

```js run
let arg = prompt("Enter a value?");
let arg = prompt("Introduceți o valoare?");
switch (arg) {
case '0':
case '1':
alert( 'One or zero' );
alert( 'Unu sau zero' );
break;

case '2':
alert( 'Two' );
alert( 'Doi' );
break;

case 3:
alert( 'Never executes!' );
alert( 'Nu se execută niciodată!' );
break;
default:
alert( 'An unknown value' );
alert( 'Valoare necunoscută' );
}
```

1. For `0`, `1`, the first `alert` runs.
2. For `2` the second `alert` runs.
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.
1. Pentru `0`, `1`, primul `alert` este executat.
2. Pentru `2` se execută al doilea `alert`.
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.