Một điều kiện cũng có thể được lồng trong một điều kiện khác. Ví dụ: giả sử chúng ta có hai biến số nguyên,
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here3 và
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here4. Mẫu lựa chọn sau đây cho thấy cách chúng ta có thể quyết định chúng có liên quan với nhau như thế nào
if x < y: print["x is less than y"] else: if x > y: print["x is greater than y"] else: print["x and y must be equal"]
Điều kiện bên ngoài chứa hai nhánh. Nhánh thứ hai [cái khác từ bên ngoài] chứa một câu lệnh
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here5 khác, có hai nhánh của riêng nó. Hai nhánh đó cũng có thể chứa các câu điều kiện
Luồng điều khiển cho ví dụ này có thể được nhìn thấy trong hình minh họa lưu đồ này
Đây là một chương trình hoàn chỉnh định nghĩa các giá trị cho
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here3 và
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here4. Chạy chương trình và xem kết quả. Sau đó thay đổi giá trị của các biến để thay đổi luồng điều khiển
Ghi chú
Trong một số ngôn ngữ lập trình, khớp if và other là một vấn đề. Tuy nhiên, trong Python đây không phải là trường hợp. Mẫu thụt đầu dòng cho chúng ta biết chính xác cái nào khác thuộc về cái nào nếu
Nếu bạn vẫn còn một chút không chắc chắn, đây là lựa chọn tương tự như một phần của ví dụ về codelens. Bước qua nó để xem cách chọn đúng
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here8
Có những tình huống trong cuộc sống thực khi chúng ta cần đưa ra một số quyết định và dựa trên những quyết định này, chúng ta quyết định mình nên làm gì tiếp theo. Các tình huống tương tự cũng phát sinh trong lập trình khi chúng ta cần đưa ra một số quyết định và dựa trên những quyết định này, chúng ta sẽ thực thi khối mã tiếp theo. Điều này được thực hiện với sự trợ giúp của các câu lệnh ra quyết định trong Python
Ví dụ
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here9
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here0
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here1
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here2
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here3
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here4
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here0
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here1
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here2
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here3
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here4
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here5
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here6
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here7____18
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here9
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here5
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here6
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here7____143
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here9
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here45
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here46
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here5
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here6
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here7____210
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here9
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here5
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here6
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here7____215
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here9
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here6
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here7
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here19
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here9
đầu ra
if [condition1]: # Executes when condition1 is true if [condition2]: # Executes when condition2 is true # if Block is end here # if Block is end here2
Câu lệnh Nested-if
Chúng ta có thể có một câu lệnh if…elif…else bên trong một câu lệnh if…elif…else khác. Điều này được gọi là lồng trong lập trình máy tính. Bất kỳ số lượng các câu lệnh này có thể được lồng vào nhau. Thụt lề là cách duy nhất để tìm ra mức độ lồng nhau. Điều này có thể gây nhầm lẫn, vì vậy phải tránh nếu chúng ta có thể