Hướng dẫn while loop inside if statement python - vòng lặp while bên trong câu lệnh if python

Các vòng lặp được sử dụng trong lập trình để lặp lại một khối mã cụ thể. Trong bài viết này, bạn sẽ học cách tạo một vòng lặp trong thời gian trong Python.

Show

Video: Python trong khi vòng lặp

Vòng lặp trong Python là gì?

Vòng lặp trong khi trong Python được sử dụng để lặp lại một khối mã miễn là biểu thức kiểm tra (điều kiện) là đúng.

Chúng tôi thường sử dụng vòng lặp này khi chúng tôi không biết số lần lặp lại trước.

Cú pháp trong khi vòng lặp trong Python

while test_expression:
    Body of while

Trong vòng lặp trong khi, biểu thức kiểm tra được kiểm tra trước. Cơ thể của vòng lặp chỉ được nhập nếu

Enter n: 10
The sum is 55
9 đánh giá là
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0. Sau một lần lặp, biểu thức kiểm tra được kiểm tra lại. Quá trình này tiếp tục cho đến khi
Enter n: 10
The sum is 55
9 đánh giá thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2.

Trong Python, cơ thể của vòng lặp được xác định thông qua thụt lề.

Cơ thể bắt đầu với vết lõm và dòng chưa được khai thác đầu tiên đánh dấu sự kết thúc.

Python diễn giải bất kỳ giá trị khác không là

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0.
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
4 và
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5 được hiểu là
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2.

Sơ đồ của vòng lặp trong khi

Hướng dẫn while loop inside if statement python - vòng lặp while bên trong câu lệnh if python
Sơ đồ trong khi vòng lặp trong Python

Ví dụ: Python trong khi vòng lặp

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)

Khi bạn chạy chương trình, đầu ra sẽ là:

Enter n: 10
The sum is 55

Trong chương trình trên, biểu thức kiểm tra sẽ là

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0 miễn là biến bộ đếm I của chúng tôi nhỏ hơn hoặc bằng n (10 trong chương trình của chúng tôi).

Chúng ta cần tăng giá trị của biến bộ đếm trong phần thân của vòng lặp. Điều này rất quan trọng (và chủ yếu là bị lãng quên). Không làm như vậy sẽ dẫn đến một vòng lặp vô hạn (vòng lặp không bao giờ kết thúc).

Cuối cùng, kết quả được hiển thị.


Trong khi vòng lặp với những người khác

Tương tự như với các vòng lặp, trong khi các vòng lặp cũng có thể có khối

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 tùy chọn.

Phần

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 được thực thi nếu điều kiện trong vòng lặp trong khi đánh giá thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2.

Vòng lặp trong khi có thể được chấm dứt với một tuyên bố phá vỡ. Trong những trường hợp như vậy, phần

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 bị bỏ qua. Do đó, phần
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 của một vòng lặp chạy nếu không có sự phá vỡ xảy ra và điều kiện là sai.

Dưới đây là một ví dụ để minh họa điều này.

'''Example to illustrate
the use of else statement
with the while loop'''

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 một biến bộ đếm để in chuỗi bên trong vòng ba lần.

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

Inside loop
Inside loop
Inside loop
Inside else
3 trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2. Do đó, phần
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 được thực thi.

Mục lục

  • Vòng lặp trong Python là gì?
    • Cú pháp trong khi vòng lặp trong Python
    • Trong vòng lặp trong khi, biểu thức kiểm tra được kiểm tra trước. Cơ thể của vòng lặp chỉ được nhập nếu
      Enter n: 10
      The sum is 55
      9 đánh giá là
      '''Example to illustrate
      the use of else statement
      with the while loop'''
      
      counter = 0
      
      while counter < 3:
          print("Inside loop")
          counter = counter + 1
      else:
          print("Inside else")
      0. Sau một lần lặp, biểu thức kiểm tra được kiểm tra lại. Quá trình này tiếp tục cho đến khi
      Enter n: 10
      The sum is 55
      9 đánh giá thành
      '''Example to illustrate
      the use of else statement
      with the while loop'''
      
      counter = 0
      
      while counter < 3:
          print("Inside loop")
          counter = counter + 1
      else:
          print("Inside else")
      2.
    • Ví dụ: Python trong khi vòng lặp
  • Trong khi vòng lặp với những người khác

