> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/gas-opt/syntactic-sugar.md).

# Syntactic Sugar

### Incrementation

There are four ways to increment and decrement a variable in Solidity.

```solidity
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.3;

contract test1{
   uint256 public number;

    // transaction cost: 43816
    function increment() public returns (uint256){
        number += 1;
        return number;
   }
}

contract test2{
   uint256 public number;

    // transaction cost: 43803
    function increment() public returns (uint256){
        number = number + 1;
        return number;
   }
}

contract test3{
   uint256 public number;

    // transaction cost: 43653 
    function increment() public returns (uint256){
        return number++;
   }
}

contract test4{
   uint256 public number;

   // transaction cost: 43647 
    function increment() public returns (uint256){
        return ++number;
   }
}
```

* Above list four contracts illustrating how we could increment a number variable by a value of 1.&#x20;
* Contract `test4` is the most gas effective out of the lot when ran.&#x20;

### Decrementing

```solidity
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.3;

contract test1{
   uint256 public number = 1;

    // transaction cost: 21894 
    function decrement() public returns (uint256){
        number -= 1;
        return number;
   }
}

contract test2{
   uint256 public number = 1;

    // transaction cost: 21881 
    function decrement() public returns (uint256){
        number = number - 1;
        return number;
   }
}

contract test3{
   uint256 public number = 1;

    // transaction cost: 21731  
    function decrement() public returns (uint256){
        return number--;
   }
}

contract test4{
   uint256 public number = 1;

   // transaction cost: 21725  
    function decrement() public returns (uint256){
        return --number;
   }
}
```

* Once again `test4` approach requires the least gas.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/eth-dev/gas-opt/syntactic-sugar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
