Có những vấn đề xảy ra trong cuộc sống thực ra khi chúng ta cần phải đư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 vấn đề tương tự cũng phát sinh trong quá trình lập trình, nơi 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 hiện thi khối mã tiếp theo. Các câu lệnh được đưa ra quyết định trong ngôn ngữ lập trình quyết định hướng của luồng thực thi chương trình.
Trong Python, câu lệnh if-else elif được sử dụng để giải quyết
câu lệnh nếu
câu lệnh nếu là câu lệnh ra quyết định đơn giản nhất. Nó được sử dụng để quyết định xem một câu lệnh hoặc một khối câu lệnh nào sẽ được thực thi hay không, tức là nếu một điều kiện nhất định là đúng thì một khối câu lệnh được thực thi, đảo ngược thì không
cú pháp.
if condition: # Statements to execute if # condition is true
Ở đây, điều kiện sau khi đánh giá sẽ là đúng hay sai. if câu lệnh chấp nhận giá trị boolean - nếu giá trị là đúng thì nó sẽ thực thi khối câu lệnh bên dưới nó, đảo ngược lại thì không. Chúng ta có thể sử dụng điều kiện với dấu trích dẫn '[' ']'.
Như chúng ta đã biết, python sử dụng dòng đầu vào để xác định một khối. Vì vậy, khối bên dưới câu lệnh sẽ được xác định như thể hiện trong ví dụ dưới đây.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.
Save the command of the command if in Python
Ví dụ. Câu lệnh if trong Python
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.2
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
đầu ra.
I am Not in if
Như điều kiện có trong câu lệnh nếu sai. Vì vậy, khối bên dưới câu lệnh nếu được thực thi
if other
Chỉ riêng câu lệnh nếu cho chúng ta biết rằng nếu một điều kiện đúng, nó sẽ thực hiện một khối câu lệnh và nếu điều kiện sai thì nó sẽ không thực hiện. Nhưng nếu chúng ta muốn làm điều gì đó khác nếu điều kiện sai. Đây là tuyên bố khác. Chúng ta có thể sử dụng câu lệnh other với câu lệnh if để thực thi một khối mã khi điều kiện sai.
cú pháp.
________số 8Lưu đồ của câu lệnh if-else trong Python
Ví dụ 1. Câu lệnh if-else trong Python
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.2______13
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.2______13
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
I am Not in if5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.2____13
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
if [condition]: # Executes this block if # condition is true else: # Executes this block if # condition is false0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
if [condition]: # Executes this block if # condition is true else: # Executes this block if # condition is false4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
đầu ra.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.8
Khối mã theo sau câu lệnh khác được thực thi vì điều kiện hiện tại trong câu lệnh nếu là sai sau khi gọi câu lệnh không nằm trong khối [không có dấu cách]
Ví dụ 2. Python if other in the list list
if [condition]: # Executes this block if # condition is true else: # Executes this block if # condition is false6
if [condition]: # Executes this block if # condition is true else: # Executes this block if # condition is false7
if [condition]: # Executes this block if # condition is true else: # Executes this block if # condition is false8_______89
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.80
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.81
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.80
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.83
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.80
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.85
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.80
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.87
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.80_______289
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.30
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.31
if [condition]: # Executes this block if # condition is true else: # Executes this block if # condition is false7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.33
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.34
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.35
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.36
if [condition]: # Executes this block if # condition is true else: # Executes this block if # condition is false6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.38
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.39
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.30
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.30
lồng nhau - nếu
Một nếu lồng nhau là một câu lệnh nếu là mục tiêu của một câu lệnh nếu khác. Các câu lệnh if lồng nhau có nghĩa là một câu lệnh nếu bên trong một câu lệnh if other. Có, Python cho phép chúng ta lồng các lệnh if vào trong các câu lệnh if. tức là, chúng ta có thể đặt một câu lệnh nếu bên trong một câu lệnh nếu khác
cú pháp.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3
Lưu đồ của câu lệnh Python lồng nhau nếu
Ví dụ. Python lồng nhau nếu
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.32
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3____14
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.32
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3____14
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.40
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.32
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3____14
I am Not in if5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
đầu ra.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3
thang if-elif-else
Tại đây, người dùng có thể quyết định trong một số tùy chọn. Các câu lệnh nếu được thực hiện từ trên xuống. Ngay sau khi một trong các điều kiện kiểm soát nếu đúng, câu lệnh liên kết với nếu điều đó được thực thi và phần còn lại của cấp bậc bị loại bỏ. Nếu không có điều kiện nào là đúng, thì câu lệnh khác cuối cùng sẽ được thực thi
cú pháp.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
Lưu đồ của Python nếu câu lệnh if other elif
Ví dụ. Câu lệnh if else elif trong Python
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.2______13
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
I am Not in if60
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
đầu ra.
I am Not in if6
Câu lệnh nếu rút ngắn tay
Bất kể khi nào chỉ có một câu lệnh duy nhất được thực thi bên trong khối if thì lệnh viết tắt nếu có thể sử dụng được. The command could be set the same line with the command if.
cú pháp.
I am Not in if9
Ví dụ. Python if viết tắt
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.2
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
I am Not in if65
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
đầu ra
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.0
Lệnh rút ngắn if-else command
Điều này có thể được sử dụng để viết các câu lệnh if-else trong một dòng duy nhất mà chỉ có một câu lệnh được thực thi trong cả khối if và other.