Bây giờ bạn nên nắm bắt tốt cách thực hiện một đoạn mã lặp đi lặp lại. This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Mastering While Loops

Hướng dẫn tiếp theo trong loạt bài này bao gồm việc lặp lại xác định với các vòng lặp

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
76 thực hiện hiện tại trong đó số lần lặp lại được chỉ định một cách rõ ràng. means executing the same block of code over and over, potentially many times. A programming structure that implements iteration is called a loop.

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Làm chủ trong khi các vòng lặp

  • Chúng ta có thể sử dụng trong khi vòng lặp bên trong nếu câu lệnh trong Python không?indefinite iteration, the number of times the loop is executed isn’t specified explicitly in advance. Rather, the designated block is executed repeatedly as long as some condition is met.

  • Python cho phép trong khi các vòng bên trong trong khi các vòng lặp và các câu lệnh trong phần thân của các câu lệnh IF.definite iteration, the number of times the designated block will be executed is specified explicitly at the time the loop starts.

Tôi có thể sử dụng một vòng lặp trong một câu lệnh IF không?

  • Và để trả lời câu hỏi của bạn, vâng, nó hoàn toàn hợp lệ để làm tổ nếu các câu lệnh trong vòng lặp.
  • Vòng lặp một thời gian có thể ở trong vòng lặp Python một thời gian không?
  • Python trong khi Loop chỉ là một tuyên bố Python khác. Như bạn đã biết rằng trong khi cơ thể vòng lặp có thể chứa các câu lệnh, chúng ta có thể viết trong khi vòng lặp bên trong trong khi vòng lặp. Trong khi vòng lặp bên trong một vòng khác được gọi là lồng nhau trong khi vòng lặp.

Lặp đi lặp lại có nghĩa là thực thi cùng một khối mã nhiều lần, có khả năng nhiều lần. Một cấu trúc lập trình thực hiện lặp lại được gọi là một vòng lặp.

Trong lập trình, có hai loại lặp, không xác định và xác định:

Với lần lặp không xác định, số lần vòng lặp được thực thi được chỉ định trước một cách rõ ràng. Thay vào đó, khối được chỉ định được thực hiện nhiều lần miễn là một số điều kiện được đáp ứng.

Với lần lặp xác định, số lần khối được chỉ định sẽ được thực thi được chỉ định rõ ràng tại thời điểm vòng lặp bắt đầu.

while <expr>:
    <statement(s)>

Trong hướng dẫn này, bạn sẽ:

Tìm hiểu về vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3, cấu trúc điều khiển Python được sử dụng để lặp lại không xác định

Xem cách thoát ra khỏi vòng lặp hoặc vòng lặp lặp lại sớm

Xem xét vòng lặp này:

>>>

 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100

Ở đây, những gì mà xảy ra trong ví dụ này:

  • while <expr>:
        <statement(s)>
    
    7 ban đầu là
    while <expr>:
        <statement(s)>
    
    8. Biểu thức trong tiêu đề câu lệnh
    Inside loop
    Inside loop
    Inside loop
    Inside else
    3 trên dòng 2 là
     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    0, điều này là đúng, do đó, cơ thể vòng lặp thực thi. Bên trong thân vòng trên dòng 3,
    while <expr>:
        <statement(s)>
    
    7 bị giảm bởi
     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    2 xuống
     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    3, sau đó được in.

  • Khi phần thân của vòng lặp kết thúc, việc thực thi chương trình sẽ trở lại đầu vòng lặp ở dòng 2 và biểu thức được đánh giá lại. Nó vẫn còn đúng, vì vậy cơ thể thực hiện một lần nữa và

     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    4 được in.

  • Điều này tiếp tục cho đến khi

    while <expr>:
        <statement(s)>
    
    7 trở thành
    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    5. Tại thời điểm đó, khi biểu thức được kiểm tra, nó là sai và vòng lặp chấm dứt. Việc thực hiện sẽ tiếp tục tại câu lệnh đầu tiên theo cơ thể vòng lặp, nhưng có một trong trường hợp này.

