Javascript Loop While

3 min read

Loops can execute a block of code as long as a specified condition is true.

Заасан нөхцөл үнэн бол л гогцоонууд кодын блокыг гүйцэтгэх боломжтой.


The While Loop #

The while loop loops through a block of code as long as a specified condition is true.

While давталт нь заасан нөхцөл үнэн бол л блокийн кодоор дамждаг.

Syntax #

Синтакс #

while (condition) {
  // code block to be executed
}

Example Жишээ #

In the following example, the code in the loop will run, over and over again, as long as a variable (i) is less than 10:

Дараах жишээнд (i) хувьсагч 10-аас бага байвал л гогцоонд байгаа код дахин дахин ажиллана.

Example Жишээ #

while (i < 10) { text += "The number is " + i; i++; }

If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser.

Хэрэв та нөхцөлд ашигласан хувьсагчаа нэмэгдүүлэхээ мартсан бол давталт хэзээ ч дуусахгүй. Энэ нь таны хөтөчийг сүйрүүлэх болно.


The Do/While Loop #

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Do / while давталт нь while давталтын хувилбар юм. Энэ давталт нь нөхцөл үнэн эсэхийг шалгахаас өмнө кодын блокыг нэг удаа гүйцэтгэж, нөхцөл үнэн бол л давталтыг давтах болно.

Syntax #

Синтакс #

do {
  // code block to be executed
}
while (condition);

Example Жишээ #

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

Доорх жишээнд do / while давталтыг ашигласан болно. Нөхцөл шалгагдахаас өмнө кодын блок гүйцэтгэгддэг тул нөхцөл нь худлаа байсан ч гэсэн давталтыг ядаж нэг удаа гүйцэтгэх болно.

Example Жишээ #

do { text += "The number is " + i; i++; } while (i < 10);

Do not forget to increase the variable used in the condition, otherwise the loop will never end!

Нөхцөлд ашигласан хувьсагчийг нэмэгдүүлэхээ бүү мартаарай, тэгэхгүй бол давталт хэзээ ч дуусахгүй!


 
 
 
 
 
 
 

Comparing For and While #

For and while харьцуулах #

If you have read the previous chapter, about the for loop, you will discover that a while loop is much the same as a for loop, with statement 1 and statement 3 omitted.

Хэрэв та for гэсэн давталтын талаар өмнөх бүлгийг уншсан бол while давталт нь for loop-той адилхан бөгөөд 1-р мэдэгдэл, 3-р мэдэгдлийг орхигдуулсан болохыг олж мэдэх болно.

The loop in this example uses a for loop to collect the car names from the cars array:

Энэ жишээний давталт for loop машины багцаас машины нэрийг цуглуулахад ашигладаг.

Example Жишээ #

var cars = ["BMW", "Volvo", "Saab", "Ford"]; var i = 0; var text = ""; for (;cars[i];) { text += cars[i] + "<br>"; i++; }

The loop in this example uses a while loop to collect the car names from the cars array:

Энэ жишээний давталт нь машины багцаас машины нэрийг цуглуулахын тулд while давталтыг ашигладаг.

Example Жишээ #

var cars = ["BMW", "Volvo", "Saab", "Ford"]; var i = 0; var text = ""; while (cars[i]) { text += cars[i] + "<br>"; i++; }

Powered by BetterDocs

Leave a Reply