[증감 연산자]
1. 증감 연산자(Increment/Decrement Operator)
증감 연산자는 변수의 값을 1씩 증가시키거나 감소시키는 데 사용된다. C#에서는 ++가 증가 연산자이고, --가 감소 연산자 이다. 전치 증가와 후치 증가 두 가지 형태가 있다. 전치 증가는 변수를 증가시킨 후에 다른 연산을 수행한다. 후치 증가는 다른 연산을 수행한 후에 변수를 증가한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | using System; class Increment_DecrementOperator { static void Main() { //& 변수 선언(증가 연산자) int a = 5; Console.WriteLine("-전치 증가-"); int b = ++a; // a를 1 증가시키고, 그 값을 b에 할당 Console.WriteLine("a : " + a); // 결과 : 6 Console.WriteLine("b : " + b); // 결과 : 6 Console.Write("\n"); Console.WriteLine("-후치 증가-"); int c = a++; // a의 현재 값을 c에 할당하고, a를 1 증가시킴 Console.WriteLine("a : " + a); // 결과 : 7 Console.WriteLine("c : " + c); // 결과 : 6 Console.Write("\n"); //& 변수 선언(감소 연산자) int d = 8; Console.WriteLine("-전치 감소-"); int e = --d; // d를 1 감소시키고, 그 값을 e에 할당 Console.WriteLine("d : " + d); // 결과 : 7 Console.WriteLine("e : " + e); // 결과 : 7 Console.Write("\n"); Console.WriteLine("-후치 감소-"); int f = d--; // d의 현재 값을 f에 할당하고, d를 1 감소시킴 Console.WriteLine("d : " + d); // 결과 : 6 Console.WriteLine("f : " + f); // 결과 : 7 }// Main }// Increment_DecrementOperator | cs |
728x90
'프로그래밍 > C#' 카테고리의 다른 글
C#] 논리 연산자 (0) | 2024.01.12 |
---|---|
C#] 관계연산자/비교연산자 (1) | 2024.01.12 |
C#] 할당 연산자 (0) | 2024.01.12 |
C#] 문자열 연결 연산자 (0) | 2024.01.12 |
C#] 산술 연산자 (0) | 2024.01.12 |