[프로그램]

c# 논리 연산 코드 8가지 NOT and or exor if 삼항연산 null

Blackberrys 2023. 6. 27. 10:01
반응형


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.