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 + 12
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 + 12 trong Python được sử dụng để chạy mã khối 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 + 12 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 + 12 đá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 + 16
- 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 + 16 ướ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 + 18, 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 + 12 đượ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 + 16 đượ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 51
- 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 + 16 đánh giá thành 1 2 3 4 51, vòng lặp dừng lại
Lưu đồ của Python trong khi Vòng lặp
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
54Hành động_______10_______5
1
2
3
4
56# 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 + 181
2
3
4
58 is printed. 1
2
3
4
59 đượ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
56# 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 + 18# 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
59 đượ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
56# 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 + 18# 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
59 được tăng lên 4. Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 110
1
2
3
4
56# 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 + 18Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 113 is printed. 1
2
3
4
59 được tăng lên 5. Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 115
1
2
3
4
56# 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 + 18Enter a number: 12
Enter a number: 4
Enter a number: -5
Enter a number: 0
total = 118 is printed. 1
2
3
4
59 đượ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
56_______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 = 11Trong 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 + 16 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 + 18. Do đó, thân vòng lặp sẽ chạy vô hạn lần