You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/12-while-for/article.md
+53-53Lines changed: 53 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,54 +1,54 @@
1
-
# Loops: while and for
1
+
# Ciklai: while ir for
2
2
3
-
We often need to repeat actions.
3
+
Mes dažnai turime pakartoti veiksmus.
4
4
5
-
For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10.
5
+
Pavyzdžiui, įvairių dalykų išvedimas vienas paskui kitą iš sąrašo, arba paprasčiausiai to paties kodo paliedimas kiekvienam numeriui nuo 1 iki 10.
6
6
7
-
*Loops* are a way to repeat the same code multiple times.
7
+
*Cilkai* (ang. *Loops*) yra būdas pakartoti daug kartų tą patį kodą.
8
8
9
-
## The "while" loop
9
+
## Ciklas "while"
10
10
11
-
The`while`loop has the following syntax:
11
+
Ciklas`while`turi sekančią sintaksę:
12
12
13
13
```js
14
14
while (condition) {
15
-
//code
16
-
//so-called "loop body"
15
+
//kodas
16
+
//taip vadinamas "ciklo korpusas" (ang. "loop body")
17
17
}
18
18
```
19
19
20
-
While the `condition` is truthy, the `code` from the loop body is executed.
20
+
Kol `sąlyga` yra truthy, `kodas` iš ciklo rinkinio yra įvykdomas.
21
21
22
-
For instance, the loop below outputs `i`while`i < 3`:
22
+
Pavyzdžiui, ciklas žemiau atiduoda `i`kol`i < 3`:
23
23
24
24
```js run
25
25
let i =0;
26
-
while (i <3) { //shows 0, then 1, then 2
26
+
while (i <3) { //parodo 0, tada 1, tada 2
27
27
alert( i );
28
28
i++;
29
29
}
30
30
```
31
31
32
-
A single execution of the loop body is called *an iteration*. The loop in the example above makes three iterations.
32
+
Vienas ciklo rinkinio įvykdymas vadinamas *iteracija* (ang. *an iteration*). Ciklas pavyzdyje aukščiau padaro tris iteracijas.
33
33
34
-
If `i++` was missing from the example above, the loop would repeat (in theory) forever. In practice, the browser provides ways to stop such loops, and in server-side JavaScript, we can kill the process.
34
+
Jeigu kodo pavyzdyje viršuje nebūtų `i++`, ciklas (teoriškai) kartotųsi amžinai. Praktiškai, naršyklė suteikia būdų sustabdyti tokį ciklą ir procesas gali būti užbaigtas serverio pusės JavaScript.
35
35
36
-
Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by `while`.
36
+
Bet kokia išraiška arba kintamasis gali būti ciklo sąlyga, ne tik palyginimas: sąlyga yra įvertinama ir `while` paverčiama į loginį sprendimą.
37
37
38
-
For instance, a shorter way to write `while (i != 0)`is`while (i)`:
38
+
Pavyzdžiui, trumpesnis būdas parašyti `while (i != 0)`yra`while (i)`:
39
39
40
40
```js run
41
41
let i =3;
42
42
*!*
43
-
while (i) { //when i becomes 0, the condition becomes falsy, and the loop stops
43
+
while (i) { //kai i pavirsta 0, sąlyga tampa falsy ir ciklas sustoja
44
44
*/!*
45
45
alert( i );
46
46
i--;
47
47
}
48
48
```
49
49
50
-
````smart header="Curly braces are not required for a single-line body"
51
-
If the loop body has a single statement, we can omit the curly braces `{…}`:
50
+
````smart header="Riestiniai skliaustai nėra reikalingi vienos eilės korpusui"
51
+
Jeigu ciklo kopusas turi tik vieną teiginį, galime nenaudoti riestinių skliaustų `{…}`:
52
52
53
53
```js run
54
54
let i = 3;
@@ -58,19 +58,19 @@ while (i) alert(i--);
58
58
```
59
59
````
60
60
61
-
## The "do..while" loop
61
+
## Ciklas "do..while"
62
62
63
-
The condition check can be moved *below* the loop body using the `do..while` syntax:
63
+
Sąlygos patikrinimas gali būtų perkeltas *žemiau* ciklo korpuso naudojant sintaksę `do..while`:
64
64
65
65
```js
66
66
do {
67
-
//loop body
68
-
} while (condition);
67
+
//ciklo korpusas
68
+
} while (sąlyga);
69
69
```
70
70
71
-
The loop will first execute the body, then check the condition, and, while it's truthy, execute it again and again.
71
+
Ciklas visų pirma įvykdys korpusą, tada patikrins sąlygą ir kol ji yra truthy, įvykdys vėl ir vėl.
72
72
73
-
For example:
73
+
Pavyzdžiui:
74
74
75
75
```js run
76
76
let i =0;
@@ -80,69 +80,69 @@ do {
80
80
} while (i <3);
81
81
```
82
82
83
-
This form of syntax should only be used when you want the body of the loop to execute **at least once**regardless of the condition being truthy. Usually, the other form is preferred: `while(…) {…}`.
83
+
Tokia sintaksė turėtų būti naudojama kai norite, kad ciklo korpusas būtų įvykdytas **bent vieną kartą**nepaisant to ar jo sąlyga yra truthy. Dažniausiai vis dėlto naudojama kita forma: `while(…) {…}`.
84
84
85
-
## The "for" loop
85
+
## Ciklas "for"
86
86
87
-
The`for`loop is more complex, but it's also the most commonly used loop.
87
+
Ciklas`for`yra kiek sudėtingesnis, bet jis taip pat yra dažniausiai naudojamas ciklas.
88
88
89
-
It looks like this:
89
+
Jis atrodo taip:
90
90
91
91
```js
92
-
for (begin; condition; step) {
93
-
// ... loop body ...
92
+
for (begin; condition; step) {// pradžia; sąlyga; žingsnis
93
+
// ... ciklo korpusas ...
94
94
}
95
95
```
96
96
97
-
Let's learn the meaning of these parts by example. The loop below runs `alert(i)`for`i`from`0`up to (but not including) `3`:
97
+
Išmokime šių dalių reikšmę su pavyzdžiais. Ciklas žemiau paleidžia `alert(i)`dėl`i`nuo`0`iki (bet neįskaitant) `3`:
98
98
99
99
```js run
100
-
for (let i =0; i <3; i++) { //shows 0, then 1, then 2
100
+
for (let i =0; i <3; i++) { //parodo 0, tada 1, tada 2
|begin|`i = 0`|Executes once upon entering the loop. |
110
-
|condition|`i < 3`|Checked before every loop iteration. If false, the loop stops. |
111
-
|body|`alert(i)`|Runs again and again while the condition is truthy. |
112
-
|step|`i++`| Executes after the body on each iteration.|
109
+
|pradžia|`i = 0`|Įvykdomas vieną kartą pradedant ciklą|
110
+
|salyga|`i < 3`|Patikrinama prieš kiekvieną ciklo iteraciją. Jeigu netiesa, ciklas sustoja|
111
+
|korpusas|`alert(i)`|Įvykdomas vėl ir vėl kol sąlyga yra truthy. |
112
+
|žingsnis |`i++`| Įvykdomas po korpuso per kiekvieną iteraciją. |
113
113
114
-
The general loop algorithm works like this:
114
+
Įprastinio ciklo algoritmasveikia taip:
115
115
116
116
```
117
-
Run begin
118
-
→ (if condition → run body and run step)
119
-
→ (if condition → run body and run step)
120
-
→ (if condition → run body and run step)
117
+
Pradedamas vykdymas
118
+
→ (jeigu sąlyga → paleisti korpusą ir paleisti žingsnį)
119
+
→ (jeigu sąlyga → paleisti korpusą ir paleisti žingsnį)
120
+
→ (jeigu sąlyga → paleisti korpusą ir paleisti žingsnį)
121
121
→ ...
122
122
```
123
123
124
-
That is, `begin`executes once, and then it iterates: after each `condition` test, `body` and `step` are executed.
124
+
Tai yra, `begin`įvykdomas vieną kartą ir tada jis kartojasi (ang. iterates): po kiekvieno `sąlygos` testo, įvykdomi `korpusas` ir `žingsnis`.
125
125
126
-
If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper.
126
+
Jeigu ciklai jums naujiena, padėtų juos geriau suprasti, jeigu sugrįžtumėte prie pavyzdžio ir atkurtumėte kaip jis veikia žingsnis po žingsnio ant popieriaus lapo.
127
127
128
-
Here's exactly what happens in our case:
128
+
Štai kas konkrečiai vyksta mūsų atveju:
129
129
130
130
```js
131
131
// for (let i = 0; i < 3; i++) alert(i)
132
132
133
-
//run begin
133
+
//pradedamas vykdymas
134
134
let i =0
135
-
//if condition → run body and run step
135
+
//jeigu sąlyga → paleisti korpusą ir paleisti žingsnį
136
136
if (i <3) { alert(i); i++ }
137
-
//if condition → run body and run step
137
+
//jeigu sąlyga → paleisti korpusą ir paleisti žingsnį
138
138
if (i <3) { alert(i); i++ }
139
-
//if condition → run body and run step
139
+
//jeigu sąlyga → paleisti korpusą ir paleisti žingsnį
140
140
if (i <3) { alert(i); i++ }
141
-
// ...finish, because now i == 3
141
+
// ...pabaiga, nes dabar i == 3
142
142
```
143
143
144
-
````smart header="Inline variable declaration"
145
-
Here, the "counter" variable `i` is declared right in the loop. This is called an "inline" variable declaration. Such variables are visible only inside the loop.
Čia "skaičiuojantis" (ang. "counter") kintamasis `i` yra deklaruotas tiesis cikle. is declared right in the loop. This is called an "inline" variable declaration. Such variables are visible only inside the loop.
0 commit comments