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.

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

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 >

 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 :
        
    
    7 ban đầu là
    while :
        
    
    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 :
        
    
    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 :
        
    
    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 :
    
7 ban đầu là
while :
    
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 :
    
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 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 :
    
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 :
        
    
    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 :
    
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.

Ở đâ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 = 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 = 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 = 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 >> 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:

Khi

while :
    
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 >> 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 :
    
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 :
    
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 >>

# 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 > 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 >>

# 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 

# 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 

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 >

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 

Bài Viết Liên Quan

Chủ Đề