Skip to content

Commit 5a853aa

Browse files
authored
Merge pull request #122 from galenteryan/condition
Conditional branching: if, '?'
2 parents 3ecf108 + 70e304f commit 5a853aa

File tree

1 file changed

+87
-87
lines changed

1 file changed

+87
-87
lines changed
Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,117 @@
1-
# Conditional branching: if, '?'
1+
# Պայմանական ճյուղավորում. if, ?
22

3-
Sometimes, we need to perform different actions based on different conditions.
3+
Երբեմն, մենք կարիք ենք ունենում կատարել տարբեր գործողություններ՝ կախված տարբեր պայմաններից։
44

5-
To do that, we can use the `if` statement and the conditional operator `?`, that's also called a "question mark" operator.
5+
Դա անելու համար մենք կարող ենք օգտագործել `if` դրույթը և `?` պայմանական օպերատորը: Վերջինս նաև կոչվում է «հարցական նշան»-ի օպերատոր։
66

7-
## The "if" statement
7+
## «if» դրույթը
88

9-
The `if(...)` statement evaluates a condition in parentheses and, if the result is `true`, executes a block of code.
9+
`if(...)` դրույթը արժևորում է փակագծերում նշված պայմանը և, եթե արդյունքը `true` է, ապա կատարում է կոդի բլոկը:
1010

11-
For example:
11+
Օրինակ.
1212

1313
```js run
14-
let year = prompt('In which year was ECMAScript-2015 specification published?', '');
14+
let year = prompt('Ո՞ր թվականին է թողարկվել ECMAScript-2015 ստանդարտը:', '');
1515

1616
*!*
17-
if (year == 2015) alert( 'You are right!' );
17+
if (year == 2015) alert( 'Դուք ճիշտ եք:' );
1818
*/!*
1919
```
2020

21-
In the example above, the condition is a simple equality check (`year == 2015`), but it can be much more complex.
21+
Վերոնշյալ օրինակում պայմանը ստուգում է պարզ հավասարություն (`year == 2015`), բայց այն կարող է շատ ավելի բարդ լինել:
2222

23-
If we want to execute more than one statement, we have to wrap our code block inside curly braces:
23+
Եթե մենք ցանկանում ենք կատարել մեկից ավել դրույթներ, ապա մեր կոդի հատվածը պետք է վերցնենք ձևավոր փակագծերի մեջ:
2424

2525
```js
2626
if (year == 2015) {
27-
alert( "That's correct!" );
28-
alert( "You're so smart!" );
27+
alert( "Ճիշտ է:" );
28+
alert( "Դուք այնքան խելացի եք․․․" );
2929
}
3030
```
3131

32-
We recommend wrapping your code block with curly braces `{}` every time you use an `if` statement, even if there is only one statement to execute. Doing so improves readability.
32+
Մենք խորհուրդ ենք տալիս պատել կոդի հատվածը `{}` ձևավոր փակագծերով ամեն անգամ, երբ օգտագործում եք `if` դրույթը, նույնիսկ եթե կատարման համար առկա է միայն մեկ դրույթ։ Այդպես բարելավվում է ընթեռնելիությունը:
3333

34-
## Boolean conversion
34+
## Բուլյան փոխակերպում
3535

36-
The `if (…)` statement evaluates the expression in its parentheses and converts the result to a boolean.
36+
`if (…)` դրույթը գնահատում է իր փակագծերում առկա արտահայտությունը և արդյունքը դարձնում բուլյան:
3737

38-
Let's recall the conversion rules from the chapter <info:type-conversions>:
38+
Եկեք հիշենք փոխակերպման կանոնները <info:type-conversions> գլխից.
3939

40-
- A number `0`, an empty string `""`, `null`, `undefined`, and `NaN` all become `false`. Because of that they are called "falsy" values.
41-
- Other values become `true`, so they are called "truthy".
40+
- `0` թիվը, դատարկ տողը `""`, `null`, `undefined` և `NaN`՝ սրանք բոլորը դառնում են `false`: Այդ պատճառով կոչվում են «կեղծ» արժեքներ։
41+
- Մյուս արժեքները դառնում են `true`, ուստի դրանք կոչվում են «ճշմարիտ»:
4242

43-
So, the code under this condition would never execute:
43+
Այսպիսով, այս պայմանի ներսում կոդը երբեք չի գործարկվի.
4444

4545
```js
46-
if (0) { // 0 is falsy
46+
if (0) { // 0-ն կեղծ է
4747
...
4848
}
4949
```
5050

51-
...and inside this condition -- it always will:
51+
...և այս պայմանի ներսում միշտ կգործարկվի.
5252

