fbpx

A Guide to Operators in Solidity

Rahul Ravindran

Solidity is a statically typed language, which means that the type of each variable (state and local) needs to be specified. In addition, types can interact with each other in expressions containing operators. In this blog we will look at operators in solidity.

Logical Operators

A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. They include:

  • ! (logical negation): Reverse the result, returns false if the result is true
  • && (logical conjunction, “and”) : Returns true if both statements are true
  • || (logical disjunction, “or”): Returns true if one of the statements is true

    pragma solidity ^0.8.0;

    // Creating a contract
    contract logicalOperators{

    bool isRaining = true;
    bool isNight = false;

    // will return true
    bool a = !isNight;

    // will return true
    bool b = isRaining || isNight;

    // will return false
    bool c = isRaining && isNight

    }

Comparison Operators

Comparison operators are used to compare values of 2 integers

  • == (equality) : Returns true if values are same
  • != (inequality): Returns true if values are not the same
  • > (Greater than): Checks if left value is greater than right or not, returns true if greater, and vice-versa
  • < (Less than): Checks if left value is less than right or not, returns true if less, and vice-versa

0 Comments

Leave a Reply

More great articles

10x Solidity Development using Foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust. Foundry is consistently 1.5x-11x…

Read Story

Getting Started With Hardhat Smart Contract Framework

Hardhat is a new smart contract framework that is gaining popularity in the past year. It makes development and debugging…

Read Story

Getting Started With Ganache V7

Intro OK developers welcome back! In this video I think it's time for us to revisit Ganache. Ganache is part…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]
Arrow-up