Share C# If Else: Sử Dụng Câu Lệnh If Else Trong C#

hongchau729

New member
## C# Nếu khác: Sử dụng câu lệnh IF khác trong C#

Câu lệnh `Nếu 'khác là một câu lệnh có điều kiện cho phép bạn thực thi mã khác nhau tùy thuộc vào điều kiện là đúng hay sai.Cú pháp của câu lệnh `if` khác như sau:

`` `C#
if (điều kiện) {
// mã để thực thi nếu điều kiện là đúng
} khác {
// mã để thực thi nếu điều kiện là sai
}
`` `

Ví dụ: mã sau kiểm tra xem biến `x` bằng 10. Nếu có, mã in thông báo" x bằng 10 ".Mặt khác, mã in thông báo "x không bằng 10".

`` `C#
int x = 10;

if (x == 10) {
Console.WriteLine ("x bằng 10");
} khác {
Console.WriteLine ("X không bằng 10");
}
`` `

Bạn cũng có thể sử dụng câu lệnh `khác nếu 'để kiểm tra nhiều điều kiện.Cú pháp của câu lệnh `khác nếu 'như sau:

`` `C#
if (điều kiện1) {
// mã để thực thi nếu điều kiện1 là đúng
} if if (điều kiện2) {
// mã để thực thi nếu điều kiện2 là đúng
} khác {
// mã để thực thi nếu không điều kiện1 và điều kiện2 là đúng
}
`` `

Ví dụ: mã sau kiểm tra xem biến `x` bằng 10, 20 hoặc 30. Nếu nó bằng 10, mã sẽ in thông báo" x bằng 10 ".Nếu nó bằng 20, mã sẽ in thông báo "x bằng 20".Nếu nó bằng 30, mã sẽ in thông báo "x bằng 30".Mặt khác, mã in thông báo "x không bằng 10, 20 hoặc 30".

`` `C#
int x = 10;

if (x == 10) {
Console.WriteLine ("x bằng 10");
} if if (x == 20) {
Console.WriteLine ("x bằng 20");
} khác if (x == 30) {
Console.WriteLine ("x bằng 30");
} khác {
Console.WriteLine ("X không bằng 10, 20 hoặc 30");
}
`` `

### hashtags

* #csharp
* #Conditionals
* #Nếu khác
* #Programming
* #tutorial
=======================================
## C# If Else: Use the IF Else statement in C#

The `if` else statement is a conditional statement that allows you to execute different code depending on whether a condition is true or false. The syntax of the `if` else statement is as follows:

```c#
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
```

For example, the following code checks if the variable `x` is equal to 10. If it is, the code prints the message "x is equal to 10". Otherwise, the code prints the message "x is not equal to 10".

```c#
int x = 10;

if (x == 10) {
Console.WriteLine("x is equal to 10");
} else {
Console.WriteLine("x is not equal to 10");
}
```

You can also use the `else if` statement to check multiple conditions. The syntax of the `else if` statement is as follows:

```c#
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if neither condition1 nor condition2 is true
}
```

For example, the following code checks if the variable `x` is equal to 10, 20, or 30. If it is equal to 10, the code prints the message "x is equal to 10". If it is equal to 20, the code prints the message "x is equal to 20". If it is equal to 30, the code prints the message "x is equal to 30". Otherwise, the code prints the message "x is not equal to 10, 20, or 30".

```c#
int x = 10;

if (x == 10) {
Console.WriteLine("x is equal to 10");
} else if (x == 20) {
Console.WriteLine("x is equal to 20");
} else if (x == 30) {
Console.WriteLine("x is equal to 30");
} else {
Console.WriteLine("x is not equal to 10, 20, or 30");
}
```

### Hashtags

* #csharp
* #Conditionals
* #ifelse
* #Programming
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top