Lưu ý rằng biểu thức kiểm soát của vòng

Inside loop
Inside loop
Inside loop
Inside else
3 được kiểm tra trước, trước khi bất cứ điều gì khác xảy ra. Nếu nó sai khi bắt đầu, cơ thể vòng lặp sẽ không bao giờ được thực thi:

>>>

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...

Ở đây, những gì mà xảy ra trong ví dụ này:

while <expr>:
    <statement(s)>
7 ban đầu là
while <expr>:
    <statement(s)>
8. Biểu thức trong tiêu đề câu lệnh
Inside loop
Inside loop
Inside loop
Inside else
3 trên dòng 2 là
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0, điều này là đúng, do đó, cơ thể vòng lặp thực thi. Bên trong thân vòng trên dòng 3,
while <expr>:
    <statement(s)>
7 bị giảm bởi
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
2 xuống
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
3, sau đó được in.

>>>

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo

Ở đây, những gì mà xảy ra trong ví dụ này:

while : 7 ban đầu là while : 8. Biểu thức trong tiêu đề câu lệnh Inside loop Inside loop Inside loop Inside else3 trên dòng 2 là 1>>> n = 5 2>>> while n > 0: 3... n -= 1 4... print(n) 5... 64 73 82 91 100 0, điều này là đúng, do đó, cơ thể vòng lặp thực thi. Bên trong thân vòng trên dòng 3, while : 7 bị giảm bởi 1>>> n = 5 2>>> while n > 0: 3... n -= 1 4... print(n) 5... 64 73 82 91 100 2 xuống 1>>> n = 5 2>>> while n > 0: 3... n -= 1 4... print(n) 5... 64 73 82 91 100 3, sau đó được in.

Khi phần thân của vòng lặp kết thúc, việc thực thi chương trình sẽ trở lại đầu vòng lặp ở dòng 2 và biểu thức được đánh giá lại. Nó vẫn còn đúng, vì vậy cơ thể thực hiện một lần nữa và

 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
4 được in.

  • Điều này tiếp tục cho đến khi

    while <expr>:
        <statement(s)>
    
    7 trở thành
    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    5. Tại thời điểm đó, khi biểu thức được kiểm tra, nó là sai và vòng lặp chấm dứt. Việc thực hiện sẽ tiếp tục tại câu lệnh đầu tiên theo cơ thể vòng lặp, nhưng có một trong trường hợp này.
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    5
    statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body.

  • Lưu ý rằng biểu thức kiểm soát của vòng

    Inside loop
    Inside loop
    Inside loop
    Inside else
    3 được kiểm tra trước, trước khi bất cứ điều gì khác xảy ra. Nếu nó sai khi bắt đầu, cơ thể vòng lặp sẽ không bao giờ được thực thi:
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    6
    statement immediately terminates the current loop iteration. Execution jumps to the top of the loop, and the controlling expression is re-evaluated to determine whether the loop will execute again or terminate.

Trong ví dụ trên, khi gặp vòng lặp,

while <expr>:
    <statement(s)>
7 là
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5. Biểu thức kiểm soát
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã sai, vì vậy cơ thể vòng lặp không bao giờ thực thi.

Hướng dẫn while loop inside if statement python - vòng lặp while bên trong câu lệnh if python
Ở đây, một vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 khác liên quan đến một danh sách, thay vì so sánh số:

Khi một danh sách được đánh giá trong bối cảnh Boolean, đó là sự thật nếu nó có các yếu tố trong đó và giả nếu nó trống rỗng. Trong ví dụ này,

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
2 là đúng miễn là nó có các yếu tố trong đó. Khi tất cả các mục đã được xóa bằng phương pháp
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
3 và danh sách trống,
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
2 là sai và vòng lặp chấm dứt.

 1n = 5
 2while n > 0:
 3    n -= 1
 4    if n == 2:
 5        break
 6    print(n)
 7print('Loop ended.')

Các câu lệnh Python

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 và
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
0

Trong mỗi ví dụ bạn đã thấy cho đến nay, toàn bộ phần thân của vòng

Inside loop
Inside loop
Inside loop
Inside else
3 được thực hiện trên mỗi lần lặp. Python cung cấp hai từ khóa chấm dứt lặp lại vòng lặp sớm:

