Nói chung, các câu lệnh được thực thi tuần tự: câu lệnh đầu tiên trong một hàm được thực thi đầu tiên, theo sau là thứ hai, v.v. Có thể có một tình huống khi bạn cần thực thi một khối mã nhiều lần. Show
Ngôn ngữ lập trình cung cấp các cấu trúc điều khiển khác nhau cho phép các đường dẫn thực thi phức tạp hơn. Một câu lệnh LOOP cho phép chúng tôi thực thi một câu lệnh hoặc nhóm các câu lệnh nhiều lần. Biểu đồ sau đây minh họa một câu lệnh vòng lặp - 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.
Các câu lệnh điều khiển vòng lặpCác câu lệnh điều khiển vòng 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. Nhấp vào các liên kết sau để kiểm tra chi tiết của họ. Hãy để chúng tôi xem qua các câu lệnh điều khiển vòng lặp một cách ngắn gọn
Chạy một vòng vô hạn trong khi vòng lặp và chỉ phá vỡ nếu việc dừng lại được nâng lên. Trong khối thử, chúng tôi tìm nạp phần tử tiếp theo của trái cây với hàm tiếp theo ().Sau khi tìm nạp phần tử, chúng tôi đã thực hiện thao tác để được thực hiện với phần tử. (tức là in (trái cây))
while expression: statement(s) Hello Geek Hello Geek Hello Geek9 while condition: # execute these statements else: # execute these statements63 for iterator_var in sequence: statements(s)3 Example: Pythonif condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements43 Hello Geek Hello Geek Hello Geek7 while condition: # execute these statements else: # execute these statements71 while condition: # execute these statements else: # execute these statements72 Hello Geek Hello Geek Hello Geek9 if condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements2 if condition: # execute these statements else: # execute these statements3 Hello Geek Hello Geek Hello Geek6 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek6 if condition: # execute these statements else: # execute these statements7 if condition: # execute these statements else: # execute these statements8 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 while condition: # execute these statements else: # execute these statements2 while condition: # execute these statements else: # execute these statements3 Output: Hello Geek Hello Geek Hello Geek Sử dụng câu lệnh khác trong khi các vòng lặpNhư đã thảo luận ở trên, trong khi Loop thực thi khối cho đến khi một điều kiện được thỏa mãn. Khi điều kiện trở thành sai, câu lệnh ngay sau khi vòng lặp được thực thi. & Nbsp; Mệnh đề khác chỉ được thực thi khi điều kiện trong khi của bạn trở thành sai. Nếu bạn thoát ra khỏi vòng lặp, hoặc nếu một ngoại lệ được nâng lên, nó sẽ thắng được thực hiện. & NBSP; Nếu khác như thế này: if condition: # execute these statements else: # execute these statements Và trong khi vòng lặp như thế này tương tự while condition: # execute these statements else: # execute these statements Examples: PythonHello Geek Hello Geek Hello Geek6 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek8 Hello Geek Hello Geek Hello Geek9 if condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek In Else Block0 if condition: # execute these statements else: # execute these statements3 Hello Geek Hello Geek Hello Geek6 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek6 if condition: # execute these statements else: # execute these statements7 if condition: # execute these statements else: # execute these statements8 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 while condition: # execute these statements else: # execute these statements2 while condition: # execute these statements else: # execute these statements3 Sử dụng câu lệnh khác trong khi các vòng lặp if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 for iterator_var in sequence: statements(s)7 while condition: # execute these statements else: # execute these statements3 Output: Hello Geek Hello Geek Hello Geek In Else Block Như đã thảo luận ở trên, trong khi Loop thực thi khối cho đến khi một điều kiện được thỏa mãn. Khi điều kiện trở thành sai, câu lệnh ngay sau khi vòng lặp được thực thi. & Nbsp;Mệnh đề khác chỉ được thực thi khi điều kiện trong khi của bạn trở thành sai. Nếu bạn thoát ra khỏi vòng lặp, hoặc nếu một ngoại lệ được nâng lên, nó sẽ thắng được thực hiện. & NBSP; PythonHello Geek Hello Geek Hello Geek6 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek8 Hello Geek Hello Geek Hello Geek9 if condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek In Else Block0
Câu lệnh duy nhất trong khi khốiGiống như khối IF, nếu khối trong khi bao gồm một câu lệnh thì chúng ta có thể khai báo toàn bộ vòng lặp trong một dòng như hình dưới đây: Syntax: for iterator_var in sequence: statements(s) Hello Geek Hello Geek Hello Geek9 0 1 2 33 Hello Geek Hello Geek Hello Geek7__17 Python3LƯU Ý: Đề nghị không sử dụng loại vòng lặp này vì nó là một vòng lặp vô hạn không bao giờ kết thúc trong đó điều kiện luôn đúng và bạn phải chấm dứt mạnh mẽ trình biên dịch. Cho vòng lặp trong Python if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 geeks for geeks4 Đối với các vòng được sử dụng cho việc di chuyển tuần tự. Ví dụ: đi qua một danh sách hoặc chuỗi hoặc mảng, v.v. Trong Python, không có kiểu C cho vòng lặp, tức là, cho (i = 0; i 0 1 2 3 Nó có thể được sử dụng để lặp lại trên một phạm vi và lặp lại.Pythonwhile condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 geeks for geeks7 while condition: # execute these statements else: # execute these statements3 Hello Geek Hello Geek Hello Geek6 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek8 Hello Geek Hello Geek Hello Geek9 if condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek In Else Block0 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 geeks for geeks4 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek07 while condition: # execute these statements else: # execute these statements3 for iterator_var in sequence: statements(s)2 for iterator_var in sequence: statements(s)3 Câu lệnh duy nhất trong khi khối if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 geeks for geeks4 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek27 while condition: # execute these statements else: # execute these statements3 Giống như khối IF, nếu khối trong khi bao gồm một câu lệnh thì chúng ta có thể khai báo toàn bộ vòng lặp trong một dòng như hình dưới đây: Hello Geek Hello Geek Hello Geek9 0 1 2 33 Hello Geek Hello Geek Hello Geek7__17 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 geeks for geeks4 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek41 while condition: # execute these statements else: # execute these statements3 LƯU Ý: Đề nghị không sử dụng loại vòng lặp này vì nó là một vòng lặp vô hạn không bao giờ kết thúc trong đó điều kiện luôn đúng và bạn phải chấm dứt mạnh mẽ trình biên dịch. Cho vòng lặp trong Python Đối với các vòng được sử dụng cho việc di chuyển tuần tự. Ví dụ: đi qua một danh sách hoặc chuỗi hoặc mảng, v.v. Trong 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 một phạm vi và lặp lại. List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3452 Hello Geek Hello Geek Hello Geek7 List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3454 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek69 while condition: # execute these statements else: # execute these statements3 Is Đầu ra: if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 Hello Geek Hello Geek Hello Geek92 Output: List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 345 Ví dụ với List, tuple, String và Dictionary Lặp lại sử dụng cho các vòng lặpgeeks for geeks9 Hello Geek Hello Geek Hello Geek7 geeks for geeks Inside Else Block1 geeks for geeks Inside Else Block2 while condition: # execute these statements else: # execute these statements geeks for geeks Inside Else Block4 geeks for geeks Inside Else Block3 if condition: # execute these statements else: # execute these statements2 See the below example: PythonHello Geek Hello Geek Hello Geek6 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek8 Hello Geek Hello Geek Hello Geek9 if condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek In Else Block0 for iterator_var in sequence: statements(s)2 for iterator_var in sequence: statements(s)3 Output: geeks for geeks Câu lệnh duy nhất trong khi khốiGiống như khối IF, nếu khối trong khi bao gồm một câu lệnh thì chúng ta có thể khai báo toàn bộ vòng lặp trong một dòng như hình dưới đây: PythonHello Geek Hello Geek Hello Geek6 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek8 Hello Geek Hello Geek Hello Geek9 if condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek In Else Block0 for iterator_var in sequence: statements(s)2 for iterator_var in sequence: statements(s)3 for iterator_var in sequence: statements(s)2 for iterator_var in sequence: statements(s)3 Câu lệnh duy nhất trong khi khối Output: geeks for geeks Inside Else Block Giống như khối IF, nếu khối trong khi bao gồm một câu lệnh thì chúng ta có thể khai báo toàn bộ vòng lặp trong một dòng như hình dưới đây:Hello Geek Hello Geek Hello Geek9 0 1 2 33 Hello Geek Hello Geek Hello Geek7__17 Hello Geek Hello Geek Hello Geek0 LƯU Ý: Đề nghị không sử dụng loại vòng lặp này vì nó là một vòng lặp vô hạn không bao giờ kết thúc trong đó điều kiện luôn đúng và bạn phải chấm dứt mạnh mẽ trình biên dịch. Hello Geek Hello Geek Hello Geek1 Cho vòng lặp trong Python Pythonif condition: # execute these statements else: # execute these statements45 if condition: # execute these statements else: # execute these statements46 if condition: # execute these statements else: # execute these statements47 if condition: # execute these statements else: # execute these statements48 Is if condition: # execute these statements else: # execute these statements3 List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3455 if condition: # execute these statements else: # execute these statements60 List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3457 List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3458 if condition: # execute these statements else: # execute these statements63 if condition: # execute these statements else: # execute these statements64 while condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements66 Hello Geek Hello Geek Hello Geek7 if condition: # execute these statements else: # execute these statements68 while condition: # execute these statements else: # execute these statements3 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 Hello Geek Hello Geek Hello Geek46 Output: Hello Geek Hello Geek Hello Geek2 Các câu lệnh điều khiển vòng lặpCác câu lệnh điều khiển vòng thay đổi thực thi từ trình tự bình thường của chúng. 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. & NBSP; Tiếp tục tuyên bố:Nó trả lại điều khiển cho đầu vòng lặp. PythonList Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3455 if condition: # execute these statements else: # execute these statements74 List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3457 if condition: # execute these statements else: # execute these statements76 for iterator_var in sequence: statements(s)3 if condition: # execute these statements else: # execute these statements3 if condition: # execute these statements else: # execute these statements79 if condition: # execute these statements else: # execute these statements74 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek7 if condition: # execute these statements else: # execute these statements83 if condition: # execute these statements else: # execute these statements84 if condition: # execute these statements else: # execute these statements90 if condition: # execute these statements else: # execute these statements91 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements95 if condition: # execute these statements else: # execute these statements96 if condition: # execute these statements else: # execute these statements3 if condition: # execute these statements else: # execute these statements98 Hello Geek Hello Geek Hello Geek7 while condition: # execute these statements else: # execute these statements00 Output: Hello Geek Hello Geek Hello Geek3 Tuyên bố phá vỡ: & nbsp;Nó mang lại sự kiểm soát ra khỏi vòng lặp PythonList Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3455 if condition: # execute these statements else: # execute these statements74 List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3457 if condition: # execute these statements else: # execute these statements76 for iterator_var in sequence: statements(s)3 if condition: # execute these statements else: # execute these statements3 if condition: # execute these statements else: # execute these statements79 if condition: # execute these statements else: # execute these statements74 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek7 if condition: # execute these statements else: # execute these statements83 if condition: # execute these statements else: # execute these statements84 if condition: # execute these statements else: # execute these statements90 while condition: # execute these statements else: # execute these statements19 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements95 if condition: # execute these statements else: # execute these statements96 Output: Hello Geek Hello Geek Hello Geek4 if condition: # execute these statements else: # execute these statements3if condition: # execute these statements else: # execute these statements98Hello Geek Hello Geek Hello Geek7 while condition: # execute these statements else: # execute these statements00Tuyên bố phá vỡ: & nbsp; PythonList Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3455 if condition: # execute these statements else: # execute these statements74 List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3457 if condition: # execute these statements else: # execute these statements76 for iterator_var in sequence: statements(s)3 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements29 if condition: # execute these statements else: # execute these statements3 if condition: # execute these statements else: # execute these statements79 if condition: # execute these statements else: # execute these statements74 Hello Geek Hello Geek Hello Geek7 Hello Geek Hello Geek Hello Geek7 if condition: # execute these statements else: # execute these statements83 if condition: # execute these statements else: # execute these statements84 Output: Hello Geek Hello Geek Hello Geek5 if condition: # execute these statements else: # execute these statements3while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements1if condition: # execute these statements else: # execute these statements95if condition: # execute these statements else: # execute these statements96if condition: # execute these statements else: # execute these statements3 if condition: # execute these statements else: # execute these statements98 Hello Geek Hello Geek Hello Geek7 while condition: # execute these statements else: # execute these statements00 Tuyên bố phá vỡ: & nbsp; Python3Nó mang lại sự kiểm soát ra khỏi vòng lặp while condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements95 while condition: # execute these statements else: # execute these statements22 while condition: # execute these statements else: # execute these statements46 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements48 Tuyên bố vượt qua: & nbsp; 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ác câu lệnh, chức năng và lớp kiểm soát trống. while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements31 while condition: # execute these statements else: # execute these statements22
Python3Nó mang lại sự kiểm soát ra khỏi vòng lặp while condition: # execute these statements else: # execute these statements0 if condition: # execute these statements else: # execute these statements95 while condition: # execute these statements else: # execute these statements22 Tuyên bố vượt qua: & nbsp; while condition: # execute these statements else: # execute these statements65 while condition: # execute these statements else: # execute these statements66 for iterator_var in sequence: statements(s)3 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ác câu lệnh, chức năng và lớp kiểm soát trống. if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements48 while condition: # execute these statements else: # execute these statements0 while condition: # execute these statements else: # execute these statements31 while condition: # execute these statements else: # execute these statements22 if condition: # execute these statements else: # execute these statements3 while condition: # execute these statements else: # execute these statements19 Làm thế nào cho vòng lặp trong Python hoạt động trong nội bộ? Các câu lệnh Loop là gì?Vòng lặp chương trình là một loạt các câu lệnh thực hiện cho một số lần lặp lại được chỉ định hoặc cho đến khi các điều kiện được chỉ định được đáp ứng. Sử dụng mệnh đề WHRE để chỉ ra rằng vòng lặp sẽ thực hiện nhiều lần miễn là biểu thức trong khi đánh giá là true (1).a series of statements that executes for a specified number of repetitions or until specified conditions are met. Use the WHILE clause to indicate that the loop should execute repeatedly as long as the WHILE expression evaluates to true (1).
Tuyên bố vòng lặp với ví dụ là gì?Một vòng lặp được sử dụng để thực hiện một khối các câu lệnh liên tục cho đến khi một điều kiện cụ thể được thỏa mãn.Ví dụ: khi bạn hiển thị số từ 1 đến 100, bạn có thể muốn đặt giá trị của một biến thành 1 và hiển thị 100 lần, tăng giá trị của nó thêm 1 trên mỗi lần lặp lại.
Những gì cho vòng lặp trong Python đưa ra ví dụ?Vòng lặp trong Python là một hàm lặp.Nếu bạn có một đối tượng chuỗi như một danh sách, bạn có thể sử dụng vòng lặp để lặp lại các mục có trong danh sách.Chức năng của vòng lặp For không khác lắm so với những gì bạn thấy trong nhiều ngôn ngữ lập trình khác.an iterating function. If you have a sequence object like a list, you can use the for loop to iterate over the items contained within the list. The functionality of the for loop isn't very different from what you see in multiple other programming languages.
Tại sao vòng lặp được sử dụng trong Python?Các vòng lặp rất quan trọng trong Python hoặc trong bất kỳ ngôn ngữ lập trình nào khác vì chúng giúp bạn thực hiện một khối mã nhiều lần.Bạn sẽ thường xuyên đối mặt với các tình huống mà bạn sẽ cần sử dụng một đoạn mã nhiều lần nhưng bạn không muốn viết cùng một dòng mã nhiều lần.help you to execute a block of code repeatedly. You will often come face to face with situations where you would need to use a piece of code over and over but you don't want to write the same line of code multiple times. |