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. Bây giờ 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 2
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 2 trong Python được sử dụng để chạy mã khối cho đến khi đáp ứng một điều kiện nhất địnhCú 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 2 làwhile condition:
# body of while loop
Đây, - 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 2 đá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 6 - 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 6 ướ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 8, 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 2 được thực thi # 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 được đánh giá lại- Quá trình này tiếp tục cho đến khi điều kiện là
1
2
3
4
5 1 - 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 6 đánh giá thành 1
2
3
4
5 1, vòng lặp dừng lại
Lưu đồ của Python trong khi Vòng lặpLưu đồ vòng lặp while
Ví 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 4Hành động_______10_______5
1
2
3
4
5 6# 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 81
2
3
4
5 8 is printed. 1
2
3
4
5 9 được tăng lên 2. # program to calculate the sum of numbers
# until the user enters zero
total = 0
number = int(input('Enter a number: '))
# add numbers until number is zero
while number != 0:
total += number # total = total + number
# take integer input again
number = int(input('Enter a number: '))
print('total =', total) 0
1
2
3
4
5 6# 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 8# program to calculate the sum of numbers
# until the user enters zero
total = 0
number = int(input('Enter a number: '))
# add numbers until number is zero
while number != 0:
total += number # total = total + number
# take integer input again
number = int(input('Enter a number: '))
print('total =', total) 3 is printed. 1
2
3
4
5 9 được tăng lên 3. # program to calculate the sum of numbers
# until the user enters zero
total = 0
number = int(input('Enter a number: '))
# add numbers until number is zero
while number != 0:
total += number # total = total + number
# take integer input again
number = int(input('Enter a number: '))
print('total =', total) 5
1
2
3
4
5 6# 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 8# program to calculate the sum of numbers
# until the user enters zero
total = 0
number = int(input('Enter a number: '))
# add numbers until number is zero
while number != 0:
total += number # total = total + number
# take integer input again
number = int(input('Enter a number: '))
print('total =', total) 8 is printed. 1
2
3
4
5 9 được tăng lên 4. Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 11 0
1
2
3
4
5 6# 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 8Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 11 3 is printed. 1
2
3
4
5 9 được tăng lên 5. Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 11 5
1
2
3
4
5 6# 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 8Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 11 8 is printed. 1
2
3
4
5 9 được tăng lên 6. age = 32
# the test condition is always True
while age > 18:
print('You can vote') 0
1
2
3
4
5 6_______10_______1Vòng lặp kết thúc
ví dụ 2. Python trong khi vòng lặp# program to calculate the sum of numbers
# until the user enters zero
total = 0
number = int(input('Enter a number: '))
# add numbers until number is zero
while number != 0:
total += number # total = total + number
# take integer input again
number = int(input('Enter a number: '))
print('total =', total)
đầu ra Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 11 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 6 luôn có giá trị 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 8. Do đó, thân vòng lặp sẽ chạy vô hạn lần
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.
Điều gì xảy ra với while trong Python?
Trong khi có thể có 2 điều kiện trong Python?
Tôi có thể sử dụng cái gì thay cho vòng lặp in range for trong Python?
Thay vì sử dụng hàm range(), chúng ta có thể sử dụng hàm enumerate() tích hợp sẵn trong python. enumerate() cho phép chúng ta lặp qua một chuỗi nhưng nó theo dõi cả chỉ mục và phần tử. |