Increment/Decrement in solidity smart contract

eluconsmidar

eluconsmidar

Posted on May 8, 2024

Increment/Decrement in solidity smart contract

Here is a simple contract that you can get, increment and decrement the count store in this contract.

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Counter {
     uint256 public count;

     // Function to get the current count
     function get() public view returns (uint256) {
          return count;
     }

     // Function to increment count by 1
     function inc() public {
          count += 1;
     }

     // Function to decrement count by 1
     function dec() public {
          // This function will fail if count = 0
          count -= 1;
     }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
eluconsmidar
eluconsmidar

Posted on May 8, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related