> For the complete documentation index, see [llms.txt](https://magnum-opus.gitbook.io/c++-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://magnum-opus.gitbook.io/c++-learning/leetcode/2011.-final-value-of-variable-after-performing-operations.md).

# 2011. Final Value of Variable After Performing Operations

There is a programming language with only **four** operations and **one** variable `X`:

* `++X` and `X++` **increments** the value of the variable `X` by `1`.
* `--X` and `X--` **decrements** the value of the variable `X` by `1`.

Initially, the value of `X` is `0`.

Given an array of strings `operations` containing a list of operations, return *the **final** value of* `X` *after performing all the operations*.

&#x20;

**Example 1:**

<pre><code><strong>Input: operations = ["--X","X++","X++"]
</strong><strong>Output: 1
</strong><strong>Explanation: The operations are performed as follows:
</strong>Initially, X = 0.
--X: X is decremented by 1, X =  0 - 1 = -1.
X++: X is incremented by 1, X = -1 + 1 =  0.
X++: X is incremented by 1, X =  0 + 1 =  1.
</code></pre>

**Example 2:**

<pre><code><strong>Input: operations = ["++X","++X","X++"]
</strong><strong>Output: 3
</strong><strong>Explanation: The operations are performed as follows:
</strong>Initially, X = 0.
++X: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
X++: X is incremented by 1, X = 2 + 1 = 3.
</code></pre>

**Example 3:**

<pre><code><strong>Input: operations = ["X++","++X","--X","X--"]
</strong><strong>Output: 0
</strong><strong>Explanation: The operations are performed as follows:
</strong>Initially, X = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
--X: X is decremented by 1, X = 2 - 1 = 1.
X--: X is decremented by 1, X = 1 - 1 = 0.
</code></pre>

{% code title="решение" %}

```cpp
int finalValueAfterOperations(vector<string>& operations) 
    {
        int x = 0;
        for(int i = 0; i < operations.size(); i++)
        {
            
            if(operations[i] == "--X" )
            {
                --x;
            }
            else if(operations[i] == "X--")
            {
                --x;
            }
            else
            {
                ++x;
            }
        }   
        return x; 
    }
```

{% endcode %}
