mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-23 23:03:40 +02:00
Translate Logic and Control Structures
This commit is contained in:
@@ -252,65 +252,79 @@ myObj.myThirdKey = true;
|
|||||||
myObj.myFourthKey; // = undefined
|
myObj.myFourthKey; // = undefined
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
|
// 3. Логика и управляющие структуры
|
||||||
// 3. Logic and Control Structures
|
// 3. Logic and Control Structures
|
||||||
|
|
||||||
|
//
|
||||||
|
// Синтаксис управляющих структур очень похож на его реализацию в Java.
|
||||||
// The syntax for this section is almost identical to Java's.
|
// The syntax for this section is almost identical to Java's.
|
||||||
|
|
||||||
|
// if работает так как вы ожидаете.
|
||||||
// The if structure works as you'd expect.
|
// The if structure works as you'd expect.
|
||||||
var count = 1;
|
var count = 1;
|
||||||
if (count == 3){
|
if (count == 3){
|
||||||
// evaluated if count is 3
|
// выполнится, если значение count равно 3
|
||||||
} else if (count == 4){
|
} else if (count == 4){
|
||||||
// evaluated if count is 4
|
// выполнится, если значение count равно 4
|
||||||
} else {
|
} else {
|
||||||
// evaluated if it's not either 3 or 4
|
// выполнится, если значение countне будет равно ни 3 ни 4
|
||||||
}
|
}
|
||||||
|
|
||||||
// As does while.
|
// Поведение while тоже вполне предсказуемо
|
||||||
while (true){
|
while (true){
|
||||||
// An infinite loop!
|
// Бесконечный цикл
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Циклы do-while похожи на while, но они всегда выполняются хотябы 1 раз.
|
||||||
// Do-while loops are like while loops, except they always run at least once.
|
// Do-while loops are like while loops, except they always run at least once.
|
||||||
var input
|
var input
|
||||||
do {
|
do {
|
||||||
input = getInput();
|
input = getInput();
|
||||||
} while (!isValid(input))
|
} while (!isValid(input))
|
||||||
|
|
||||||
|
// Цикл for такой же как в C b Java:
|
||||||
|
// инициализация; условие продолжения; итерация
|
||||||
// the for loop is the same as C and Java:
|
// the for loop is the same as C and Java:
|
||||||
// initialisation; continue condition; iteration.
|
// initialisation; continue condition; iteration.
|
||||||
for (var i = 0; i < 5; i++){
|
for (var i = 0; i < 5; i++){
|
||||||
|
// выполнится 5 раз
|
||||||
// will run 5 times
|
// will run 5 times
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// && - логическое и, || - логическое или
|
||||||
// && is logical and, || is logical or
|
// && is logical and, || is logical or
|
||||||
if (house.size == "big" && house.colour == "blue"){
|
if (house.size == "big" && house.color == "blue"){
|
||||||
house.contains = "bear";
|
house.contains = "bear";
|
||||||
}
|
}
|
||||||
if (colour == "red" || colour == "blue"){
|
if (color == "red" || color == "blue"){
|
||||||
|
// если цвет или красный или синий
|
||||||
// colour is either red or blue
|
// colour is either red or blue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// && и || удобны для установки значений по умолчанию.
|
||||||
// && and || "short circuit", which is useful for setting default values.
|
// && and || "short circuit", which is useful for setting default values.
|
||||||
var name = otherName || "default";
|
var name = otherName || "default";
|
||||||
|
|
||||||
|
|
||||||
|
// выражение switch проверяет равество с помощью ===
|
||||||
|
// используйте 'break' после каждого case
|
||||||
|
// иначе помимо правильного case выполнятся и все последующие.
|
||||||
// switch statement checks for equality with ===
|
// switch statement checks for equality with ===
|
||||||
// use 'break' after each case
|
// use 'break' after each case
|
||||||
// or the cases after the correct one will be executed too.
|
// or the cases after the correct one will be executed too.
|
||||||
grade = 'B';
|
grade = '4'; // оценка
|
||||||
switch (grade) {
|
switch (grade) {
|
||||||
case 'A':
|
case '5':
|
||||||
console.log("Great job");
|
console.log("Великолепно");
|
||||||
break;
|
break;
|
||||||
case 'B':
|
case '4':
|
||||||
console.log("OK job");
|
console.log("Неплохо");
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case '3':
|
||||||
console.log("You can do better");
|
console.log("Можно и лучше");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("Oy vey");
|
console.log("Да уж.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user