5353
```js
54-
if (1) { // 1 is truthy
54+
if (1) { // 1-ը ճշմարիտ է
5555
...
5656
}
5757
```
5858

59-
We can also pass a pre-evaluated boolean value to `if`, like this:
59+
Մենք կարող ենք նաև նախապես գնահատված բուլյան արժեք փոխանցել `if`-ին, այսպես.
6060

6161
```js
62-
let cond = (year == 2015); // equality evaluates to true or false
62+
let cond = (year == 2015); // հավասարությունը գնահատվում է ճշմարիտ կամ կեղծ
6363

6464
if (cond) {
6565
...
6666
}
6767
```
6868

69-
## The "else" clause
69+
## «else» դրույթը
7070

71-
The `if` statement may contain an optional "else" block. It executes when the condition is falsy.
71+
`if` դրույթը կարող է պարունակել կամընտրական, ոչ պարտադիր «else» բլոկ: Այն գործարկվում է, երբ պայմանը կեղծ է:
7272

73-
For example:
73+
Օրինակ.
7474
```js run
75-
let year = prompt('In which year was the ECMAScript-2015 specification published?', '');
75+
let year = prompt('Ո՞ր թվականին է հրապարակվել ECMAScript-2015 դասակարգումը:', '');
7676

7777
if (year == 2015) {
78-
alert( 'You guessed it right!' );
78+
alert( 'Դուք ճիշտ գուշակեցիք:' );
7979
} else {
80-
alert( 'How can you be so wrong?' ); // any value except 2015
80+
alert( 'Ինչպե՞ս կարող եք այդքան սխալվել:' ); // ցանկացած արժեք, բացի 2015-ից
8181
}
8282
```
8383

84-
## Several conditions: "else if"
84+
## Մի քանի պայմաններ. «else if»
8585

86-
Sometimes, we'd like to test several variants of a condition. The `else if` clause lets us do that.
86+
Երբեմն մենք ցանկանում ենք փորձարկել պայմանի մի քանի տարբերակներ: `else if` դրույթը մեզ թույլ է տալիս անել դա:
8787

88-
For example:
88+
Օրինակ.
8989

9090
```js run
91-
let year = prompt('In which year was the ECMAScript-2015 specification published?', '');
91+
let year = prompt('Ո՞ր թվականին է հրապարակվել ECMAScript-2015 դասակարգումը:', '');
9292

9393
if (year < 2015) {
94-
alert( 'Too early...' );
94+
alert( 'Շատ վաղ է...' );
9595
} else if (year > 2015) {
96-
alert( 'Too late' );
96+
alert( 'Շատ ուշ է' );
9797
} else {
98-
alert( 'Exactly!' );
98+
alert( 'Ճիշտ այդպես:' );
9999
}
100100
```
101101

102-
In the code above, JavaScript first checks `year < 2015`. If that is falsy, it goes to the next condition `year > 2015`. If that is also falsy, it shows the last `alert`.
102+
Վերևի կոդում JavaScript-ը նախ ստուգում է `year < 2015`-ը: Եթե դա կեղծ է, անցում է կատարում հաջորդ պայմանին՝ `year > 2015`: Եթե դա նույնպես կեղծ է, ցույց է տալիս վերջին `alert`-ը:
103103

104-
There can be more `else if` blocks. The final `else` is optional.
104+
Կարող են լինել ավելի շատ `else if` բլոկներ: Վերջին `else`-ը կամընտիր է:
105105

106-
## Conditional operator '?'
106+
## Պայմանական օպերատոր «?»
107107

108-
Sometimes, we need to assign a variable depending on a condition.
108+
Երբեմն մեզ պետք է լինում փոփոխական նշանակել՝ կախված պայմանից:
109109

110-
For instance:
110+
Օրինակ.
111111

112112
```js run no-beautify
113113
let accessAllowed;
114-
let age = prompt('How old are you?', '');
114+
let age = prompt('Քանի՞ տարեկան եք:', '');
115115

116116
*!*
117117
if (age > 18) {
@@ -124,116 +124,116 @@ if (age > 18) {
124124
alert(accessAllowed);
125125
```
126126

127-
The so-called "conditional" or "question mark" operator lets us do that in a shorter and simpler way.
127+
Այսպես կոչված «պայմանական» կամ «հարցական նշանի» օպերատորը թույլ է տալիս դա անել ավելի կարճ և ավելի պարզ ձևով:
128128

129-
The operator is represented by a question mark `?`. Sometimes it's called "ternary", because the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
129+
Օպերատորը ներկայացված է `?` հարցական նշանով: Երբեմն այն կոչվում է «եռակի», քանի որ օպերատորն ունի երեք օպերանդ։ Այն իրականում միակ օպերատորն է JavaScript-ում, որն այդքան օպերանդ ունի:
130130