Tuyên bố Python

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 ngay lập tức chấm dứt hoàn toàn một vòng lặp. Thực hiện chương trình tiến hành tuyên bố đầu tiên sau cơ thể vòng lặp.

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
1

Tuyên bố Python

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6 ngay lập tức chấm dứt lặp lại vòng lặp hiện tại. Việc thực thi nhảy lên đỉnh của vòng lặp và biểu thức kiểm soát được đánh giá lại để xác định xem vòng lặp sẽ thực thi lại hay chấm dứt.

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
2

Sự khác biệt giữa

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 và
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6 được thể hiện trong sơ đồ sau:

phá vỡ và tiếp tục

Ở đây, một tệp tập lệnh có tên là

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
2 thể hiện câu lệnh
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5:

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
3

Chạy

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
2 từ trình thông dịch dòng lệnh tạo ra đầu ra sau:

Hướng dẫn while loop inside if statement python - vòng lặp while bên trong câu lệnh if python

Khi

while <expr>:
    <statement(s)>
7 trở thành
>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
6, câu lệnh
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 được thực thi. Vòng lặp bị chấm dứt hoàn toàn và thực thi chương trình nhảy vào câu lệnh
>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
8 trên dòng 7.

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
4

Kịch bản tiếp theo,

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
9, giống hệt nhau ngoại trừ câu lệnh
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6 thay cho
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5:

Đầu ra của

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
9 trông như thế này:

Lần này, khi

while <expr>:
    <statement(s)>
7 là
>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
6, tuyên bố
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6 gây ra việc chấm dứt lần lặp đó. Do đó,
>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
6 được in. Việc thực thi trở về đầu vòng lặp, điều kiện được đánh giá lại và nó vẫn đúng. Vòng lặp tiếp tục, chấm dứt khi
while <expr>:
    <statement(s)>
7 trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5, như trước đây.

Điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

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

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
5

Python cho phép một mệnh đề

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 tùy chọn ở cuối vòng
Inside loop
Inside loop
Inside loop
Inside else
3. Đây là một tính năng độc đáo của Python, không được tìm thấy trong hầu hết các ngôn ngữ lập trình khác. Cú pháp được hiển thị bên dưới:

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
6

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
02 được chỉ định trong mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 sẽ được thực thi khi vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 chấm dứt.

Về bây giờ, bạn có thể đang nghĩ, "Điều đó hữu ích như thế nào?" Bạn có thể hoàn thành điều tương tự bằng cách đặt những câu lệnh đó ngay sau vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 mà không cần
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

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

Một trong những cách giải thích sau đây có thể giúp làm cho nó trực quan hơn:

  • Hãy nghĩ về tiêu đề của vòng lặp (

    # Program to add natural
    # numbers up to 
    # sum = 1+2+3+...+n
    
    # To take input from the user,
    # n = int(input("Enter n: "))
    
    n = 10
    
    # initialize sum and counter
    sum = 0
    i = 1
    
    while i <= n:
        sum = sum + i
        i = i+1    # update counter
    
    # print the sum
    print("The sum is", sum)
    25) như một câu lệnh
    while <expr>:
        <statement(s)>
    
    1 (
    # Program to add natural
    # numbers up to 
    # sum = 1+2+3+...+n
    
    # To take input from the user,
    # n = int(input("Enter n: "))
    
    n = 10
    
    # initialize sum and counter
    sum = 0
    i = 1
    
    while i <= n:
        sum = sum + i
        i = i+1    # update counter
    
    # print the sum
    print("The sum is", sum)
    27) được thực thi nhiều lần, với mệnh đề
    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    8 cuối cùng đã được thực thi khi điều kiện trở nên sai.

  • Hãy nghĩ về

    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    8 như thể đó là
    # Program to add natural
    # numbers up to 
    # sum = 1+2+3+...+n
    
    # To take input from the user,
    # n = int(input("Enter n: "))
    
    n = 10
    
    # initialize sum and counter
    sum = 0
    i = 1
    
    while i <= n:
        sum = sum + i
        i = i+1    # update counter
    
    # print the sum
    print("The sum is", sum)
    30, trong đó khối sau đó được thực thi nếu có một
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    5.

