C#에서 10가지 다양한 논리적인 코드 입니다.
논리 NOT 연산자:
csharp
Copy code
bool condition = false;
bool result = !condition;
논리 AND 연산자:
csharp
Copy code
bool condition1 = true;
bool condition2 = false;
bool result = condition1 && condition2;
논리 OR 연산자:
csharp
Copy code
bool condition1 = true;
bool condition2 = false;
bool result = condition1 || condition2;
논리 XOR 연산자:
csharp
Copy code
bool condition1 = true;
bool condition2 = false;
bool result = condition1 ^ condition2;
논리적인 IF문:
csharp
Copy code
bool condition = true;
if (condition)
{
// 조건이 참일 때 실행되는 코드
}
else
{
// 조건이 거짓일 때 실행되는 코드
}
논리적인 IF-ELSE IF-ELSE문:
csharp
Copy code
int number = 10;
if (number > 10)
{
// number가 10보다 큰 경우 실행되는 코드
}
else if (number < 10)
{
// number가 10보다 작은 경우 실행되는 코드
}
else
{
// number가 10과 같은 경우 실행되는 코드
}
삼항 연산자:
csharp
Copy code
int number = 5;
string result = (number > 0) ? "양수" : "음수";
Switch 문:
csharp
Copy code
int dayOfWeek = 2;
switch (dayOfWeek)
{
case 1:
Console.WriteLine("월요일");
break;
case 2:
Console.WriteLine("화요일");
break;
case 3:
Console.WriteLine("수요일");
break;
//...
default:
Console.WriteLine("알 수 없는 요일");
break;
}
Null 조건부 연산자:
csharp
Copy code
string text = null;
int length = text?.Length ?? 0;
Null 병합 연산자:
csharp
Copy code
string name = null;
string displayName = name ?? "Unknown";
위 예시들은 C#에서 논리적인 조건을 사용하는 다양한 코드 예시입니다. 이러한 코드를 기반으로 논리 연산과 조건문을 활용하여 원하는 로직을 구현할 수 있습니다.
I am a father of one boy and two girls living in Cheongju. We love to go "plogging" together every day. Plogging is an activity where we pick up litter while taking a walk, with the goal of preserving the environment and creating a clean community. It's a valuable time for our family to join forces.
You can find our activities on YouTube at https://www.youtube.com/@0070TV. We kindly ask for your support and encouragement.
'[프로그램]' 카테고리의 다른 글
| C#에서 Dictionary를 사용하기 위해 System.Collections.Generic 네임스페이스를 추가해야 합니다. (0) | 2023.06.28 |
|---|---|
| c#PLC 연결시 이더넷,232,485 (0) | 2023.06.27 |
| c# txt to txt통신 코드 (1) | 2023.06.27 |
| C#을 사용하여 Windows 시작 시 자동으로 백그라운드에서 실행되는 코드 (0) | 2023.06.27 |
| C# 포커 게임의 예시 소스 코드 (0) | 2023.06.26 |