---
title: Python While Loops
description: "> ## Python While Loops – Python While Давталт ### Python Loops – Python Давталтууд Python has two primitive loop commands: Python-д хоёр үндсэн давталтын команд байдаг: - `while` loops `while` давталт - `for` loops `for` давталт ### The while Loop – while…"
url: "https://apprentice.mn/docs/python-while-loops/"
updated: "2024-07-29"
category: Python
---

# Python While Loops

> ## Python While Loops – Python While Давталт

### Python Loops – Python Давталтууд

Python has two primitive loop commands:

Python-д хоёр үндсэн давталтын команд байдаг:

- `while` loops

`while` давталт

- `for` loops

`for` давталт

### The while Loop – while Давталт

With the `while` loop we can execute a set of statements as long as a condition is true.

`while` давталт ашиглан нөхцөл үнэн байгаа тохиолдолд мэдэгдлүүдийг гүйцэтгэж болно.

#### Example – Жишээ

Print i as long as i is less than 6:

`i` нь 6-аас бага байхад хэвлэх:

`i = 1while i < 6:  print(i)  i += 1`

**Note:** remember to increment i, or else the loop will continue forever.

**Тайлбар:** `i`-ийг нэмэгдүүлэхээ мартахгүй байх хэрэгтэй, эс тэгвэл давталт зогсолтгүй үргэлжилнэ.

The `while` loop requires relevant variables to be ready, in this example we need to define an indexing variable, `i`, which we set to 1.

`while` давталт нь холбогдох хувьсагчдыг бэлэн байхыг шаарддаг. Энэ жишээнд бид индексийн хувьсагч болох `i`-г 1-тэй тэнцүү болгож тодорхойлсон.

### The break Statement – break Мэдэгдэл

With the `break` statement we can stop the loop even if the while condition is true:

`break` мэдэгдлийг ашиглан нөхцөл үнэн байсан ч давталтыг зогсоож болно.

#### Example – Жишээ

Exit the loop when i is 3:

`i` нь 3-тэй тэнцүү болох үед давталтаас гарах:

`i = 1while i < 6:  print(i)  if i == 3:    break  i += 1`

### The continue Statement – continue Мэдэгдэл

With the `continue` statement we can stop the current iteration, and continue with the next:

`continue` мэдэгдлийг ашиглан одоогийн давталтыг зогсоож, дараагийн давталтыг үргэлжлүүлж болно.

#### Example – Жишээ

Continue to the next iteration if `i` is 3:

Хэрвээ `i` нь 3 бол дараагийн давталтаас үргэлжлүүлнэ үү:

`i = 0while i < 6:  i += 1  if i == 3:    continue  print(i)`

### The else Statement – else Мэдэгдэл

With the `else` statement we can run a block of code once when the condition no longer is true:

`else` мэдэгдлийг ашиглан нөхцөл үнэн биш болбол нэг удаа блок кодын ажиллуулж болно.

##### Example

Print a message once the condition is false:

`i = 1while i < 6:  print(i)  i += 1else:  print("i нь 6-гаас бага биш байна")`

        [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-while-loops%2F&linkname=Python%C2%A0While%20Loops](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-while-loops%2F&linkname=Python%C2%A0While%20Loops)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-while-loops%2F&linkname=Python%C2%A0While%20Loops](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-while-loops%2F&linkname=Python%C2%A0While%20Loops)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-while-loops%2F&linkname=Python%C2%A0While%20Loops](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-while-loops%2F&linkname=Python%C2%A0While%20Loops)[https://www.addtoany.com/share](https://www.addtoany.com/share)