Nếu bạn không tìm thấy một trong hai cách giải thích này hữu ích, thì hãy bỏ qua chúng.

Khi nào một mệnh đề

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 trên vòng
Inside loop
Inside loop
Inside loop
Inside else
3 có ích không? Một tình huống phổ biến là nếu bạn đang tìm kiếm một danh sách cho một mục cụ thể. Bạn có thể sử dụng
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 để thoát vòng lặp nếu mục được tìm thấy và mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 có thể chứa mã có nghĩa là được thực thi nếu mục được tìm thấy:

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
7

Một điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 là một chút kỳ lạ, không thường thấy. Nhưng don lồng né tránh nó nếu bạn tìm thấy một tình huống mà bạn cảm thấy nó làm tăng thêm sự rõ ràng cho mã của bạn!

Vòng lặp vô hạn

Giả sử bạn viết một vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 mà về mặt lý thuyết không bao giờ kết thúc. Nghe kỳ lạ, phải không?

Xem xét ví dụ này:

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
8

Một điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 là một chút kỳ lạ, không thường thấy. Nhưng don lồng né tránh nó nếu bạn tìm thấy một tình huống mà bạn cảm thấy nó làm tăng thêm sự rõ ràng cho mã của bạn!Ctrl+C, which generates an interrupt from the keyboard. Otherwise, it would have gone on unendingly. Many
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
39 output lines have been removed and replaced by the vertical ellipsis in the output shown.

Vòng lặp vô hạn

Giả sử bạn viết một vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 mà về mặt lý thuyết không bao giờ kết thúc. Nghe kỳ lạ, phải không?

Xem xét ví dụ này:

Mã này đã bị chấm dứt bởi Ctrl+C, tạo ra một ngắt từ bàn phím. Nếu không, nó sẽ tiếp tục không ngừng. Nhiều dòng đầu ra

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
39 đã được gỡ bỏ và thay thế bằng dấu chấm lửng dọc trong đầu ra được hiển thị.

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
9

Một điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 là một chút kỳ lạ, không thường thấy. Nhưng don lồng né tránh nó nếu bạn tìm thấy một tình huống mà bạn cảm thấy nó làm tăng thêm sự rõ ràng cho mã của bạn!

Vòng lặp vô hạn

Enter n: 10
The sum is 55
0

Giả sử bạn viết một vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 mà về mặt lý thuyết không bao giờ kết thúc. Nghe kỳ lạ, phải không?

Xem xét ví dụ này:

Mã này đã bị chấm dứt bởi Ctrl+C, tạo ra một ngắt từ bàn phím. Nếu không, nó sẽ tiếp tục không ngừng. Nhiều dòng đầu ra # Program to add natural # numbers up to # sum = 1+2+3+...+n # To take input from the user, # n = int(input("Enter n: ")) n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is", sum)39 đã được gỡ bỏ và thay thế bằng dấu chấm lửng dọc trong đầu ra được hiển thị.

Rõ ràng,

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0 sẽ không bao giờ sai, hoặc chúng tôi đã gặp rắc rối rất lớn. Do đó,
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
41 bắt đầu một vòng lặp vô hạn về mặt lý thuyết sẽ chạy mãi mãi.

Enter n: 10
The sum is 55
1

Có lẽ điều đó không giống như một cái gì đó mà bạn muốn làm, nhưng mô hình này thực sự khá phổ biến. Ví dụ: bạn có thể viết mã cho một dịch vụ khởi động và chạy mãi mãi chấp nhận các yêu cầu dịch vụ. Ngay lập tức, trong bối cảnh này có nghĩa là cho đến khi bạn tắt nó, hoặc cho đến khi cái chết nhiệt của vũ trụ, tùy theo điều kiện nào đến trước.

>>>

Enter n: 10
The sum is 55
2

Một điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 là một chút kỳ lạ, không thường thấy. Nhưng don lồng né tránh nó nếu bạn tìm thấy một tình huống mà bạn cảm thấy nó làm tăng thêm sự rõ ràng cho mã của bạn!

Enter n: 10
The sum is 55
3

Vòng lặp vô hạn

Enter n: 10
The sum is 55
4

