Skip to content

The "switch" statement #13

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 7 commits into from
Dec 10, 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 `'==='`.
За да съвпаднат точно функционалностите на `switch`,`if` трябва да използва стриктно сравнение `'==='`.

For given strings though, a simple `'=='` works too.
За стрингове обаче, просто `'=='` също работи.

```js no-beautify
if(browser == 'Edge') {
alert("You've got the Edge!");
alert("Имаш Edge!");
} else if (browser == 'Chrome'
|| browser == 'Firefox'
|| browser == 'Safari'
|| browser == 'Opera') {
alert( 'Okay we support these browsers too' );
alert( 'Окей, поддържаме тези браузъри също' );
} else {
alert( 'We hope that this page looks ok!' );
alert( 'Надяваме се тази страница да изглежда ОК!' );
}
```

Please note: the construct `browser == 'Chrome' || browser == 'Firefox' …` is split into multiple lines for better readability.
Забележка: конструкцията `browser == 'Chrome' || browser == 'Firefox' …` е разделена на няколко реда за по-добра четливост.

But the `switch` construct is still cleaner and more descriptive.
Но `switch` конструкцията е все още по-чиста и описателна.
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"
# Пренапиши конструкцията от "switch" към "if"

Write the code using `if..else` which would correspond to the following `switch`:
Напиши кода използвайки `if..else` което ще кореспондира с `switch`:

```js
switch (browser) {
case 'Edge':
alert( "You've got the Edge!" );
alert( "Имаш Edge!" );
break;

case 'Chrome':
case 'Firefox':
case 'Safari':
case 'Opera':
alert( 'Okay we support these browsers too' );
alert( 'Окей, поддържаме тези браузъри също' );
break;

default:
alert( 'We hope that this page looks ok!' );
alert( 'Надяваме се тази страница да изглежда ОК!' );
}
```

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:
Първите две проверки се превръщат в два `case`. Третата проверка е разделена на два случая:

```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.
Забележка: `break` на дъното е ненужен, но ние го добавихме, за да направим кода пригоден и за вбъдеще.

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.
В бъдеще, има шанс да искаме да добавим още `case`, например `case 4`. И ако забравим да добавим `break` преди това, в края на `case 3`, ще се появи грешка. Това е презастраховане.
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"
# Пренапиши конструкцията от "if" към "switch"

Rewrite the code below using a single `switch` statement:
Пренапиши кода по-долу използвайки една единствена `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
# Конструкцията "switch"

A `switch` statement can replace multiple `if` checks.
`switch` конструкция може да замести мновество `if` проверки.

It gives a more descriptive way to compare a value with multiple variants.
Дава възможност за по-подробен начин да се сравни стойност с множество променливи.

## The syntax
## Синтаксис

The `switch` has one or more `case` blocks and an optional default.
Конструкцията `switch` има един или повече `case` блокове и един незадължителен default.

It looks like this:
Изглежда така:

```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).
- Стойността на `x` е проверена за стриктно равенство със стойността на първия `case` (това е, `value1`) след това на втория (`value2`) и така нататък.
- Ако е открито равенство, `switch` започва да изпълнява кода започвайки от съответния `case`, до най-близкия `break` (или до края на `switch` конструкцията).
- Ако нито един `case` не съвпада тогава `default` кода се изпълнява (ако съществува).

## An example
## Пример

An example of `switch` (the executed code is highlighted):
Пример за `switch` (изпълнения код е подчертан):

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

switch (a) {
case 3:
alert( 'Too small' );
alert( 'Твърде малко' );
break;
*!*
case 4:
alert( 'Exactly!' );
alert( 'Точно!' );
break;
*/!*
case 5:
alert( 'Too large' );
alert( 'Твърде много' );
break;
default:
alert( "I don't know such values" );
alert( "Не знам подобна стойност" );
}
```

