Tôi có thể sử dụng phạm vi trong vòng lặp while trong Python không?

Chúng ta đã thấy cách thức hoạt động của vòng lặp for, và nếu xem xét kỹ hơn, chúng ta sẽ thấy rằng nó có một điểm chung với whiledo - while, một biểu thức điều kiện. Trong hầu hết các vòng lặp for, điều kiện là một inequality chẳng hạn như < hoặc > hoặc <= hoặc

while (conditional_expression){
     // runs only while conditional is truthy
}
0 để điều kiện có thể đúng với nhiều hơn một giá trị của biến iterator. Hãy xem xét

for (var i = 1; i <= 10; i++){
    console.log(i);
}

Vòng lặp dường như có một phạm vi cụ thể trong đó nó sẽ thực thi câu lệnh thân mã,

while (conditional_expression){
     // runs only while conditional is truthy
}
1 đến
while (conditional_expression){
     // runs only while conditional is truthy
}
2 như được xác định bởi giá trị bắt đầu (ban đầu),
while (conditional_expression){
     // runs only while conditional is truthy
}
1 và biểu thức điều kiện,
while (conditional_expression){
     // runs only while conditional is truthy
}
4. Nó đi qua phạm vi này theo các bước như được chỉ định bởi tham số cuối cùng,
while (conditional_expression){
     // runs only while conditional is truthy
}
5, tăng dần. Vòng lặp này sẽ lặp lại chính xác 10 lần

Một vòng lặp while (và do -while) không nhất thiết phải hoạt động trong một phạm vi nhất định mà chỉ phụ thuộc vào điều kiện để xác định xem các câu lệnh trong phần thân mã của nó sẽ được thực thi hay vòng lặp kết thúc. Không giống như vòng lặp for ở trên, không có phạm vi đã đặt (có thể có trong một số trường hợp, nhưng đó không phải là vấn đề). Vòng lặp while có thể chạy vô thời hạn hoặc thậm chí vô hạn nếu trình duyệt cho phép. Không có số lần chính xác mà nó có thể lặp lại, trừ khi nó được thiết lập với một bộ đếm

Sự khác biệt giữa whiledo - while là gì? . Vòng lặp while sẽ chỉ thực thi khi điều kiện là đúng, nghĩa là nó có thể không chạy chút nào. Vòng lặp do-while sẽ chạy ít nhất một lần, vì điều kiện không được kiểm tra cho đến khi các câu lệnh khối mã chạy xong

Trong lập trình, vòng lặp được sử dụng để lặp lại một khối mã. Ví dụ: nếu chúng tôi muốn hiển thị thông báo 100 lần, thì chúng tôi có thể sử dụng vòng lặp. Đây chỉ là một ví dụ đơn giản, chúng ta có thể đạt được nhiều hơn nữa với các vòng lặp

Trong hướng dẫn trước, chúng ta đã tìm hiểu về vòng lặp for trong Python. Sau đây, chúng ta sẽ tìm hiểu về vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0


Vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 trong Python được sử dụng để chạy một mã cụ thể cho đến khi đáp ứng một điều kiện nhất định

Cú pháp của vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 là

while condition:
    # body of while loop

Nơi đây,

  1. Một vòng lặp
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    0 đánh giá
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4
  2. Nếu
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 ước tính thành
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    6, mã bên trong vòng lặp
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    0 được thực thi
  3. # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 được đánh giá lại
  4. Quá trình này tiếp tục cho đến khi điều kiện là
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    9
  5. Khi
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 đánh giá thành
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    9, vòng lặp dừng lại

Lưu đồ cho Python While Loop

Tôi có thể sử dụng phạm vi trong vòng lặp while trong Python không?
Sơ đồ vòng lặp while trong Python

Thí dụ. Python trong khi vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1

đầu ra

1
2
3
4
5

Đây là cách chương trình hoạt động

Điều kiện biến.

1
2
3
4
5
2Hành động
1
2
3
4
5
3
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
1
2
3
4
5
6 is printed.
1
2
3
4
5
7 được tăng lên 2.
1
2
3
4
5
8
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
1 is printed.
1
2
3
4
5
7 được tăng lên 3.
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
3
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
6 is printed.
1
2
3
4
5
7 được tăng lên 4.
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
8
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
1 is printed.
1
2
3
4
5
7 được tăng lên 5.
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
3
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
6 is printed.
1
2
3
4
5
7 được tăng lên 6.
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
8
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9Vòng lặp kết thúc


ví dụ 2. Python while Loop để hiển thị cấp độ trò chơi

current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')

đầu ra

You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends

Trong ví dụ trên, chúng tôi đã sử dụng vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 để kiểm tra mức hiện tại và hiển thị nó trên bàn điều khiển


Vòng lặp vô hạn trong Python

Nếu

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 của một vòng lặp luôn là
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6, vòng lặp sẽ chạy vô hạn (cho đến khi bộ nhớ đầy). Ví dụ,

# infinite while loop
while True:
    # body of the loop

Trong ví dụ trên,

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 luôn là
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6. Do đó, thân vòng lặp sẽ chạy vô hạn lần


Python Vòng lặp While với other

Vòng lặp

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 cũng có thể có khối tùy chọn
# infinite while loop
while True:
    # body of the loop
7

Phần

# infinite while loop
while True:
    # body of the loop
7 được thực thi sau khi giá trị của
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 trong vòng lặp while ước tính thành
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9. Ví dụ,

counter = 0

while counter < 3:
    print('Inside loop')
    counter = counter + 1
else:
    print('Inside else')

đầu ra

Inside loop
Inside loop
Inside loop
Inside else

Ở đây, chúng tôi đã sử dụng biến đếm để in chuỗi

counter = 0

while counter < 3:
    print('Inside loop')
    counter = counter + 1
else:
    print('Inside else')
1 ba lần

Ở lần lặp thứ tư, điều kiện trong

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 trở thành
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9. Do đó, phần
# infinite while loop
while True:
    # body of the loop
7 được thực thi

Chúng ta có thể sử dụng phạm vi với vòng lặp while trong python không?

Đơn giản là chúng ta có thể sử dụng hàm while và range() trong python .

Vòng lặp while có thể có phạm vi không?

Không giống như vòng lặp for ở trên, không có phạm vi đã đặt (có thể có trong một số trường hợp, nhưng đó không phải là vấn đề). Vòng lặp while có thể chạy vô thời hạn hoặc thậm chí vô hạn nếu trình duyệt cho phép. Không có số lần chính xác mà nó có thể lặp lại, trừ khi nó được thiết lập với một bộ đếm.