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 > zero4,
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero5,
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero6 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 > zero4,
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero5,
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero6, or
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero7.[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 < one5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
For x starts at 0, x < 5, increment x Display "Are we having fun?" End3
For x starts at 0, x < 5, increment x Display "Are we having fun?" End4
For x starts at 0, x < 5, increment x Display "Are we having fun?" End5
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
8count 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 < one2
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one3
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one4
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
For x starts at 0, x < 5, increment x Display "Are we having fun?" End3
For x starts at 0, x < 5, increment x Display "Are we having fun?" End8
For x starts at 0, x < 5, increment x Display "Are we having fun?" End5
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero8
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one8
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one9
For x starts at 0, x < 5, increment x Display "Are we having fun?" End0
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 < one5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
Hello Geek Hello Geek Hello Geek5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
For x starts at 0, x < 5, increment x Display "Are we having fun?" End3
Hello Geek Hello Geek Hello Geek8
For x starts at 0, x < 5, increment x Display "Are we having fun?" End5
Đố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 < one5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
Hello Geek Hello Geek Hello Geek5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
For x starts at 0, x < 5, increment x Display "Are we having fun?" End3
List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3458
List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3459
while expression: statement[s]0
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
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 > zero6
Hello Geek Hello Geek Hello Geek0
Hello Geek Hello Geek Hello Geek1
Hello Geek Hello Geek Hello Geek2
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
Hello Geek Hello Geek Hello Geek5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
For x starts at 0, x < 5, increment x Display "Are we having fun?" End3
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 > zero9
For x starts at 0, x < 5, increment x Display "Are we having fun?" End3
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?" End5
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero6
Hello Geek Hello Geek Hello Geek0
Hello Geek Hello Geek Hello Geek1
List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3452
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 > zero9
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 > zero6
Hello Geek Hello Geek Hello Geek0
Hello Geek Hello Geek Hello Geek1
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 > zero9
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 > zero00
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero02
while expression:
while expression:
statement[s]
statement[s]
8count assigned five
Do
Display "Blast off is soon!"
Decrement count
While count > zero
04count assigned five
Do
Display "Blast off is soon!"
Decrement count
While count > zero
00count 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 > zero18
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero19
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero20
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero21
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero6
Hello Geek Hello Geek Hello Geek0
Hello Geek Hello Geek Hello Geek1
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero25
For x starts at 0, x < 5, increment x Display "Are we having fun?" End3
For x starts at 0, x < 5, increment x Display "Are we having fun?" End0
while expression: statement[s]4
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero29
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero30
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero6
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero33
Hello Geek Hello Geek Hello Geek1
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero25
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero36
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero37
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero39
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero41
For x starts at 0, x < 5, increment x Display "Are we having fun?" End5
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero45
Output:
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero0
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 > zero6
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero47
Hello Geek Hello Geek Hello Geek1
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero49
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero50
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero52
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero47
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero56
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero57
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero47__
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero37
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero64
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero67
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero68
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero70
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero72
Output:
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero1
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 > zero6
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero47
Hello Geek Hello Geek Hello Geek1
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero49
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero50
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero52
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero47
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero9
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero56
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero57
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero47__
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero37
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero91
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one5
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero67
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero68
Output:
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero2
count assigned five
Repeat
Display "Blast off is soon!"
Decrement count
Until count < one
5count assigned five
Do
Display "Blast off is soon!"
Decrement count
While count > zero
70count 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 < one5
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one01
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero6
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero47
Hello Geek Hello Geek Hello Geek1
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero49
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero50
Output:
For x starts at 0, x < 5, increment x Display "Are we having fun?" End2
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero67
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero68
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.