Here the `switch` starts to compare `a` from the first `case` variant that is `3`. The match fails.
Тук `switch` започва да сравнява `a` от първия `case` променлива, която е `3`. Няма съвпадение.

Then `4`. That's a match, so the execution starts from `case 4` until the nearest `break`.
След това `4`. Което съвпада и затова изпълнението започва от `case 4` до следващия `break`.

**If there is no `break` then the execution continues with the next `case` without any checks.**
**Ако няма `break` тогава изпълнението продължава със следващия `case` без никакви проверки.**

An example without `break`:
Пример без `break`:

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

switch (a) {
case 3:
alert( 'Too small' );
alert( 'Твърде малко' );
*!*
case 4:
alert( 'Exactly!' );
alert( 'Точно!' );
case 5:
alert( 'Too big' );
alert( 'Твърде много' );
default:
alert( "I don't know such values" );
alert( "Не знам подобна стойност" );
*/!*
}
```

In the example above we'll see sequential execution of three `alert`s:
В примера по-горе ще видим последователно изпълнение на три `alert` метода:

```js
alert( 'Exactly!' );
alert( 'Too big' );
alert( "I don't know such values" );
alert( 'Точно!' );
alert( 'Твърде малко' );
alert( "Не знам подобна стойност" );
```

````smart header="Any expression can be a `switch/case` argument"
Both `switch` and `case` allow arbitrary expressions.
````smart header="Всеки израз може да бъде `switch/case` аргумент"
И `switch` и `case` позволяват своеволни изрази.

For example:
Пример:

```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("Това се изпълнява, защото +a е 1, което е точно равно на b+1");
break;
*/!*

default:
alert("this doesn't run");
alert("това не се изпълнява");
}
```
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
Тук `+a` дава `1`, това е сравнено с `b + 1` в `case`, и кореспондиращия код се изпълнява.
````

## Grouping of "case"
## Групиране на "case"

Several variants of `case` which share the same code can be grouped.
Няколко варианта на `case` които споделят същия код могат да се групират.

For example, if we want the same code to run for `case 3` and `case 5`:
Като например, ако искаме същия код да се изпълни за `case 3` и `case 5`:

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

switch (a) {
case 4:
alert('Right!');
alert('Точно!');
break;

*!*
case 3: // (*) grouped two cases
case 3: // (*) групирани 2 случая
case 5:
alert('Wrong!');
alert("Why don't you take a math class?");
alert('Грешно!');
alert("Защо не си учил математика в училище?");
break;
*/!*

default:
alert('The result is strange. Really.');
alert('Резултатът е странен. Наистина.');
}
```

Now both `3` and `5` show the same message.
Сега `3` и `5` показват същото съобщение.

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`.
Възможността да "групираме" случаи е страничен ефект от това как `switch/case` работи без `break`. Тук изпълнението на `case 3` започва от ред `(*)` и продължава през `case 5`, защото няма `break`.

## Type matters
## Начина на писане има значение

Let's emphasize that the equality check is always strict. The values must be of the same type to match.
Нека подчертаем, че проверката за равенство е винаги стриктна. Стойностите трябва да са от един и същ тип.

For example, let's consider the code:
Като например, нека разгледаме кода:

```js run
let arg = prompt("Enter a value?");
let arg = prompt("Добави стойност?");
switch (arg) {
case '0':
case '1':
alert( 'One or zero' );
alert( 'Едно или нула' );
break;

case '2':
alert( 'Two' );
alert( 'Две' );
break;

case 3:
alert( 'Never executes!' );
alert( 'Никога не се изпълнява!' );
break;
default:
alert( 'An unknown value' );
alert( 'Незнайна стойност' );
}
```

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. За `0`, `1`, първия `alert` се изпълнява.
2. За `2` втория `alert` се изпълнява.
3. Но за `3`, резултата от `prompt` е стринг `"3"`, който не е стриктно равен `===` на числото `3`. Затова получаваме мъртъв код в `case 3`! `default` варианта ще се изпълни.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy