연산자

Fruit·2023년 3월 28일

✨ Hello C#!

목록 보기
10/34
post-thumbnail

🌸 산술 연산자

+, -, *, /, %


🌸 증가 연산자, 감소 연산자

++, --

using System;

namespace IncDecOperator
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int a = 10;

            Console.WriteLine(a);
            Console.WriteLine(a++);		// 출력 후 증가
            Console.WriteLine(a);
            Console.WriteLine(++a);		// 증가 후 출력
        }
    }
}

[실행 결과]
10
10
11
12



🌸 관계 연산자

<, >, <=, >=, ==, !=


🌸 조건 연산자

조건식 ? 참일 때 값 : 거짓일 때 값

using System;

namespace ConditionalOperator
{
    class MainApp
    {
        static void Main(string[] args)
        {
            string result = (10 % 2) == 0 ? "짝수" : "홀수";

            Console.WriteLine(result);
        }
    }
}

[실행 결과]
짝수



🌸 논리 연산자

&& (AND), || (OR), ! (NOT)


🌸 할당 연산자

=, ==, -=, *=, /=, %=, &=, |=, |=, ^=, <<=, >>=

using System;

namespace AssignmentOperator
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int a = 100;

            Console.WriteLine($"a += 90: {a += 90}");
            Console.WriteLine($"a /= 10: {a /= 10}");
            Console.WriteLine($"a %= 2: {a %= 2}");
            Console.WriteLine($"a <<= 1: {a <<= 1}");
            Console.WriteLine($"a |= 1: {a |= 1}");     // 0010 | 0001 → 0011
        }
    }
}

[실행 결과]
a += 90: 190
a /= 10: 19
a %= 2: 1
a <<= 1: 2
a |= 1: 3

▪ 사진 출처: Pixabay - Robert Owen-Wahl

profile
🌼인생 참 🌻꽃🌻 같다🌼

0개의 댓글