Enter n: 10
The sum is 55
5

Giả sử bạn viết một vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 mà về mặt lý thuyết không bao giờ kết thúc. Nghe kỳ lạ, phải không?

Xem xét ví dụ này:

Mã này đã bị chấm dứt bởi Ctrl+C, tạo ra một ngắt từ bàn phím. Nếu không, nó sẽ tiếp tục không ngừng. Nhiều dòng đầu ra # Program to add natural # numbers up to # sum = 1+2+3+...+n # To take input from the user, # n = int(input("Enter n: ")) n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is", sum)39 đã được gỡ bỏ và thay thế bằng dấu chấm lửng dọc trong đầu ra được hiển thị.

Rõ ràng,

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0 sẽ không bao giờ sai, hoặc chúng tôi đã gặp rắc rối rất lớn. Do đó,
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
41 bắt đầu một vòng lặp vô hạn về mặt lý thuyết sẽ chạy mãi mãi.

>>>

Enter n: 10
The sum is 55
6

Một điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 là một chút kỳ lạ, không thường thấy. Nhưng don lồng né tránh nó nếu bạn tìm thấy một tình huống mà bạn cảm thấy nó làm tăng thêm sự rõ ràng cho mã của bạn!

>>>

Enter n: 10
The sum is 55
7

Một điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 là một chút kỳ lạ, không thường thấy. Nhưng don lồng né tránh nó nếu bạn tìm thấy một tình huống mà bạn cảm thấy nó làm tăng thêm sự rõ ràng cho mã của bạn!

>>>

Enter n: 10
The sum is 55
8

Một điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 là một chút kỳ lạ, không thường thấy. Nhưng don lồng né tránh nó nếu bạn tìm thấy một tình huống mà bạn cảm thấy nó làm tăng thêm sự rõ ràng cho mã của bạn!

Vòng lặp vô hạn

Trong hướng dẫn này, bạn đã tìm hiểu về việc lặp lại không xác định bằng cách sử dụng vòng lặp Python

Inside loop
Inside loop
Inside loop
Inside else
3. Bây giờ bạn có thể:indefinite iteration using the Python
Inside loop
Inside loop
Inside loop
Inside else
3 loop. You’re now able to:

  • Xây dựng các vòng
    Inside loop
    Inside loop
    Inside loop
    Inside else
    3 cơ bản và phức tạp
  • Thực thi vòng lặp với
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    5 và
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    6
  • Sử dụng mệnh đề
    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    8 với vòng lặp
    Inside loop
    Inside loop
    Inside loop
    Inside else
    3
  • Đối phó với các vòng lặp vô hạn

Bây giờ bạn nên nắm bắt tốt cách thực hiện một đoạn mã lặp đi lặp lại.

Hướng dẫn tiếp theo trong loạt bài này bao gồm việc lặp lại xác định với các vòng lặp

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
76 thực hiện hiện tại trong đó số lần lặp lại được chỉ định một cách rõ ràng.definite iteration with
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
76 loops—recurrent execution where the number of repetitions is specified explicitly.

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Làm chủ trong khi các vòng lặp This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Mastering While Loops

Chúng ta có thể sử dụng trong khi vòng lặp bên trong nếu câu lệnh trong Python không?

Python cho phép trong khi các vòng bên trong trong khi các vòng lặp và các câu lệnh trong phần thân của các câu lệnh IF..

Tôi có thể sử dụng một vòng lặp trong một câu lệnh IF không?

Và để trả lời câu hỏi của bạn, vâng, nó hoàn toàn hợp lệ để làm tổ nếu các câu lệnh trong vòng lặp.yes it's perfectly valid to nest if statements in a while loop.

Vòng lặp một thời gian có thể ở trong vòng lặp Python một thời gian không?

Python trong khi Loop chỉ là một tuyên bố Python khác.Như bạn đã biết rằng trong khi cơ thể vòng lặp có thể chứa các câu lệnh, chúng ta có thể viết trong khi vòng lặp bên trong trong khi vòng lặp.Trong khi vòng lặp bên trong một vòng khác được gọi là lồng nhau trong khi vòng lặp.we can write while loop inside while loop. While loop inside another while loop is called Nested While Loop.