131-
The syntax is:
131+
Շարահյուսությունը հետևյալն է.
132132
```js
133133
let result = condition ? value1 : value2;
134134
```
135135

136-
The `condition` is evaluated: if it's truthy then `value1` is returned, otherwise -- `value2`.
136+
Գնահատվում է `condition`-ը. եթե այն ճշմարիտ է, ապա `value1`-ն է վերադարձվում, հակառակ դեպքում՝ `value2`-ը:
137137

138-
For example:
138+
Օրինակ.
139139

140140
```js
141141
let accessAllowed = (age > 18) ? true : false;
142142
```
143143

144-
Technically, we can omit the parentheses around `age > 18`. The question mark operator has a low precedence, so it executes after the comparison `>`.
144+
Տեխնիկապես մենք կարող ենք բաց թողնել փակագծերը `age > 18`-ի շուրջ: Հարցական նշանի օպերատորն ունի ցածր գերակայություն, ուստի այն գործարկվում է `>` համեմատությունից հետո:
145145

146-
This example will do the same thing as the previous one:
146+
Այս օրինակը կանի նույն բանը, ինչ նախորդը.
147147

148148
```js
149-
// the comparison operator "age > 18" executes first anyway
150-
// (no need to wrap it into parentheses)
149+
// համեմատության «age > 18» օպերատորը ամեն դեպքում առաջինն է կատարվում
150+
// (կարիք չկա փակագծերի մեջ վերցնել այն)
151151
let accessAllowed = age > 18 ? true : false;
152152
```
153153

154-
But parentheses make the code more readable, so we recommend using them.
154+
Բայց փակագծերը կոդն ավելի ընթեռնելի են դարձնում, ուստի խորհուրդ ենք տալիս օգտագործել դրանք:
155155

156156
````smart
157-
In the example above, you can avoid using the question mark operator because the comparison itself returns `true/false`:
157+
Վերևի օրինակում դուք կարող եք խուսափել հարցական նշանի օպերատորի օգտագործումից, քանի որ համեմատությունն ինքնին վերադարձնում է `true/false`.
158158
159159
```js
160-
// the same
160+
// նույնը
161161
let accessAllowed = age > 18;
162162
```
163163
````
164164

165-
## Multiple '?'
165+
## Բազմաթիվ «?»
166166

167-
A sequence of question mark operators `?` can return a value that depends on more than one condition.
167+
Հարցական նշանի օպերատորների `?` հաջորդականությունը կարող է վերադարձնել արժեք, որը կախված է մեկից ավելի պայմաններից:
168168

169-
For instance:
169+
Օրինակ.
170170
```js run
171-
let age = prompt('age?', 18);
171+
let age = prompt('տարի՞քը', 18);
172172

173-
let message = (age < 3) ? 'Hi, baby!' :
174-
(age < 18) ? 'Hello!' :
175-
(age < 100) ? 'Greetings!' :
176-
'What an unusual age!';
173+
let message = (age < 3) ? 'Ողջույն փոքրիկ' :
174+
(age < 18) ? 'Ողջույն' :
175+
(age < 100) ? 'Ողջույններ' :
176+
'Ի՜նչ անսովոր տարիք է։';
177177

178178
alert( message );
179179
```
180180

181-
It may be difficult at first to grasp what's going on. But after a closer look, we can see that it's just an ordinary sequence of tests:
181+
Սկզբում կարող է դժվար լինել հասկանալը, թե ինչ է կատարվում: Բայց ավելի ուշադիր նայելուց հետո մենք կարող ենք տեսնել, որ դա ընդամենը թեստերի սովորական հաջորդականություն է.
182182

183-
1. The first question mark checks whether `age < 3`.
184-
2. If true -- it returns `'Hi, baby!'`. Otherwise, it continues to the expression after the colon '":"', checking `age < 18`.
185-
3. If that's true -- it returns `'Hello!'`. Otherwise, it continues to the expression after the next colon '":"', checking `age < 100`.
186-
4. If that's true -- it returns `'Greetings!'`. Otherwise, it continues to the expression after the last colon '":"', returning `'What an unusual age!'`.
183+
1. Առաջին հարցական նշանը ստուգում է, թե արդյո՞ք `age < 3`:
184+
2. Եթե ճիշտ է, այն վերադարձնում է `'Ողջույն փոքրիկ'`: Հակառակ դեպքում, այն շարունակում է `:` կրկնակետից հետո արտահայտությունը՝ ստուգելով `age < 18`:
185+
3. Եթե դա ճիշտ է, այն վերադարձնում է `'Ողջույն'`: Հակառակ դեպքում, այն շարունակում է հաջորդ `:` կրկնակետից հետո արտահայտությունը՝ ստուգելով `age < 100`:
186+
4. Եթե դա ճիշտ է, այն վերադարձնում է `'Ողջույններ!'`: Հակառակ դեպքում, այն շարունակում է վերջին `:` կրկնակետից հետո արտահայտությունը՝ վերադարձնելով `'Ի՜նչ անսովոր տարիք է։'`:
187187

