Skip to content

Commit 610afe5

Browse files
authored
Translated article.md
1 parent f033939 commit 610afe5

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed
Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# The "switch" statement
1+
# Teiginys "switch"
22

3-
A `switch` statement can replace multiple `if` checks.
3+
Teiginys `switch` gali pakeisti daugybinius `if` patikrinimus.
44

5-
It gives a more descriptive way to compare a value with multiple variants.
5+
Jis suteikia lengviau apibūdinamą kelią palyginti vertes su įvairiais variantais.
66

7-
## The syntax
7+
## Sintaksė
88

9-
The `switch` has one or more `case` blocks and an optional default.
9+
Teiginys `switch` turi vieną ir daugiau `case` (bylos) blokų ir numatytąjį pasirinkimą.
1010

11-
It looks like this:
11+
Tai atrodo taip:
1212

1313
```js no-beautify
1414
switch(x) {
15-
case 'value1': // if (x === 'value1')
15+
case 'vertė1': // if (x === 'vertė1')
1616
...
1717
[break]
1818

19-
case 'value2': // if (x === 'value2')
19+
case 'vertė2': // if (x === 'vertė2')
2020
...
2121
[break]
2222

@@ -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+
- Vertė `x` yra patikrinama griežta lygybe su verte iš pirmos bylos `case` (tai yra `vertė1`) tada su antra (`vertė2`) ir taip toliau.
30+
- Jeigu lygybė randama, `switch` pradeda vykdyti kodą nuo atitinkančios bylos `case`, iki artimiausio `break` (arba kol pasibaigs `switch`).
31+
- Jeigu nei viena byla nesurado atitikimo, tokiu atveju yra įvykdomas `default` kodas (jeigu toks egzistuoja).
3232

33-
## An example
33+
## Pavyzdys
3434

35-
An example of `switch` (the executed code is highlighted):
35+
Pavyzdys `switch` (įvykdytas kodas yra paryškintas):
3636

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

4040
switch (a) {
4141
case 3:
42-
alert( 'Too small' );
42+
alert( 'Per mažas' );
4343
break;
4444
*!*
4545
case 4:
46-
alert( 'Exactly!' );
46+
alert( 'Kaip tik!' );
4747
break;
4848
*/!*
4949
case 5:
50-
alert( 'Too large' );
50+
alert( 'Per didelis' );
5151
break;
5252
default:
53-
alert( "I don't know such values" );
53+
alert( "Tokios vertės nežinau" );
5454
}
5555
```
5656

57-
Here the `switch` starts to compare `a` from the first `case` variant that is `3`. The match fails.
57+
Šiuo atveju `switch` pradeda lyginti `a` iš pirmos bylos `case` variantą, kuris yra `3`. Atitikmuo nėra randamas.
5858

59-
Then `4`. That's a match, so the execution starts from `case 4` until the nearest `break`.
59+
Tada `4`. Atitikmuo randamas, tad pradedamas vykdymas nuo `case 4` iki artimiausio `break`.
6060

61-
**If there is no `break` then the execution continues with the next `case` without any checks.**
61+
**Jeigu nėra `break` tokiu atveju vykdymas tęsiasi su sekančia `case` be jokių patikrinimų.**
6262

63-
An example without `break`:
63+
Pavyzdys be `break`:
6464

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

