Hướng dẫn what is iterative control statements in python? - câu lệnh điều khiển lặp trong python là gì?

Kenneth Leroy Busbee và Dave Braunschweig

Tổng quan

Trong các cấu trúc điều khiển lặp, một câu lệnh hoặc khối được thực thi cho đến khi chương trình đạt đến một trạng thái nhất định hoặc các hoạt động đã được áp dụng cho mọi yếu tố của một bộ sưu tập. Điều này thường được thể hiện bằng các từ khóa như

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
4,
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
5,
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6 hoặc ________ 17. [1]iteration control structures, a statement or block is executed until the program reaches a certain state, or operations have been applied to every element of a collection. This is usually expressed with keywords such as
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
4,
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
5,
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6, or
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
7.[1]

Thảo luận

Thuộc tính cơ bản của cấu trúc điều khiển lặp là có thể lặp lại một số dòng mã. Hiển thị trực quan của phép lặp tạo ra một mẫu vòng tròn khi được lưu trữ, do đó, từ vòng lặp lặp được liên kết với các cấu trúc điều khiển lặp. Lặp lại có thể được thực hiện bằng & nbsp; kiểm tra trước các vòng, kiểm tra sau các vòng lặp và đếm các vòng. Một câu hỏi sử dụng các khái niệm Boolean thường kiểm soát tần suất vòng lặp sẽ thực thi.

Cấu trúc điều khiển lặp lại [lặp lại]

Mã giả: Trong khi

count assigned zero
While count < 5
    Display "I love computers!"
    Increment count
End

Mã giả: làm trong khi

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero

Mã giả: lặp lại cho đến khi

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one

Mã giả: cho

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End

Người giới thiệu

  • CNX.org: Nguyên tắc cơ bản lập trình - Một cách tiếp cận có cấu trúc mô -đun bằng cách sử dụng C ++

Một tuyên bố lặp lại là gì?

Các câu lệnh lặp lại gây ra các câu lệnh [hoặc báo cáo hỗn hợp] được thực thi bằng không hoặc nhiều lần, tuân theo một số tiêu chí chấm dứt vòng lặp. Khi các tuyên bố này là các tuyên bố ghép, chúng được thực thi theo thứ tự, ngoại trừ khi câu lệnh Break hoặc câu lệnh tiếp tục gặp phải.
Syntax :

while expression:
    statement[s]

Một tuyên bố lặp lại với các ví dụ là gì?

Đây là câu trả lời được xác minh của chuyên gia C ++ cung cấp bốn câu lệnh lặp-trong khi, làm, cho và dựa trên phạm vi cho. Ví dụ: Lặp lại là khi quy trình tương tự được lặp lại nhiều lần. Một số ví dụ là phân chia dài, số Fibonacci, số nguyên tố và trò chơi máy tính.

Ngôn ngữ lập trình Python cung cấp các loại vòng lặp sau để xử lý các yêu cầu vòng lặp.

Trong khi loopsyntax:

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
3
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
4
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
5

Output:

Hello Geek
Hello Geek
Hello Geek

Trong Python, tất cả các câu lệnh được thụt vào cùng một số lượng không gian ký tự sau khi cấu trúc lập trình được coi là một phần của một khối mã. Python sử dụng thụt lề làm phương pháp nhóm các câu lệnh.

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
8
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
0