188-
Here's how this looks using `if..else`:
188+
Ահա, թե ինչպես է սա երևում `if..else`-ի միջոցով.
189189

190190
```js
191191
if (age < 3) {
192-
message = 'Hi, baby!';
192+
message = 'Ողջույն փոքրիկ';
193193
} else if (age < 18) {
194-
message = 'Hello!';
194+
message = 'Ողջույն';
195195
} else if (age < 100) {
196-
message = 'Greetings!';
196+
message = 'Ողջույններ';
197197
} else {
198-
message = 'What an unusual age!';
198+
message = 'Ի՜նչ անսովոր տարիք է։';
199199
}
200200
```
201201

202-
## Non-traditional use of '?'
202+
## «?»-ի ոչ ավանդական օգտագործումը
203203

204-
Sometimes the question mark `?` is used as a replacement for `if`:
204+
Երբեմն `?` հարցական նշանն օգտագործվում է որպես `if`-ին փոխարինող.
205205

206206
```js run no-beautify
207-
let company = prompt('Which company created JavaScript?', '');
207+
let company = prompt('Ո՞ր ընկերությունն է ստեղծել JavaScript-ը:', '');
208208

209209
*!*
210210
(company == 'Netscape') ?
211-
alert('Right!') : alert('Wrong.');
211+
alert('Ճիշտ է:') : alert('Սխալ է:');
212212
*/!*
213213
```
214214

215-
Depending on the condition `company == 'Netscape'`, either the first or the second expression after the `?` gets executed and shows an alert.
215+
Կախված `company == 'Netscape'` պայմանից, գործարկվում է առաջին կամ երկրորդ արտահայտությունը `?`-ից հետո և ցույց է տալիս alert-ը:
216216

217-
We don't assign a result to a variable here. Instead, we execute different code depending on the condition.
217+
Մենք այստեղ արդյունքը չենք վերագրում փոփոխականի: Փոխարենը մենք գործարկում ենք տարբեր կոդեր՝ կախված պայմանից։
218218

219-
**It's not recommended to use the question mark operator in this way.**
219+
**Խորհուրդ չի տրվում այս եղանակով օգտագործել հարցական նշանի օպերատորը։**
220220

221-
The notation is shorter than the equivalent `if` statement, which appeals to some programmers. But it is less readable.
221+
Նշանագրությունն ավելի կրճատ է, քան համարժեք `if` դրույթի դեպքում, ինչը գրավիչ է որոշ ծրագրավորողների համար: Բայց դա ավելի քիչ ընթեռնելի է։
222222

223-
Here is the same code using `if` for comparison:
223+
Ահա նույն կոդը, որտեղ համեմատության համար օգտագործված է `if`.
224224

225225
```js run no-beautify
226-
let company = prompt('Which company created JavaScript?', '');
226+
let company = prompt('Ո՞ր ընկերությունն է ստեղծել JavaScript-ը:', '');
227227

228228
*!*
229229
if (company == 'Netscape') {
230-
alert('Right!');
230+
alert('Ճիշտ է:');
231231
} else {
232-
alert('Wrong.');
232+
alert('Սխալ է:');
233233
}
234234
*/!*
235235
```
236236

237-
Our eyes scan the code vertically. Code blocks which span several lines are easier to understand than a long, horizontal instruction set.
237+
Մեր աչքերը ուղղահայաց են սքանավորում կոդը: Կոդի բլոկները, որոնք ընդգրկում են մի քանի տող, ավելի հասկանալի են, քան երկար, հորիզոնական հրահանգների հավաքածուն:
238238

239-
The purpose of the question mark operator `?` is to return one value or another depending on its condition. Please use it for exactly that. Use `if` when you need to execute different branches of code.
239+
Հարցական նշանի օպերատորի `?` նպատակն է վերադարձնել այս կամ այն արժեքը՝ կախված իր պայմանից: Օգտագործեք այն հենց դրա համար: Օգտագործեք `if`, երբ անհրաժեշտ է գործարկել կոդի տարբեր ճյուղեր:

0 commit comments

Comments
 (0)