6868
switch (a) {
6969
case 3:
70-
alert( 'Too small' );
70+
alert( 'Per mažas' );
7171
*!*
7272
case 4:
73-
alert( 'Exactly!' );
73+
alert( 'Kaip tik!' );
7474
case 5:
75-
alert( 'Too big' );
75+
alert( 'Per didelis' );
7676
default:
77-
alert( "I don't know such values" );
77+
alert( "Tokios vertės nežinau" );
7878
*/!*
7979
}
8080
```
8181

82-
In the example above we'll see sequential execution of three `alert`s:
82+
Pavyzdyje aukščiau iš eilės matome kaip įvykdomi trys `alert`:
8383

8484
```js
85-
alert( 'Exactly!' );
86-
alert( 'Too big' );
87-
alert( "I don't know such values" );
85+
alert( 'Kaip tik!' );
86+
alert( 'Per didelis' );
87+
alert( "Tokios vertės nežinau" );
8888
```
8989

90-
````smart header="Any expression can be a `switch/case` argument"
91-
Both `switch` and `case` allow arbitrary expressions.
90+
````smart header="Bet kokia išraiška gali būti `switch/case` argumentu"
91+
Abu `switch` ir `case` leidžia sutartines išraiškas.
9292

93-
For example:
93+
Pavyzdžiui:
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("šitas preina, nes +a yra 1, tiksliai lygu b+1");
103103
break;
104104
*/!*
105105

106106
default:
107-
alert("this doesn't run");
107+
alert("tai nepasileidžia");
108108
}
109109
```
110-
Here `+a` gives `1`, that's compared with `b + 1` in `case`, and the corresponding code is executed.
110+
Čia `+a` atiduoda `1`, tai yra palyginama su `b + 1` byloje `case`, įvykdomas atitinkamas kodas.
111111
````
112112
113-
## Grouping of "case"
113+
## Grupavimas su "case"
114114
115-
Several variants of `case` which share the same code can be grouped.
115+
Keli `case` variantai, kurie dalinasi tuo pačiu kodu gali būti sugrupuoti.
116116
117-
For example, if we want the same code to run for `case 3` and `case 5`:
117+
Pavyzdžiui, jeigu mes norime, kad tas pats kodas pasileistų byloms `case 3` ir `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('Teisingai!');
125125
break;
126126
127127
*!*
128-
case 3: // (*) grouped two cases
128+
case 3: // (*) dvi bylos sugrupuotos
129129
case 5:
130-
alert('Wrong!');
131-
alert("Why don't you take a math class?");
130+
alert('Neteisingai!');
131+
alert('Būtų neblogai apsilankyti matematikos pamokoje.');
132132
break;
133133
*/!*
134134
135135
default:
136-
alert('The result is strange. Really.');
136+
alert('Rezultatas yra iš tikrųjų keistas.');
137137
}
138138
```
139139
140-
Now both `3` and `5` show the same message.
140+
Dabar abu `3` ir `5` parodo tą pačią žinutę.
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+
Gebėjimas "sugrupuoti" bylas yra šalutinis efektas to kaip `switch/case` veikia be `break`. Čia `case 3` vykdymas prasideda nuo eilės su `(*)` ir eina per bylą `case 5`, nes nėra `break`.
143143
144-
## Type matters
144+
## Tipas yra svarbu
145145
146-
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
146+
Pabrėžkime tai, kad lygybės patikrinimas yra visada griežtas. Vertės turi būti vienodo tipo, kad atitiktų viena kitą.
147147
148-
For example, let's consider the code:
148+
Pavyzdžiui, apsvarstykime tokį kodą:
149149
150150
```js run
151-
let arg = prompt("Enter a value?");
151+
let arg = prompt("Įveskite vertę?");
152152
switch (arg) {
153153
case '0':
154154
case '1':
155-
alert( 'One or zero' );
155+
alert( 'Vienas arba nulis' );
156156
break;
157157
158158
case '2':
159-
alert( 'Two' );
159+
alert( 'Du' );
160160
break;
161161
162162
case 3:
163-
alert( 'Never executes!' );
163+
alert( 'Niekada nėra įvykdomas!' );
164164
break;
165165
default:
166-
alert( 'An unknown value' );
166+
alert( 'Nežinoma vertė' );
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. Kai įvedamas `0`, `1`, paleidžiamas pirmas `alert`.
171+
2. Kai įvedamas `2` paleidžiamas antras `alert`.
172+
3. Bet `3` neįvykdomas, kadangi `prompt` rezultatas yra eilutė `"3"`, o tai nėra griežtai lygu `===` skaičiui `3`. Tad mes turime neveikiantį kodą byloje `case 3`! Įvykdomas `default` variantas.

0 commit comments

Comments
 (0)