In Python, there is no C style for loop, i.e., for [i=0; i zero4
count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
2
count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
3
count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
4

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
3
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
8
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
5

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
8
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
8
count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
9
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
0

Xem điều này cho một ví dụ trong đó trong khi vòng lặp được sử dụng cho các trình lặp. Như đã đề cập trong bài viết, không nên sử dụng trong khi vòng lặp cho các trình lặp trong Python.

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
Hello Geek
Hello Geek
Hello Geek
5

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
3
Hello Geek
Hello Geek
Hello Geek
8
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
5

Đối với trong Loopin Python, không có kiểu c cho vòng lặp, tức là, cho [i = 0; i

Nó có thể được sử dụng để lặp lại trên các trình lặp và một phạm vi.

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
Hello Geek
Hello Geek
Hello Geek
5

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
3
List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345
8
List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345
9

while expression:
    statement[s]
0
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
while expression:
    statement[s]
2
while expression:
    statement[s]
3
while expression:
    statement[s]
4
while expression:
    statement[s]
5
while expression:
    statement[s]
4
while expression:
    statement[s]
3
while expression:
    statement[s]
8

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
Hello Geek
Hello Geek
Hello Geek
0
Hello Geek
Hello Geek
Hello Geek
1
Hello Geek
Hello Geek
Hello Geek
2

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
Hello Geek
Hello Geek
Hello Geek
5

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
3
while expression:
    while expression: 
        statement[s]
        statement[s]
2
while expression:
    while expression: 
        statement[s]
        statement[s]
3

for iterator_var in sequence:
    statements[s]
0
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
3
while expression:
    statement[s]
3
while expression:
    statement[s]
4
while expression:
    statement[s]
5
while expression:
    statement[s]
4
while expression:
    statement[s]
3
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
5

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
Hello Geek
Hello Geek
Hello Geek
0
Hello Geek
Hello Geek
Hello Geek
1
List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345
2

for iterator_var in sequence:
    for iterator_var in sequence:
        statements[s]
        statements[s]
0
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
for iterator_var in sequence:
    for iterator_var in sequence:
        statements[s]
        statements[s]
2

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
Hello Geek
Hello Geek
Hello Geek
0
Hello Geek
Hello Geek
Hello Geek
1
for iterator_var in sequence:
    for iterator_var in sequence:
        statements[s]
        statements[s]
6

while expression:
    while expression: 
        statement[s]
        statement[s]
4
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
while expression:
    while expression: 
        statement[s]
        statement[s]
6
while expression:
    while expression: 
        statement[s]
        statement[s]
7

Output:

List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345

while expression:
    while expression: 
        statement[s]
        statement[s]
8
while expression:
    while expression: 
        statement[s]
        statement[s]
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
00
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
02

while expression:
    while expression: 
        statement[s]
        statement[s]
8
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
04
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
00
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
07

Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.
Syntax:

for iterator_var in sequence:
    for iterator_var in sequence:
        statements[s]
        statements[s]

Cú pháp cho một câu lệnh LOOP trong ngôn ngữ lập trình Python như sau:

while expression:
    while expression: 
        statement[s]
        statement[s]

Một lưu ý cuối cùng về việc làm tổ vòng là chúng ta có thể đặt bất kỳ loại vòng nào bên trong bất kỳ loại vòng lặp nào khác. Ví dụ, A For Loop có thể ở trong vòng một thời gian hoặc ngược lại.

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
18
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
19
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
20
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
21

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
Hello Geek
Hello Geek
Hello Geek
0
Hello Geek
Hello Geek
Hello Geek
1
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
25
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
3
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
0
while expression:
    statement[s]
4
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
29
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
30

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
33
Hello Geek
Hello Geek
Hello Geek
1
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
25
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
36

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
37
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
39
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
41
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
5

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
45

Output:

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
0

Các câu lệnh điều khiển vòng lặp Câu lệnh điều khiển thay đổi thực thi từ trình tự bình thường của nó. Khi thực thi để lại một phạm vi, tất cả các đối tượng tự động được tạo trong phạm vi đó bị phá hủy. Python hỗ trợ các câu lệnh kiểm soát sau.
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

Tiếp tục tuyên bố trả lại điều khiển cho đầu vòng lặp.
It returns the control to the beginning of the loop.

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
47
Hello Geek
Hello Geek
Hello Geek
1
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
49
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
50

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
52
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
47
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
56
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
57
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
47__

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
37
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
64

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
67
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
68

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
70
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
72

Output:

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
1

Break tuyên bố mang lại quyền kiểm soát ra khỏi vòng lặp
It brings control out of the loop

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
47
Hello Geek
Hello Geek
Hello Geek
1
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
49
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
50

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
52
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
47
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
56
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
57
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
47__

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
37
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
91

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
67
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
68

Output:

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
2

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
70
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
9
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
72

We use pass statement to write empty loops. Pass is also used for empty control statement, function and classes.

Break tuyên bố mang lại quyền kiểm soát ra khỏi vòng lặp

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
5
count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one
01

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
6
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
47
Hello Geek
Hello Geek
Hello Geek
1
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
49
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
50

Output:

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End
2
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
67
count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero
68

Pass Statement Chúng tôi sử dụng câu lệnh PASS để viết các vòng trống. Pass cũng được sử dụng cho câu lệnh, chức năng và lớp kiểm soát trống.
How to print a list in reverse order [from last to first item] using while and for in loops.


Báo cáo kiểm soát lặp đi lặp lại là gì?

Trong các cấu trúc điều khiển lặp, một câu lệnh hoặc khối được thực thi cho đến khi chương trình đạt đến một trạng thái nhất định hoặc các hoạt động đã được áp dụng cho mọi yếu tố của một bộ sưu tập. Điều này thường được thể hiện bằng các từ khóa như trong khi, lặp lại, cho hoặc làm .. cho đến khi.a statement or block is executed until the program reaches a certain state, or operations have been applied to every element of a collection. This is usually expressed with keywords such as while , repeat , for , or do.. until .

Cấu trúc kiểm soát lặp lại trong Python là gì?

Một cấu trúc lặp là một cấu trúc sẽ thực hiện một chuỗi các câu lệnh liên tục nếu một điều kiện đúng hoặc một tác vụ không đầy đủ.Khi điều kiện không còn đúng và luồng điều khiển trở lại khối điều kiện, nó sẽ dừng lặp lại và chương trình sẽ chuyển sang bước tiếp theo của mã.one which will execute a sequence of statements repeatedly if a condition holds true or a task is incomplete. Once the condition is no longer true and the flow of control returns to the condition block it will stop the iteration and the program will move to the next step of code.

Một tuyên bố lặp lại là gì?

Các câu lệnh lặp lại gây ra các câu lệnh [hoặc báo cáo hỗn hợp] được thực thi bằng không hoặc nhiều lần, tuân theo một số tiêu chí chấm dứt vòng lặp.Khi các tuyên bố này là các tuyên bố ghép, chúng được thực thi theo thứ tự, ngoại trừ khi câu lệnh Break hoặc câu lệnh tiếp tục gặp phải.cause statements [or compound statements] to be executed zero or more times, subject to some loop-termination criteria. When these statements are compound statements, they are executed in order, except when either the break statement or the continue statement is encountered.

Một tuyên bố lặp lại với các ví dụ là gì?

Đây là câu trả lời được xác minh của chuyên gia C ++ cung cấp bốn câu lệnh lặp-trong khi, làm, cho và dựa trên phạm vi cho.Ví dụ: Lặp lại là khi quy trình tương tự được lặp lại nhiều lần.Một số ví dụ là phân chia dài, số Fibonacci, số nguyên tố và trò chơi máy tính.Iteration is when the same procedure is repeated multiple times. Some examples were long division, the Fibonacci numbers, prime numbers, and the calculator game.

Bài Viết Liên Quan

Chủ Đề