> For the complete documentation index, see [llms.txt](https://docs.sysout.co.kr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sysout.co.kr/base-language/java/beginner/operator/increase-decrease-operation.md).

# 증감 연산

## 증감 연산

이 문서에서는 `증감 연산`에 대해서 살펴본다.

### 증감 연산이란

증가 연산은 값을 1만큼 `증가` 또는 `감소`시키기 위한 연산이다. 따라서 증감 연산은 복합대입 연산의 `+=1`, `-=1`과 같다. 프로그래밍에서는 1 증가 혹은 감소를 사용할 일이 많기 때문에 편리하게 쓸 수 있도록 만들어놓은 연산이다. 또한 위치를 조절하여 우선순위를 제어할 수 있다는 특징이 있다.

### 증감 연산자

증감 연산자는 다음과 같이 존재한다

| 연산자 | 사용 예 | 설명                    |
| --- | ---- | --------------------- |
| ++  | ++a  | a를 다른 작업보다 먼저 1 증가시킨다 |
|     | a++  | a를 다른 작업을 마치고 1 증가시킨다 |
| --  | --a  | a를 다른 작업보다 먼저 1 감소시킨다 |
|     | a--  | a를 다른 작업을 마치고 1 감소시킨다 |

### 증감 연산자 데모 1

```java
import java.lang.*;

public class IncDecOperatorExample1 {
    public static void main(String[] args){
        int value = 10;
        
        System.out.println(value++);
        System.out.println(++value);
        
        System.out.println(value--);
        System.out.println(--value);
    }
}
```

실행 결과는 다음과 같다.

```
10
12
12
10
```

코드에 대해서 하나씩 살펴보면 다음과 같다.

```java
System.out.println(value++);
```

이 코드에서는 출력과 증가 작업이 동시에 실행되고 있다. 이 때, `++`가 변수의 뒤에 있기 때문에 우선순위가 나중으로 밀리게 되어 출력을 먼저 하고 증가를 나중에 하는 상황이 된다. 위의 코드를 풀어서 작성하면 다음과 같다.

```java
System.out.println(value);
value += 1;
```

그 다음줄 코드는 증가를 먼저 하도록 `++`가 변수의 앞에 작성되어 있다.

```java
System.out.println(++value);
```

따라서 풀어쓰면 다음과 같은 코드가 된다.

```java
value += 1;
System.out.println(value);
```

결론적으로 예제를 풀어서 작성하면 다음과 같이 된다.

```java
import java.lang.*;

public class IncDecOperatorExample1_1 {
    public static void main(String[] args){
        int value = 10;
        
        System.out.println(value);
        value += 1;
        
        value += 1;
        System.out.println(value);
        
        System.out.println(value);
        value -= 1;
        
        value -= 1;
        System.out.println(value);
    }
}
```
