프로그래밍 언어에서는 일반적인 수학 연산과 유사한 연산자들이 지원됨.
산술, 단항, 대입 연산자 등이 있다.
기본적인 사칙연산 (+ - * /) 과 나머지 연산 % 을 지원한다.
// 덧셈
Console.WriteLine(3 + 2); // output: 5
// 뺄셈
Console.WriteLine(3 - 2); // output: 1
// 곱셈
Console.WriteLine(3 * 2); // output: 6
❗ 나눗셈은 주의할 점이 있음:
// 정수형 나눗셈
Console.WriteLine(5 / 3); // output: 1
// 실수형 나눗셈
Console.WriteLine(5f / 3); // output: 1.6
🔸 나머지 % 연산자는 나머지 값을 반환함:
Console.WriteLine(5 % 3); // output: 2
피연산자 앞에 ++ 또는 -- 를 사용하며, 위치에 따라 전위 / 후위 연산자로 구분된다.
int y = 0;
// 단항 연산자
Console.WriteLine(y++); // 후위증가연산자
Console.WriteLine(++y); // 전위증가연산자
Console.WriteLine(--y); // 전위감소연산자
Console.WriteLine(y--); // 후위감소연산자
++y → y를 먼저 증가시키고, 증가된 값을 반환📝 l-value: 메모리 주소를 가지는 값으로 대입 연산자의 왼쪽에 올 수 있음
y = 0;
Console.WriteLine("전위 연산자");
Console.WriteLine("y: " + y); // output: 0
Console.WriteLine("++y: " + ++y); // output: 1
Console.WriteLine("y: " + y); // output: 1
y++ → y를 먼저 반환하고, 나중에 증가📝 r-value: 메모리 주소가 없거나 변경 불가능한 값으로, 대입 연산자의 오른쪽에만 올 수 있음
y = 0;
Console.WriteLine("후위 연산자");
Console.WriteLine("y: " + y); // output: 0
Console.WriteLine("y++: " + y++); // output: 0
Console.WriteLine("y: " + y); // output: 1
우측 피연산자의 값을 좌측 변수에 대입(저장) 하는 연산자
// 대입 연산자
Console.WriteLine("대입 연산자( = )");
int x = 1; // 변수 x에 1을 대입
+=, -=, *=, /=, %= 등)x = x + 3 → x += 3 으로 간결하게 표현 가능// 복합 대입 연산자
Console.WriteLine(x = x + 1);
x = 1;
Console.WriteLine(x += 1);