Lựa chọn lồng nhau trong Python là gì?

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 here
3 và
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
4. 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 here
5 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 here
3 và
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
4. 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 here
8

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 here
9

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
0

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
1

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
2
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
3
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
4
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
0

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
1
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
2
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
3
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
4

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
5
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
6
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
7____18
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
9

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
5
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
6
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
7____143
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
9

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
45
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
46

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
5
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
6
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
7____210
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
9

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
5
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
6
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
7____215
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
9

if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
6
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
7
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
19
if [condition1]:
   # Executes when condition1 is true
   if [condition2]: 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here
9

đầ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 here
2

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ể

Ý nghĩa của việc lồng nhau trong Python là gì?

Các hàm lồng nhau [hoặc bên trong, lồng nhau] là các hàm mà chúng ta định nghĩa bên trong các hàm khác để truy cập trực tiếp vào các biến và tên được xác định trong hàm kèm theo. Nested functions have many uses, primarily for creating closures and decorators.

Tại sao IF lồng nhau được sử dụng trong Python?

Có thể xảy ra trường hợp bạn muốn kiểm tra một điều kiện khác sau khi một điều kiện chuyển thành true . Trong tình huống như vậy, bạn có thể sử dụng cấu trúc if lồng nhau.

Tại sao nó được gọi là lồng nhau nếu?

Câu lệnh if lồng nhau là câu lệnh if được đặt bên trong câu lệnh if khác . Các câu lệnh if lồng nhau thường được sử dụng khi bạn phải kiểm tra kết hợp các điều kiện trước khi quyết định hành động thích hợp.

Điều kiện lồng nhau là gì?

Câu lệnh điều kiện lồng nhau là câu lệnh if hoặc if else bên trong câu lệnh if other khác .

Chủ Đề