<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Developers Forum for XinFin XDC Network: daniel weber</title>
    <description>The latest articles on Developers Forum for XinFin XDC Network by daniel weber (@daniel_weber_eaba2f02503a).</description>
    <link>https://www.xdc.dev/daniel_weber_eaba2f02503a</link>
    <image>
      <url>https://www.xdc.dev/images/mQZp6Zn-EWTilieuPvReIcn9c9T542zY8Qw8iqMwEL4/rs:fill:90:90/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL3VzZXIvcHJv/ZmlsZV9pbWFnZS8x/MC81ZTA4NTFmMC1j/NThhLTQ3NzgtODY3/Ni03NjVmNjRjYTlk/NjAuanBn</url>
      <title>Developers Forum for XinFin XDC Network: daniel weber</title>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://www.xdc.dev/feed/daniel_weber_eaba2f02503a"/>
    <language>en</language>
    <item>
      <title>Create a Smart Contract that Returns Address and Balance of Owner using Solidity.</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Sun, 31 Jul 2022 08:49:34 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/create-a-smart-contract-that-returns-address-and-balance-of-owner-using-solidity-6dn</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/create-a-smart-contract-that-returns-address-and-balance-of-owner-using-solidity-6dn</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Create a smart contract named MyContract having a state variable as owner. Create a constructor to fetch the address of the owner from msg and hold it into the state variable owner. Also, create a function getBalance() to show the current balance of the owner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Every smart contract is owned by an address called as owner. A smart contract can know its owner’s address using sender property and its available balance using a special built-in object called msg.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Open Remix-IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Select File Explorer from the left side icons and select Solidity in the environment. Click on New option below the Solidity environment. Enter the file name as MyContract.sol and Click on the OK button.&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/gS-nG8RGlOezAgGgjiE-Cd3Oor5TLVASmuP9Mw8-MXQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2VubDhueXp6bW93/a3JreWJkM2E4LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/gS-nG8RGlOezAgGgjiE-Cd3Oor5TLVASmuP9Mw8-MXQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2VubDhueXp6bW93/a3JreWJkM2E4LnBu/Zw" alt="Image description" width="880" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Enter the following Solidity Code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to
// retrieve address and
// balance of owner
pragma solidity ^0.6.8; 

// Creating a contract
contract MyContract
{
    // Private state variable
    address private owner;

    // Defining a constructor
    constructor() public{
        owner=msg.sender;
    }

    // Function to get
    // address of owner
    function getOwner(
    ) public view returns (address) {   
        return owner;
    }

    // Function to return
    // current balance of owner
    function getBalance(
    ) public view returns(uint256){
        return owner.balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;  Compile the file MyContract.sol from the Solidity Compiler tab. &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/fivGYR6XHNd0DZeXRiEwlE32Umtal0pNc6UV9Mhg2mk/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3cyZGljYmt3Mjh2/N3d3aWtyZ3NxLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/fivGYR6XHNd0DZeXRiEwlE32Umtal0pNc6UV9Mhg2mk/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3cyZGljYmt3Mjh2/N3d3aWtyZ3NxLnBu/Zw" alt="Image description" width="871" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Deploy the smart contract from the Deploy and Run Transaction tab and you will get the balance and address of the owner. &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/GSlw8aEBzIE3X-ifajQMUdgV5u_Duh2q0pIPnqcL0H8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/Lzh0YWZjbWVrYnVi/ODNkMXhtcWF3LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/GSlw8aEBzIE3X-ifajQMUdgV5u_Duh2q0pIPnqcL0H8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/Lzh0YWZjbWVrYnVi/ODNkMXhtcWF3LnBu/Zw" alt="Image description" width="880" height="678"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; The output below shows the address and the balance of the owner.&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/tX48ylN7qrO6UaItgqEK_TCLqrV0XwS1tTUKNqU3r2o/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzQ5anhoYzFhamxk/d213eDkzYmxoLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/tX48ylN7qrO6UaItgqEK_TCLqrV0XwS1tTUKNqU3r2o/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzQ5anhoYzFhamxk/d213eDkzYmxoLnBu/Zw" alt="Image description" width="367" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>tutorial</category>
      <category>learnsolidity</category>
    </item>
    <item>
      <title>What is Hashing in Solidity?</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Wed, 27 Jul 2022 04:38:55 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/what-is-hashing-in-solidity-2k5f</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/what-is-hashing-in-solidity-2k5f</guid>
      <description>&lt;p&gt;A cryptographic hash function is an algorithm that takes an arbitrary amount of data as input and produces the enciphered text of fixed size. Even a slight change in the input gives a completely different output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solidity provides the following cryptographic functions:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/4f74pTb9d7U8ZiDEHDSGbK9bWFXUtfo7_qkcv-eJF7c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2FwYng2NHk0dHk4/Yzl6MHZldDdrLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/4f74pTb9d7U8ZiDEHDSGbK9bWFXUtfo7_qkcv-eJF7c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2FwYng2NHk0dHk4/Yzl6MHZldDdrLnBu/Zw" alt="Image description" width="880" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ethereum uses Keccak for hashing which is similar but not the same as SHA_256. For proof of work, it uses a custom scheme called ethash which is designed to be ASIC-resistant.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, a smart contract is created to take a string as input and give an 8 digit hash as an output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pragma version
pragma solidity ^0.6.6; 

// Creating a contract
contract helloGeeks 
{
    // We want hash to be of 8 digits 
    // hence we store 10^8 which is 
    // used to extract first 8 digits 
    // later by Modulus 
    uint hashDigits = 8;

    // Equivalent to 10^8 = 8
    uint hashModulus = 10 ** hashDigits; 

    // Function to generate the hash value
    function _generateRandom(string memory _str) 
        public view returns (uint) 
    {
        // "packing" the string into bytes and 
        // then applying the hash function. 
        // This is then typecasted into uint.
        uint random = 
             uint(keccak256(abi.encodePacked(_str)));

        // Returning the generated hash value 
        return random % hashModulus;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;GeeksForGeeks&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/pfG7HmfeMz3Vf0dCCs9lMiXfm2D5Ogtsz9Mun2Rol5A/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3NnczYwaGx6ZGNt/NXRqejJpZ2NxLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/pfG7HmfeMz3Vf0dCCs9lMiXfm2D5Ogtsz9Mun2Rol5A/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3NnczYwaGx6ZGNt/NXRqejJpZ2NxLnBu/Zw" alt="Image description" width="300" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>hashing</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What are Enums and Structs in Solidity?</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Tue, 19 Jul 2022 08:25:51 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/what-are-enums-and-structs-in-solidity-36k8</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/what-are-enums-and-structs-in-solidity-36k8</guid>
      <description>&lt;p&gt;Enums are the way of creating user-defined data types, it is usually used to provide names for integral constants which makes the contract better for maintenance and reading. Enums restrict the variable with one of a few predefined values, these values of the enumerated list are called enums. Options are represented with integer values starting from zero, a default value can also be given for the enum. By using enums it is possible to reduce the bugs in the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;enum &amp;lt;enumerator_name&amp;gt; { &lt;br&gt;
            element 1, element 2,....,element n&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types consist of an enumerator week_days, and functions are defined to set and get the value of a variable of the type enumerator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// how to use 'enumerator'
pragma solidity ^0.5.0;

// Creating a contract
contract Types { 

    // Creating an enumerator
    enum week_days
    {
      Monday,
      Tuesday,
      Wednesday,
      Thursday,
      Friday,
      Saturday,
      Sunday
     } 

    // Declaring variables of
    // type enumerator
    week_days week;   

    week_days choice;

    // Setting a default value
    week_days constant default_value
      = week_days.Sunday;

    // Defining a function to
    // set value of choice
    function set_value() public {
      choice = week_days.Thursday;
    }

    // Defining a function to
    // return value of choice
    function get_choice(
    ) public view returns (week_days) {
      return choice;
    }

    // Defining function to
    //  return default value
    function getdefaultvalue(
    ) public pure returns(week_days) { 
        return default_value; 
    } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/1F9Pb4K2Omr61fsOjjAAGGzxAF85JIdTo7clJOb--pI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2N6b2ZhMHhhOWgy/N3BvN3YxNXFvLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/1F9Pb4K2Omr61fsOjjAAGGzxAF85JIdTo7clJOb--pI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2N6b2ZhMHhhOWgy/N3BvN3YxNXFvLnBu/Zw" alt="Image description" width="461" height="236"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Struct
&lt;/h3&gt;

&lt;p&gt;Structs in Solidity allows you to create more complicated data types that have multiple properties. You can define your own type by creating a struct.&lt;/p&gt;

&lt;p&gt;They are useful for grouping together related data.&lt;/p&gt;

&lt;p&gt;Structs can be declared outside of a contract and imported in another contract. Generally, it is used to represent a record. To define a structure struct keyword is used, which creates a new data type. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;struct &amp;lt;structure_name&amp;gt; {  &lt;br&gt;
   &amp;lt;data type&amp;gt; variable_1;  &lt;br&gt;
   &amp;lt;data type&amp;gt; variable_2; &lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
For accessing any element of the structure, ‘dot operator’ is used, which separates the struct variable and the element we wish to access. To define the variable of structure data type structure name is used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Test consists of a structure Book, and functions are defined to set and get values of the elements of the structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// how to use 'structures'
pragma solidity ^0.5.0;

// Creating a contract
contract test {

   // Declaring a structure
   struct Book {
      string name;
      string writter;
      uint id;
      bool available;
   }

   // Declaring a structure object
   Book book1;

   // Assigning values to the fields
   // for the structure object book2
   Book book2
     = Book("Building Ethereum DApps",
            "Roberto Infante ",
             2, false);

   // Defining a function to set values
   // for the fields for structure book1
   function set_book_detail() public {
      book1 = Book("Introducing Ethereum and Solidity",
                   "Chris Dannen",
                    1, true);
   }


   // Defining function to print
   // book2 details
   function book_info(
   )public view returns (
     string memory, string memory, uint, bool) { 

        return(book2.name, book2.writter,
               book2.id, book2.available); 
    }

   // Defining function to print
   // book1 details
   function get_details(
   ) public view returns (string memory, uint) {
      return (book1.name, book1.id);
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/G5wePWg7l0lT186J69AeLSKX2Dh1KE_te-TVGSCMf6c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3A5cHVnYW5wZzNo/eGxwN21od2JxLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/G5wePWg7l0lT186J69AeLSKX2Dh1KE_te-TVGSCMf6c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3A5cHVnYW5wZzNo/eGxwN21od2JxLnBu/Zw" alt="Image description" width="576" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>tutorial</category>
      <category>enums</category>
      <category>structs</category>
    </item>
    <item>
      <title>Learn Mapping in Solidity that acts like a hash table or dictionary in any other language.</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Sat, 16 Jul 2022 06:03:33 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-mapping-in-solidity-that-acts-like-a-hash-table-or-dictionary-in-any-other-language-1cgf</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-mapping-in-solidity-that-acts-like-a-hash-table-or-dictionary-in-any-other-language-1cgf</guid>
      <description>&lt;p&gt;Mapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types but reference types are not allowed while the value can be of any type. Mappings are mostly used to associate the unique Ethereum address with the associated value type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;mapping(key =&amp;gt; value) &amp;lt;access specifier&amp;gt; &amp;lt;name&amp;gt;;&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating a Mapping
&lt;/h3&gt;

&lt;p&gt;Mapping is defined as any other variable type, which accepts a key type and a value type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract mapping_example a structure is defined and mapping is created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to 
// demonstrate mapping
pragma solidity ^0.4.18; 

// Defining contract 
contract mapping_example {

    //Defining structure
    struct student 
    {
        // Declaring different 
        // structure elements
        string name;
        string subject;
        uint8 marks;
    }

    // Creating a mapping
    mapping (
    address =&amp;gt; student) result;
    address[] public student_result;    
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/1jrYM8oUuIoS1FDKCIzbihCZf-Bs50_0KyDQ1FxHbZY/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2p1a3c1YXp4ZXcw/ODBza3N3OXI2LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/1jrYM8oUuIoS1FDKCIzbihCZf-Bs50_0KyDQ1FxHbZY/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2p1a3c1YXp4ZXcw/ODBza3N3OXI2LnBu/Zw" alt="Image description" width="409" height="130"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Adding values to mapping
&lt;/h3&gt;

&lt;p&gt;As the mapping is created let’s try to add some values to the mapping for better understanding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract mapping_example defines a structure, mapping is created and values are added to the mapping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to 
// demonstrate adding 
// values to mapping
pragma solidity ^0.4.18; 

// Creating contract
contract mapping_example {

    //Defining structure 
    struct student {

        //Declaring different 
        // structure elements
        string name;
        string subject;
        uint8 marks;
    }

    // Creating mapping
    mapping (
      address =&amp;gt; student) result;
    address[] public student_result;

    // Function adding values to 
    // the mapping
    function adding_values() public {
        var student 
          = result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];

        student.name = "John";
        student.subject = "Chemistry";
        student.marks = 88;
        student_result.push(
          0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/tiJxVbixHPaE8eURWQLCHLWLlEiueKNEL3-HSrDzXCI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3d4ZHBtMHk0Z2Ft/Z2Nxa3VrcnZhLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/tiJxVbixHPaE8eURWQLCHLWLlEiueKNEL3-HSrDzXCI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3d4ZHBtMHk0Z2Ft/Z2Nxa3VrcnZhLnBu/Zw" alt="Image description" width="514" height="189"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Getting values
&lt;/h3&gt;

&lt;p&gt;We have added values to the mapping, to retrieve the values we have to create a function that returns the values added to the mapping.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract mapping_example defines a structure, mapping is created, values are added to the mapping, and values are retrieved from the mapping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to
// demonstrate retrieve
// values from the mapping

pragma solidity ^0.4.18; 

contract mapping_example {

    // Defining Structure
    struct student {

        // Declaring different data types
        string name;
        string subject;
        uint8 marks;
    }

    // Creating mapping
    mapping (
      address =&amp;gt; student) result;
    address[] student_result;

    // Function adding values to the mapping
    function adding_values() public {
        var student 
          = result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];

        student.name = "John";
        student.subject = "Chemistry";
        student.marks = 88;
        student_result.push(
          0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;

    }

     // Function to retrieve 
     // values from a mapping
     function get_student_result(
     ) view public returns (address[]) {
        return student_result;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/b5q4o6msiAr6Ei7DRnLv_fb1G2lWs4Y3oSHw33UTDgM/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3FoeHVxd3loMTdo/YTY0ZWlpMzNnLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/b5q4o6msiAr6Ei7DRnLv_fb1G2lWs4Y3oSHw33UTDgM/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3FoeHVxd3loMTdo/YTY0ZWlpMzNnLnBu/Zw" alt="Image description" width="519" height="288"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Counting Mappings
&lt;/h3&gt;

&lt;p&gt;Mappings can be counted so that we can know how many values are stored in mapping.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract mapping_example defines a structure, mapping is created, values are added to the mapping, and values are retrieved from the mapping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to
// count number of 
// values in a mapping
pragma solidity ^0.4.18; 

contract mapping_example {

    // Defining structure
    struct student {

        // Declaring different
        // structure elements
        string name;
        string subject;
        uint8 marks;
    }

    // Creating mapping
    mapping (address =&amp;gt; student) result;
    address[] student_result;

    //Function adding values to the mapping
    function adding_values() public {
        var student
          = result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];

        student.name = "John";
        student.subject = "Chemistry";
        student.marks = 88;
        student_result.push(
          0xDEE7796E89C82C36BAdd1375076f39D69FafE252) -1;

    }

     // Function to retrieve 
     // values from the mapping
     function get_student_result(
     ) view public returns (address[]) {
        return student_result;
    }

    // Function to count number 
    // of values in a mapping
    function count_students(
    ) view public returns (uint) {
        return student_result.length;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/JMK_imevql4viPd_1PVYhjZ1fvJmECKTWBi123LpxZg/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzhhdWZjYWV2cjZo/b2w0Zml6OXE4LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/JMK_imevql4viPd_1PVYhjZ1fvJmECKTWBi123LpxZg/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzhhdWZjYWV2cjZo/b2w0Zml6OXE4LnBu/Zw" alt="Image description" width="527" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mapping</category>
      <category>solidity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Learn about Arrays in Solidity.</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Wed, 13 Jul 2022 07:44:20 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-about-arrays-in-solidity-48e7</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-about-arrays-in-solidity-48e7</guid>
      <description>&lt;p&gt;Arrays are data structures that store the fixed collection of elements of the same data types in which each and every element has a specific location called index. Instead of creating numerous individual variables of the same type, we just declare one array of the required size and store the elements in the array and can be accessed using the index. In Solidity, an array can be of fixed size or dynamic size. Arrays have a continuous memory location, where the lowest index corresponds to the first element while the highest represents the last&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an Array
&lt;/h3&gt;

&lt;p&gt;To declare an array in Solidity, the data type of the elements and the number of elements should be specified. The size of the array must be a positive integer and data type should be a valid Solidity type&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&amp;lt;data type&amp;gt; &amp;lt;array name&amp;gt;[size] = &amp;lt;initialization&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Fixed-size Arrays
&lt;/h3&gt;

&lt;p&gt;The size of the array should be predefined. The total number of elements should not exceed the size of the array. If the size of the array is not specified then the array of enough size is created which is enough to hold the initialization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types are created to demonstrate how to declare and initialize fixed-size arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate 
// creating a fixed-size array 
pragma solidity ^0.5.0;  

// Creating a contract 
contract Types {  

    // Declaring state variables
    // of type array
    uint[6] data1;    

    // Defining function to add 
    // values to an array 
    function array_example() public returns (
    int[5] memory, uint[6] memory){  

        int[5] memory data 
        = [int(50), -63, 77, -28, 90];  
        data1 
        = [uint(10), 20, 30, 40, 50, 60];

        return (data, data1);  
  }  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/TwPP6VZpCibFmvgJ02bp_7tKR9G_hbrjf-jAbReOSkw/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2xianFkbjJwamxv/aDJqbXd0NzZ4LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/TwPP6VZpCibFmvgJ02bp_7tKR9G_hbrjf-jAbReOSkw/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2xianFkbjJwamxv/aDJqbXd0NzZ4LnBu/Zw" alt="Image description" width="305" height="72"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Dynamic Array:
&lt;/h3&gt;

&lt;p&gt;The size of the array is not predefined when it is declared. As the elements are added the size of array changes and at the runtime, the size of the array will be determined.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types are created to demonstrate how to create and initialize dynamic arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate 
// creating a dynamic array
pragma solidity ^0.5.0;  

// Creating a contract  
contract Types {  

    // Declaring state variable 
    // of type array. One is fixed-size
    // and the other is dynamic array
    uint[] data 
      = [10, 20, 30, 40, 50]; 
    int[] data1;  

    // Defining function to 
    // assign values to dynamic array
    function dynamic_array() public returns(
      uint[] memory, int[] memory){  

        data1 
          = [int(-60), 70, -80, 90, -100, -120, 140]; 
        return (data, data1);  
    }  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/JfUCFHDmT6GqAn0T7plm01nAwkYWbN9VBxIiQDD47S8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzFrdmlwcWNnbWJj/b3hiMWVzcmhnLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/JfUCFHDmT6GqAn0T7plm01nAwkYWbN9VBxIiQDD47S8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzFrdmlwcWNnbWJj/b3hiMWVzcmhnLnBu/Zw" alt="Image description" width="483" height="79"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Array Operations
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Accessing Array Elements:&lt;/strong&gt; The elements of the array are accessed by using the index. If you want to access ith element then you have to access (i-1)th index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types first initializes an array[data] and then retrieves the value at specific index 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// accessing elements of an array
pragma solidity ^0.5.0;  

// Creating a contract 
contract Types {  

    // Declaring an array
    uint[6] data;    

    // Defining function to 
    // assign values to array
    function array_example(
    ) public payable returns (uint[6] memory){  

        data 
          = [uint(10), 20, 30, 40, 50, 60];
        return data;  
  } 

  // Defining function to access
  // values from the array
  // from a specific index  
  function array_element(
  ) public payable returns (uint){  
        uint x = data[2];
        return x;  
  }  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/GKKyhOQrTSRiBA5hVtNTsX6mdRQk-LWWgVetMmmxEv0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2Q2aHFuMWRobmt5/ano0czA2eXZzLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/GKKyhOQrTSRiBA5hVtNTsX6mdRQk-LWWgVetMmmxEv0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2Q2aHFuMWRobmt5/ano0czA2eXZzLnBu/Zw" alt="Image description" width="323" height="62"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Length of Array:&lt;/strong&gt; Length of the array is used to check the number of elements present in an array. The size of the memory array is fixed when they are declared, while in case the dynamic array is defined at runtime so for manipulation length is required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types first initializes an array[data] and then the length of the array is calculated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate 
// how to find length of an array
pragma solidity ^0.5.0;  

// Creating a contract
contract Types {  

    // Declaring an array
    uint[6] data;    

    // Defining a function to 
    // assign values to an array
    function array_example(
    ) public payable returns (uint[6] memory){  
        data = [uint(10), 20, 30, 40, 50, 60];
        return data;  
  }  

  // Defining a function to 
  // find the length of the array
  function array_length(
  ) public returns(uint) {  
        uint x = data.length;
        return x; 
    } 
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/PcW9DEzkRqAyNJrheIiLQ7C_jHA4J9ZtlXRoEYv5Ses/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/Lzl0YWJnbjA4aGF6/aXE1bWpzYW5iLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/PcW9DEzkRqAyNJrheIiLQ7C_jHA4J9ZtlXRoEYv5Ses/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/Lzl0YWJnbjA4aGF6/aXE1bWpzYW5iLnBu/Zw" alt="Image description" width="288" height="67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Push:&lt;/strong&gt; Push is used when a new element is to be added in a dynamic array. The new element is always added at the last position of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types first initializes an array[data], and then more values are pushed into the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate 
// Push operation
pragma solidity ^0.5.0;  

// Creating a contract 
contract Types {  

    // Defining the array
    uint[] data = [10, 20, 30, 40, 50]; 

    // Defining the function to push 
    // values to the array
    function array_push(
    ) public returns(uint[] memory){  

        data.push(60);  
        data.push(70);  
        data.push(80);

        return data;  
    }  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Output : *&lt;/em&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/KU7Fbi9qpvqU1LStELgq0NggaqRKQJO5sxHxHRRl3AM/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/Lzl4bHIxZDExOXpq/azZ2dGhsaGZvLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/KU7Fbi9qpvqU1LStELgq0NggaqRKQJO5sxHxHRRl3AM/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/Lzl4bHIxZDExOXpq/azZ2dGhsaGZvLnBu/Zw" alt="Image description" width="391" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Pop:&lt;/strong&gt; Pop is used when the last element of the array is to be removed in any dynamic array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types first initializes an array[data], and then values are removed from the array using the pop function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// Pop operation
pragma solidity ^0.5.0;  

// Creating a contract
contract Types {  

    // Defining an array
    uint[] data 
      = [10, 20, 30, 40, 50];

    // Defining a function to 
    // pop values from the array
    function array_pop(
    ) public returns(uint[] memory){  
        data.pop(); 
        return data;  
    }  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/cx0ofT_uI0Sw_UJQ-HDZjza2kYv5TEKWMsHeHwnlc2o/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2ZiNnFmdWpwNXFx/NmY5dTM3cXptLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/cx0ofT_uI0Sw_UJQ-HDZjza2kYv5TEKWMsHeHwnlc2o/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2ZiNnFmdWpwNXFx/NmY5dTM3cXptLnBu/Zw" alt="Image description" width="355" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>array</category>
      <category>solidity</category>
      <category>xdc</category>
    </item>
    <item>
      <title>Today Let's Learn about Solidity – Operators.</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Mon, 11 Jul 2022 06:38:34 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/today-lets-learn-about-solidity-operators-1h09</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/today-lets-learn-about-solidity-operators-1h09</guid>
      <description>&lt;p&gt;In any programming language, operators play a vital role i.e. they create a foundation for the programming. Similarly, the functionality of Solidity is also incomplete without the use of operators. Operators allow users to perform different operations on operands. Solidity supports the following types of operators based upon their functionality.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Arithmetic Operators&lt;/li&gt;
&lt;li&gt;Relational Operators&lt;/li&gt;
&lt;li&gt;Logical Operators&lt;/li&gt;
&lt;li&gt;Bitwise Operators&lt;/li&gt;
&lt;li&gt;Assignment operators&lt;/li&gt;
&lt;li&gt;Conditional Operator&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Arithmetic Operators
&lt;/h3&gt;

&lt;p&gt;These operators are used to perform arithmetic or mathematical operations. Solidity supports the following arithmetic operators:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.xdc.dev/images/H_Kt8fLkYwbUPsAKXLFQGiv3AzYU4qJcnxxfkzKUVY0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3J3YmxzenV4Z3Uw/ZThiMXd6angxLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/H_Kt8fLkYwbUPsAKXLFQGiv3AzYU4qJcnxxfkzKUVY0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3J3YmxzenV4Z3Uw/ZThiMXd6angxLnBu/Zw" alt="Image description" width="880" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract SolidityTest demonstrates the above mentioned different types of arithmetic operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity contract to demonstrate
// Arithmetic Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

    // Initializing variables
    uint16 public a = 20;
    uint16 public b = 10;

    // Initializing a variable
    // with sum
    uint public sum = a + b;

    // Initializing a variable
    // with the difference
    uint public diff = a - b;

    // Initializing a variable
    // with product
    uint public mul = a * b;

    // Initializing a variable
    // with quotient
    uint public div = a / b;

    // Initializing a variable
    // with modulus
    uint public mod = a % b;

    // Initializing a variable
    // decrement value
    uint public dec = --b;

    // Initializing a variable
    // with increment value
    uint public inc = ++a;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/fkM9o2k0ypiRdhKjvHTrIn2Ow-OGVAur8yb2Bo_jdCY/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3RmNmI4dzZzZjY5/aWZlNWxjenpkLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/fkM9o2k0ypiRdhKjvHTrIn2Ow-OGVAur8yb2Bo_jdCY/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3RmNmI4dzZzZjY5/aWZlNWxjenpkLnBu/Zw" alt="Image description" width="459" height="232"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Relational Operators
&lt;/h3&gt;

&lt;p&gt;These operators are used to compare two values. Solidity supports the following relational operators:   &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.xdc.dev/images/qQyyAP9hKCDQVBM3-9e38oPS27QlsvkqhhQern_czvg/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2JhaDJ2czVmNG4z/b2UydDBtbm9rLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/qQyyAP9hKCDQVBM3-9e38oPS27QlsvkqhhQern_czvg/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2JhaDJ2czVmNG4z/b2UydDBtbm9rLnBu/Zw" alt="Image description" width="880" height="791"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the &lt;em&gt;contract SolidityTest&lt;/em&gt; demonstrates the above mentioned different types of relational operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// Relational Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

    // Declaring variables
    uint16 public a = 20;
    uint16 public b = 10;

    // Initializing a variable
    // with bool equal result
    bool public eq = a == b;

    // Initializing a variable
    // with bool not equal result
    bool public noteq = a != b;

    // Initializing a variable
    // with bool greater than result
    bool public gtr = a &amp;gt; b;

    // Initializing a variable
    // with bool less than result
    bool public les = a &amp;lt; b;

    // Initializing a variable
    // with bool greater than equal to result
    bool public gtreq = a &amp;gt;= b;

    // Initializing a variable
    // bool less than equal to result
    bool public leseq = a &amp;lt;= b;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/_nNMYahIKt5HUAXi-DoFvDtsBZgZu1AqEK8m8TmlSrU/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3JtNmV2MGUzbzhs/ZWZnams3Mnp2LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/_nNMYahIKt5HUAXi-DoFvDtsBZgZu1AqEK8m8TmlSrU/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3JtNmV2MGUzbzhs/ZWZnams3Mnp2LnBu/Zw" alt="Image description" width="476" height="297"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Logical Operators
&lt;/h3&gt;

&lt;p&gt;These operators are used to combine two or more conditions. Solidity supports the following arithmetic operators :  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.xdc.dev/images/t8jOwe6-8vrL5s6AHfaCaAwAWp9vLHw094A1VvXNjAE/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2I2anFpaXhhZW0x/b2dtd3FtOHZlLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/t8jOwe6-8vrL5s6AHfaCaAwAWp9vLHw094A1VvXNjAE/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2I2anFpaXhhZW0x/b2dtd3FtOHZlLnBu/Zw" alt="Image description" width="880" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract logicalOperator demonstrates the above mentioned different types of logical operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// Logical Operators
pragma solidity ^0.5.0;

// Creating a contract
contract logicalOperator{

     // Defining function to demonstrate
     // Logical operator
     function Logic(
       bool a, bool b) public view returns(
       bool, bool, bool){

       // Logical AND operator 
       bool and = a&amp;amp;&amp;amp;b;

       // Logical OR operator 
       bool or = a||b;

       // Logical NOT operator
       bool not = !a;
       return (and, or, not);
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/oQ2cAeqQCEuI8HzpnSr25CZSklDaq47IpogFlL8jAAU/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3h1MWJ6aGFjaW9w/aXV0ZWxwa2t5LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/oQ2cAeqQCEuI8HzpnSr25CZSklDaq47IpogFlL8jAAU/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3h1MWJ6aGFjaW9w/aXV0ZWxwa2t5LnBu/Zw" alt="Image description" width="706" height="260"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Bitwise Operators
&lt;/h3&gt;

&lt;p&gt;These operators work at a bit level used to perform bit-level operations. Solidity supports the following arithmetic operators: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.xdc.dev/images/MwQnA09GsB_dym4MjbSpcO2yBfG9enedZVngH8sKjS0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2kzemJrbDRzem9z/ZDZ4MDVqaXc1LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/MwQnA09GsB_dym4MjbSpcO2yBfG9enedZVngH8sKjS0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2kzemJrbDRzem9z/ZDZ4MDVqaXc1LnBu/Zw" alt="Image description" width="880" height="806"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract SolidityTest demonstrates the above mentioned different types of bitwise operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// Bitwise Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

    // Declaring variables
    uint16 public a = 20;
    uint16 public b = 10;

    // Initializing a variable
    // to '&amp;amp;' value
    uint16 public and = a &amp;amp; b;

    // Initializing a variable
    // to '|' value
    uint16 public or = a | b;

    // Initializing a variable
    // to '^' value
    uint16 public xor = a ^ b;

    // Initializing a variable
    // to '&amp;lt;&amp;lt;' value
    uint16 public leftshift = a &amp;lt;&amp;lt; b;

    // Initializing a variable
    // to '&amp;gt;&amp;gt;' value
    uint16 public rightshift = a &amp;gt;&amp;gt; b;

    // Initializing a variable
    // to '~' value
    uint16 public not = ~a ;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/FJWp1mRAHBtGppUoftNnQlfdRCzxm7UKjAlwRaU5gdY/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2VscGUwZmgwdW9w/dmI5cGpqMjVjLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/FJWp1mRAHBtGppUoftNnQlfdRCzxm7UKjAlwRaU5gdY/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2VscGUwZmgwdW9w/dmI5cGpqMjVjLnBu/Zw" alt="Image description" width="458" height="299"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Assignment Operator
&lt;/h3&gt;

&lt;p&gt;These operators are for the assignment of value to a variable. The operand at the left side is variable while operand at the right side is value. Solidity supports the following arithmetic operators :  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.xdc.dev/images/PDC035Lt7-A0MB6nHZUP4_Vunv31M1aOW-UNZBgLf5E/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L281cmpwaGJvNGJv/OWc5bXQxZHBkLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/PDC035Lt7-A0MB6nHZUP4_Vunv31M1aOW-UNZBgLf5E/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L281cmpwaGJvNGJv/OWc5bXQxZHBkLnBu/Zw" alt="Image description" width="880" height="908"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract SolidityTest demonstrates the above mentioned different types of assignment operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// Assignment Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest {

        // Declaring variables
        uint16 public assignment = 20;
        uint public assignment_add = 50;
        uint public assign_sub = 50;
        uint public assign_mul = 10;
        uint public assign_div = 50;
        uint public assign_mod = 32;

        // Defining function to
        // demonstrate Assignment Operator
        function getResult() public{
           assignment_add += 10;
           assign_sub -= 20;
           assign_mul *= 10;
           assign_div /= 10;
           assign_mod %= 20;
           return ;
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/caLoDXFaN8jlIkjeuE8DCSQMtBLizI40mfUp7aN7BAQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2JjZW1vcWo3bmJx/cjN2azA2dGc2LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/caLoDXFaN8jlIkjeuE8DCSQMtBLizI40mfUp7aN7BAQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2JjZW1vcWo3bmJx/cjN2azA2dGc2LnBu/Zw" alt="Image description" width="468" height="197"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Conditional Operators
&lt;/h3&gt;

&lt;p&gt;It is a ternary operator that evaluates the expression first then checks the condition for return values corresponding to true or false. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;if condition true ? then A: else B&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract SolidityTest demonstrates the conditional operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// Conditional Operator
pragma solidity ^0.5.0;

// Creating a contract
contract SolidityTest{

     // Defining function to demonstrate
     // conditional operator
     function sub(
       uint a, uint b) public view returns(
       uint){
      uint result = (a &amp;gt; b? a-b : b-a);
      return result;
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/fUNN1MbHDrtO4Sctu7UYGB78ALh5GdQNgq_RkcQOMaQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2lscWcxbTJtcHBj/YzZsenkwdWp6LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/fUNN1MbHDrtO4Sctu7UYGB78ALh5GdQNgq_RkcQOMaQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2lscWcxbTJtcHBj/YzZsenkwdWp6LnBu/Zw" alt="Image description" width="518" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>operators</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Let's Learn Solidity - Variables.</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Thu, 07 Jul 2022 12:57:30 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/lets-learn-solidity-variables-3and</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/lets-learn-solidity-variables-3and</guid>
      <description>&lt;p&gt;A Variable is basically a placeholder for the data which can be manipulated at runtime. Variables allow users to retrieve and change the stored information. &lt;/p&gt;

&lt;h3&gt;
  
  
  Rules For Naming Variables
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A variable name should not match with reserved keywords.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variable names must start with a letter or an underscore (_), and may contain letters from “a to z” or “A to Z” or digits from “0 to 9” and characters also.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Geeks123, geeks, _123geeks are valid variable names &lt;br&gt;
123geeks, $Geeks, 12_geeks are invalid variable names&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The name of variables are case sensitive i.e.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Geeks123 and geeks123 are different variables&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Declaration of Variables
&lt;/h3&gt;

&lt;p&gt;In Solidity declaration of variables is a little bit different, to declare a variable the user has to specify the data type first followed by access modifier. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;type&amp;gt; &amp;lt;access modifier&amp;gt; &amp;lt;variable name&amp;gt; ;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int public int_var;&lt;/code&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  Types of Variables
&lt;/h3&gt;

&lt;p&gt;Solidity is a statically typed language i.e. each declared variable always has a default value based on its data type, which means there is no concept of ‘null’ or ‘undefined’. Solidity supports three types of variables: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. State Variables:&lt;/strong&gt; Values of these variables are permanently stored in the contract storage. Each function has its own scope, and state variables should always be defined outside of that scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example. the &lt;code&gt;contract Solidity_var_Test&lt;/code&gt; initializes the values of an unsigned integer state variable using a constructor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solidity&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to 
// demonstrate state 
// variables
pragma solidity ^0.5.0;

// Creating a contract
contract Solidity_var_Test {

   // Declaring a state variable
   uint8 public state_var;      

   // Defining a constructor
   constructor() public {
      state_var = 16;   
   }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/3k6oZk_-WLNFqXvs-UZne1H8JRzeiJBi8aUK5HDiEcg/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2s1cXVya2ZyOW9r/MWcwbWl5M2NuLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/3k6oZk_-WLNFqXvs-UZne1H8JRzeiJBi8aUK5HDiEcg/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2s1cXVya2ZyOW9r/MWcwbWl5M2NuLnBu/Zw" alt="output" width="345" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Local Variable:&lt;/strong&gt; Values of these variables are present till the function executes and it cannot be accessed outside that function. This type of variable is usually used to store temporary values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Solidity_var_Test defines a function to declare and initialize the local variables and return the sum of the two local variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solidity&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// local variables
pragma solidity ^0.5.0;

// Creating a contract
contract Solidity_var_Test {

   // Defining function to show the declaration and
   // scope of local variables
   function getResult() public view returns(uint){

      // Initializing local variables
      uint local_var1 = 1; 
      uint local_var2 = 2;
      uint result = local_var1 + local_var2;

      // Access the local variable
      return result; 
   }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/7tFe7-9uRkdOKvkhyG29Z49Bn4ZLPOUSUbJA9CIY5NQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2w3eWVoZ3gwbG51/MG43bTRvMWtoLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/7tFe7-9uRkdOKvkhyG29Z49Bn4ZLPOUSUbJA9CIY5NQ/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2w3eWVoZ3gwbG51/MG43bTRvMWtoLnBu/Zw" alt="Image description" width="373" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Global Variables:&lt;/strong&gt; These are some special variables that can be used globally and give information about the transactions and blockChain properties. Some of the global variables are listed below :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.xdc.dev/images/WXJ8QvJ7V9h_IGn5G4pKdo3rIikg06jO-7jSbITTE-8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2xoZDIwc2hkdDho/OTB0cDd1aTUzLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/WXJ8QvJ7V9h_IGn5G4pKdo3rIikg06jO-7jSbITTE-8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2xoZDIwc2hkdDho/OTB0cDd1aTUzLnBu/Zw" alt="Image description" width="880" height="1463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contact Test uses the msg.sender variable to access the address of the person deploying the contract.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solidity&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to 
// show Global variables
pragma solidity ^0.5.0;

// Creating a contract
contract Test { 

  // Defining a variable
  address public admin;

  // Creating a constructor to
  // use Global variable
  constructor() public {    
    admin = msg.sender;  
  }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/kOw5YbkgILvnh-VReKzk-ISkxdal88YWklOZ-ZXi3Oc/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L25uNnR5NTZvd3U2/MGFjaDkyYmZ6LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/kOw5YbkgILvnh-VReKzk-ISkxdal88YWklOZ-ZXi3Oc/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L25uNnR5NTZvd3U2/MGFjaDkyYmZ6LnBu/Zw" alt="Image description" width="300" height="142"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>solidity</category>
      <category>variables</category>
    </item>
    <item>
      <title>Learn Solidity – While, Do-While, and For Loop.</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Mon, 04 Jul 2022 09:57:56 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-solidity-while-do-while-and-for-loop-53h</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-solidity-while-do-while-and-for-loop-53h</guid>
      <description>&lt;p&gt;Loops are used when we have to perform an action over and over again. While writing a contract there may be a situation when we have to do some action repeatedly, In this situation, loops are implemented to reduce the number of lines of the statements. Solidity supports following loops too ease down the programming pressure.&lt;/p&gt;

&lt;h3&gt;
  
  
  While Loop
&lt;/h3&gt;

&lt;p&gt;This is the most basic loop in solidity, Its purpose is to execute a statement or block of statements repeatedly as far as the condition is true and once the condition becomes false the loop terminates. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;while (condition) {&lt;br&gt;
    statement or block of code to be executed if the condition is True&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types demonstrate the execution of a while loop and how an array can be initialized using the while loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to 
// demonstrate the use
// of 'While loop'
pragma solidity ^0.5.0; 

// Creating a contract 
contract Types { 

    // Declaring a dynamic array
    uint[] data; 

    // Declaring state variable
    uint8 j = 0;

    // Defining a function to 
    // demonstrate While loop'
    function loop(
    ) public returns(uint[] memory){
    while(j &amp;lt; 5) {
        j++;
        data.push(j);
     }
      return data;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/2uDjA3AqzsI6xKg51CG-O4cQ8F8kl8BnHNmqhwQiHIE/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3Rpb3VxNWM3Yndr/OXkweDk3bHZpLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/2uDjA3AqzsI6xKg51CG-O4cQ8F8kl8BnHNmqhwQiHIE/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3Rpb3VxNWM3Yndr/OXkweDk3bHZpLnBu/Zw" alt="output" width="542" height="254"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Do-While Loop
&lt;/h3&gt;

&lt;p&gt;This loop is very similar to while loop except that there is a condition check which happens at the end of loop i.e. the loop will always execute at least one time even if the condition is false. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;do &lt;br&gt;
{&lt;br&gt;
   block of statements to be executed&lt;br&gt;
} while (condition);&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types demonstrate the execution of a do-while loop and how an array can be initialized using the do-while loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to 
// demonstrate the use of
// 'Do-While loop'
pragma solidity ^0.5.0; 

// Creating a contract 
contract Types { 

    // Declaring a dynamic array
    uint[] data; 

    // Declaring state variable
    uint8 j = 0;

    // Defining function to demonstrate 
    // 'Do-While loop'
    function loop(
    ) public returns(uint[] memory){
    do{
        j++;
        data.push(j);
     }while(j &amp;lt; 5) ;
      return data;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/gHDYWFwMkoA1MPtDuEbl5fZCKhIy4pqjUJf3nx_c3_c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L245bGVyeXVjdXhn/YThyZm9uMG93LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/gHDYWFwMkoA1MPtDuEbl5fZCKhIy4pqjUJf3nx_c3_c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L245bGVyeXVjdXhn/YThyZm9uMG93LnBu/Zw" alt="Image description" width="541" height="235"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  For Loop
&lt;/h3&gt;

&lt;p&gt;This is the most compact way of looping. It takes three arguments separated by a semi-colon to run. The first one is ‘loop initialization’ where the iterator is initialized with starting value, this statement is executed before the loop starts. Second is ‘test statement’ which checks whether the condition is true or not, if the condition is true the loop executes else terminates. The third one is the ‘iteration statement’ where the iterator is increased or decreased. Below is the syntax of for loop :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;for (initialization; test condition; iteration statement) {&lt;br&gt;
       statement or block of code to be executed if the condition is True&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, the contract Types demonstrate the execution of a while loop and how an array can be initialized using the while loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to 
// demonstrate the use
// of 'For loop'
pragma solidity ^0.5.0; 

// Creating a contract
contract Types { 

    // Declaring a dynamic array 
    uint[] data; 

    // Defining a function 
    // to demonstrate 'For loop'
    function loop(
    ) public returns(uint[] memory){
    for(uint i=0; i&amp;lt;5; i++){
        data.push(i);
     }
      return data;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/m3zFKq8_RpgCguwVr5QAgX1Q5dWMshtxY-6s8ElpI5s/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2txZXJxYnYxejF6/NDRnNWViczdvLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/m3zFKq8_RpgCguwVr5QAgX1Q5dWMshtxY-6s8ElpI5s/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2txZXJxYnYxejF6/NDRnNWViczdvLnBu/Zw" alt="Image description" width="547" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>web3</category>
      <category>loop</category>
    </item>
    <item>
      <title>Learn About Solidity – Constructors with Examples.</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Sat, 02 Jul 2022 05:19:34 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-about-solidity-constructors-with-examples-e4g</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/learn-about-solidity-constructors-with-examples-e4g</guid>
      <description>&lt;p&gt;A constructor is a special method in any object-oriented programming language which gets called whenever an object of a class is initialized. It is totally different in case of Solidity, Solidity provides a constructor declaration inside the smart contract and it invokes only once when the contract is deployed and is used to initialize the contract state. A default constructor is created by the compiler if there is no explicitly defined constructor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a constructor
&lt;/h3&gt;

&lt;p&gt;A Constructor is defined using a constructor keyword without any function name followed by an access modifier. It’s an optional function which initializes state variables of the contract. A constructor can be either internal or public, an internal constructor marks contract as abstract. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
constructor() &amp;lt;Access Modifier&amp;gt; {          &lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; In the below example, in the contract constructorExample, a constructor is created to initialize the state variable str.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Solidity program to demonstrate
// creating a constructor
pragma solidity ^0.5.0;       

// Creating a contract
contract constructorExample {       

    // Declaring state variable
    string str;       

    // Creating a constructor
    // to set value of 'str'
    constructor() public {                 
        str = "GeeksForGeeks";       
    }       

    // Defining function to
    // return the value of 'str' 
    function getValue(
    ) public view returns (
      string memory) {       
        return str;       
    }     
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/4-VHg1v3aA6MQkzdbp9Y-s5zPOnSPaueclhw5Z8bC5o/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3ZhbnVrNmU2NXd1/bnB5ZGU1cXo3LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/4-VHg1v3aA6MQkzdbp9Y-s5zPOnSPaueclhw5Z8bC5o/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3ZhbnVrNmU2NXd1/bnB5ZGU1cXo3LnBu/Zw" alt="Sample Output" width="394" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constructor in Inheritance&lt;/strong&gt;&lt;br&gt;
In case if a constructor is not defined then the default constructor is called, but if the constructor is defined in a parent contract and having some arguments then the child contract should also provide the required parameters to the constructor. If the child contract is not passing any parameter to the parent’s constructor the child contract will become an abstract contract. There are two ways of calling a parent contract’s constructor: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Direct Initialization:&lt;/strong&gt; In the below example, the direct initialization method is used to initialize the constructor of the parent class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to demonstrate
// Constructor in Inheritance
pragma solidity ^0.5.0;

// Creating a contract
contract Base {

   // Declaring variable
   uint data;

   // Defining a constructor
   constructor(uint _data) public {
      data = _data;  
   }

   // Defining function
   function Print(
   ) public  returns(string memory){
       return "Direct Initialization";
   }

}

// Child contract inheriting
// the parent contract 'Base'
contract Derived is Base(2){

    // Defining a constructor
    constructor() public {}

   // Defining function to access
   // variable of parent contract
   function getData(
   ) external returns(uint){
       uint result = data ** 2;
       return result;
   }
}

// Caller contract
contract caller{

   // Creating an object of child contract
    Derived c = new Derived();

   // Accessing functions of parent
   // and child contract using
   // object of child contract
    function getResult() public returns(uint){
        c.Print();
        return c.getData();
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/0vZFczFFaU9-jzivmcmiUCWI6tuzpIpk2scVMvS3bZI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzJkcHdxMWxqaTlp/ZnZrM3NxbnQ3LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/0vZFczFFaU9-jzivmcmiUCWI6tuzpIpk2scVMvS3bZI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzJkcHdxMWxqaTlp/ZnZrM3NxbnQ3LnBu/Zw" alt="Image description" width="470" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Indirect Initialization:&lt;/strong&gt; In the below example, the indirect initialization using Base(string(abi.encodePacked(_info, _info))) is done to initialize the constructor of the base class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Solidity program to demonstrate
// Indirect Initialization
pragma solidity ^0.5.0;

// Creating a contract
contract Base {

   // Declaring state variable
   string str;

   // Defining a constructor
   constructor(
     string memory _str) public {
      str = _str;  
   }

   // Defining a function
   function Print(
   ) public returns(string memory){
       return "Indirect Initialization";
   }
}

// Child contract inheriting
// parent contract 'Base'
contract Derived is Base {

   // Defining a constructor
   constructor(
     string memory _info) Base(
     string(abi.encodePacked(
       _info, _info))) public {}

   // Defining function to
   // return value of parent
   // contract variable 'str'
   function getStr(
   ) public view returns(string memory){
       return str;
   }
}

// Caller contract
contract caller {

    // Creating an object of
    // child contract
    Derived c
      = new Derived("GeeksForGeeks");

    //Defining a function to access
    // functions of the parent
    //contract and child contract
    function getResult() public view{
        c.Print();
        c.getStr();
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/UQmerZTNnPWz_gFoPqmLQU6WxcpNuut7QkRyxt2q7y8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3UzaDY2MnFyNGh4/dWZsZ2s0ZndwLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/UQmerZTNnPWz_gFoPqmLQU6WxcpNuut7QkRyxt2q7y8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3UzaDY2MnFyNGh4/dWZsZ2s0ZndwLnBu/Zw" alt="Image description" width="454" height="365"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Need of Constructors
&lt;/h3&gt;

&lt;p&gt;Constructors are very useful in a smart contract, a parameter value can be defined at the run time and can also restrict the method call. Constructor overloading is not supported in Solidity, it only allows one constructor at a time.&lt;/p&gt;

&lt;p&gt;Example: In the below example, the contract constructorExample consists of a constructor to demonstrate the need for the constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Solidity program to demonstrate
// Need of constructors
pragma solidity ^0.5.0;       

// Creating a contract
contract constructorExample {       

    // Declaring state variable
    string str;       
    address private owner
      = 0x62Ab98A0efB752C48bd82836D0b4335018B9B97e;

    // Defining constructor
    constructor(string memory string) public {                 
        if(msg.sender == owner){   
            str = string;   
        }       
    }       

    // Defining function to
    // return value of 'str'  
    function getValue() public view returns (
      string memory) {       
        return str;       
    }     
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output :&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/Vyf0ATbYSoDaKVToOS-JgX8MrcYsJf4GT8eMWU7YqR0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2o4Zmc1bTJndjd6/bjQwNjM4b3U0LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/Vyf0ATbYSoDaKVToOS-JgX8MrcYsJf4GT8eMWU7YqR0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2o4Zmc1bTJndjd6/bjQwNjM4b3U0LnBu/Zw" alt="Image description" width="549" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>web3</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Steps to Execute Solidity Smart Contract using Remix IDE</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Thu, 30 Jun 2022 05:48:59 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/steps-to-execute-solidity-smart-contract-using-remix-ide-2i2h</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/steps-to-execute-solidity-smart-contract-using-remix-ide-2i2h</guid>
      <description>&lt;p&gt;Remix IDE is generally used to compile and run Solidity smart contracts. Below are the steps for the compilation, execution, and debugging of the smart contract. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Open Remix IDE on any of your browsers, select on the New File and click on Solidity to choose the environment. &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/E-opqfb7X-LFiL5xpL8__sgQDS_6SGrUoH0rkcT4ID0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2p4dDczNW42cDhp/bjJwNWdqa3RvLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/E-opqfb7X-LFiL5xpL8__sgQDS_6SGrUoH0rkcT4ID0/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2p4dDczNW42cDhp/bjJwNWdqa3RvLnBu/Zw" alt="remix" width="880" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Write the Smart contract in the code section, and click the Compile button under the Compiler window to compile the contract. &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/uOnoObORqSwKhjfU4D2uxeosHNax0Ze-lI7UKJ4etzM/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2s3Z21wMHJxbnZt/bTh6cnk0aWcwLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/uOnoObORqSwKhjfU4D2uxeosHNax0Ze-lI7UKJ4etzM/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2s3Z21wMHJxbnZt/bTh6cnk0aWcwLnBu/Zw" alt="Image description" width="880" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; To execute the code, click on the Deploy button under Deploy and Run Transactions window. &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/Of9T6W8p_U3C0OghHKnV7szIDTLOmhimD1fONz_QMMA/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2Vxamx2aXNqc3Vl/dHRzZzA3a2MxLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/Of9T6W8p_U3C0OghHKnV7szIDTLOmhimD1fONz_QMMA/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2Vxamx2aXNqc3Vl/dHRzZzA3a2MxLnBu/Zw" alt="Image description" width="880" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; After deploying the code click on the method calls under the drop-down of deployed contracts to run the program, and for output, check to click on the drop-down on the console.&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/JpRGQcqT66xk4X9r71AGdgrTsVdtOLW8nFkLrtBuNdA/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3g5bHFnZXk2dW1n/YmFlY3NsZ3c4LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/JpRGQcqT66xk4X9r71AGdgrTsVdtOLW8nFkLrtBuNdA/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3g5bHFnZXk2dW1n/YmFlY3NsZ3c4LnBu/Zw" alt="Image description" width="880" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; For debugging click on the Debug button corresponding to the method call in the console. Here you can check each function call and variable assignments.&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/QXS7fubvLTudT2gX6m2U6UtTAxGRAP_0Dph_vRPZjEI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2FrbXdsd2FpNWUw/cW8xcjJyMWczLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/QXS7fubvLTudT2gX6m2U6UtTAxGRAP_0Dph_vRPZjEI/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L2FrbXdsd2FpNWUw/cW8xcjJyMWczLnBu/Zw" alt="Image description" width="880" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>smartcontract</category>
      <category>solidity</category>
      <category>remix</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>An Introduction to Solidity with an example. | What is Solidity?</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Tue, 28 Jun 2022 08:29:54 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/an-introduction-to-solidity-with-an-example-what-is-solidity-1a0a</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/an-introduction-to-solidity-with-an-example-what-is-solidity-1a0a</guid>
      <description>&lt;p&gt;Solidity is a brand-new programming language created by the Ethereum which is the second-largest market of cryptocurrency by capitalization, released in the year 2015 led by Christian Reitwiessner. &lt;/p&gt;

&lt;p&gt;Some key features of solidity are listed below: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Solidity is a high-level programming language designed for implementing smart contracts.&lt;/li&gt;
&lt;li&gt;It is statically-typed object-oriented(contract-oriented) language.&lt;/li&gt;
&lt;li&gt;Solidity is highly influenced by Python, c++, and JavaScript which runs on the Ethereum Virtual Machine(EVM).&lt;/li&gt;
&lt;li&gt;Solidity supports complex user-defined programming, libraries and inheritance.&lt;/li&gt;
&lt;li&gt;Solidity is primary language for blockchains running platforms.&lt;/li&gt;
&lt;li&gt;Solidity can be used to creating contracts like voting, blind auctions, crowdfunding, multi-signature wallets, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Ethereum Virtual Machine(EVM)
&lt;/h3&gt;

&lt;p&gt;Ethereum Virtual Machine abbreviated as EVM is a runtime environment for executing smart contracts in ethereum and EVM compatible networks. It focuses widely on providing security and execution of untrusted code using an international network of public nodes. EVM is specialized to prevent Denial-of-service attack and confirms that the program does not have any access to each other’s state, also ensures that the communication is established without any potential interference. &lt;/p&gt;

&lt;h3&gt;
  
  
  Smart Contract
&lt;/h3&gt;

&lt;p&gt;Smart contracts are high-level program codes that are compiled to EVM byte code and deployed to the ethereum blockchain for further execution. It allows us to perform credible transactions without any interference of the third party, these transactions are trackable and irreversible. Languages used to write smart contracts are Solidity (a language library with similarities to C and JavaScript), Serpent (similar to Python, but deprecated), LLL (a low-level Lisp-like language), and Mutan (Go-based, but deprecated).&lt;/p&gt;

&lt;p&gt;Example: In the below example, we have discussed a sample solidity program to demonstrate how to write a smart contract in Solidity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Solidity program to
// demonstrate how to
// write a smart contract
pragma solidity &amp;gt;= 0.4.16 &amp;lt; 0.7.0;

// Defining a contract
contract Test
{

    // Declaring state variables
    uint public var1;
    uint public var2;
    uint public sum;

    // Defining public function
    // that sets the value of
    // the state variable
    function set(uint x, uint y) public
    {
        var1 = x;
        var2=y;
        sum=var1+var2;
    }

    // Defining function to
    // print the sum of
    // state variables
    function get(
    ) public view returns (uint) {
        return sum;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/c2Op4xCvcIp5_-CedssDUqma6Gr0qqTO8COxianhqwE/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L201b2V1dHRhNXR5/Y2dldXNoZjB3LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/c2Op4xCvcIp5_-CedssDUqma6Gr0qqTO8COxianhqwE/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L201b2V1dHRhNXR5/Y2dldXNoZjB3LnBu/Zw" alt="Image description" width="164" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Version Pragma:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;pragma solidity &amp;gt;=0.4.16 &amp;lt;0.7.0;&lt;/code&gt;&lt;br&gt;
Pragmas are instructions to the compiler on how to treat the code. All solidity source code should start with a “version pragma” which is a declaration of the version of the solidity compiler this code should use. This helps the code from being incompatible with the future versions of the compiler which may bring changes. The above-mentioned code states that it is compatible with compilers of version greater than and equal to 0.4.16 but less than version 0.7.0.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The contract keyword:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;contract Test{ &lt;br&gt;
//Functions and Data &lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
The contract keyword declares a contract under which is the code encapsulated.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. State variables:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;uint public var1;&lt;br&gt;
uint public var2;&lt;br&gt;
uint public sum;&lt;/code&gt;&lt;br&gt;
State variables are permanently stored in contract storage that they are written in Ethereum Blockchain. The line uint public var1 declares a state variable called var1 of type uint (unsigned integer of 256 bits). Think of it as adding a slot in a database. Similarly, goes with the declaration uint public var2 and uint public sum.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. A function declaration:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;function set(uint x, uint y) public&lt;br&gt;
function get() public view returns (uint)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a function named set of access modifier type public which takes a variable x and variable y of datatype uint as a parameter.&lt;/li&gt;
&lt;li&gt;This was an example of a simple smart contract which updates the value of var1 and var2. Anyone can call the function set and overwrite the value of var1 and var2 which is stored in Ethereum blockchain. This is an example of a decentralized application that is censorship proof and unaffected to the shutdown of any centralized server. As long as someone is running a single node of Ethereum blockchain, this smart contract will be accessible.&lt;/li&gt;
&lt;li&gt;The variable sum is calculated by adding the values of the variables var1 and var2.&lt;/li&gt;
&lt;li&gt;Function get will retrieve and print the value of the state variable sum.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to Execute The Code:
&lt;/h3&gt;

&lt;p&gt;There are practically two ways to execute a solidity smart contract:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Offline Mode: Running a solidity smart contract in Offline mode requires three prerequisites and 4 major steps to be followed to get the smart contract running:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Prerequisites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download and install node.js.&lt;/li&gt;
&lt;li&gt;Install &lt;a href="https://trufflesuite.com/truffle/"&gt;Truffle&lt;/a&gt; globally.&lt;/li&gt;
&lt;li&gt;Install &lt;a href="https://trufflesuite.com/ganache/"&gt;ganache-cli&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Objectives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a truffle project and configure a development network&lt;/li&gt;
&lt;li&gt;Create and deploy smart contracts&lt;/li&gt;
&lt;li&gt;Interact with the smart contract from Truffle console&lt;/li&gt;
&lt;li&gt;Write tests for testing main features offered by Solidity&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Online Mode: Remix IDE is generally used to compile and run Solidity smart contracts in the Online Mode.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>solidity</category>
      <category>evm</category>
      <category>web3</category>
    </item>
    <item>
      <title>How to Install Solidity in Windows?</title>
      <dc:creator>daniel weber</dc:creator>
      <pubDate>Mon, 27 Jun 2022 11:24:39 +0000</pubDate>
      <link>https://www.xdc.dev/daniel_weber_eaba2f02503a/how-to-install-solidity-in-windows-2kdn</link>
      <guid>https://www.xdc.dev/daniel_weber_eaba2f02503a/how-to-install-solidity-in-windows-2kdn</guid>
      <description>&lt;p&gt;To install solidity on windows ensure that you are using windows 10, as only windows 10 provides built-in Linux Subsystem. With the help of this feature, we can run the Ubuntu terminal on the Windows machine. &lt;/p&gt;

&lt;h3&gt;
  
  
  Below are the steps to setup Solidity on windows:
&lt;/h3&gt;

&lt;p&gt;Step 1: Open control panel on your system and toggle to Windows Subsystem for Linux under Control Panel &amp;gt; Programs &amp;gt; Programs and Features &amp;gt;Turn Windows features on or off.&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/oE4NuVLD1I0iXFi3XpVjGa9ZVWPW3cIC4zCEiM7Pd6c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzdxN3dlMzhscm93/dTQ4czU2eG4wLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/oE4NuVLD1I0iXFi3XpVjGa9ZVWPW3cIC4zCEiM7Pd6c/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzdxN3dlMzhscm93/dTQ4czU2eG4wLnBu/Zw" alt="windows feature" width="606" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: After your system restarts install “Ubuntu LTS 18.04” from Microsoft store and after installation set it up.&lt;/p&gt;

&lt;p&gt;Step 3: After setting up Bash install and check necessary dependencies like cURL, Node version manager(NVM), NodeJS and Node Packet Manager(NPM) using the following commands:&lt;/p&gt;

&lt;p&gt;Installing cURL :&lt;br&gt;
&lt;code&gt;sudo apt-get install curl&lt;/code&gt;&lt;br&gt;
&lt;code&gt;curl --version&lt;/code&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/vAI0ezRJu-QvJ4zK_4U7Em97l9IgCvpuBPhjD1TsoHo/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzMxdzUzZWI2OGVh/c2lqZzd5bGQ1LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/vAI0ezRJu-QvJ4zK_4U7Em97l9IgCvpuBPhjD1TsoHo/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzMxdzUzZWI2OGVh/c2lqZzd5bGQ1LnBu/Zw" alt="command promt" width="880" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installing NVM:&lt;br&gt;
&lt;code&gt;curl -o-https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash&lt;/code&gt;&lt;br&gt;
&lt;code&gt;nvm --version&lt;/code&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/IEknZqp1Mm92wo79wFBBXvYUPAtJ8MX8ecmwaPJvZc8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L21xbmJ5MmR3OXFr/ZXFwbmN3c2NqLnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/IEknZqp1Mm92wo79wFBBXvYUPAtJ8MX8ecmwaPJvZc8/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L21xbmJ5MmR3OXFr/ZXFwbmN3c2NqLnBu/Zw" alt="Image description" width="880" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installing NodeJS and NPM:&lt;br&gt;
&lt;code&gt;nvm install node&lt;/code&gt;&lt;br&gt;
&lt;code&gt;node --version&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm --version&lt;/code&gt;&lt;br&gt;
&lt;a href="https://www.xdc.dev/images/uRenRTbVHYitCqetduqREiX3_sy_62snaeNrgrJJjow/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzBwbDFpMHVraG1y/MzVkZDN2Y3g0LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/uRenRTbVHYitCqetduqREiX3_sy_62snaeNrgrJJjow/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/LzBwbDFpMHVraG1y/MzVkZDN2Y3g0LnBu/Zw" alt="Image description" width="880" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After installing the dependencies, install and check solidity compiler solc by using following code:&lt;br&gt;
&lt;code&gt;npm install -g solc&lt;/code&gt;&lt;br&gt;
&lt;code&gt;solcjs --version&lt;/code&gt; &lt;br&gt;
&lt;a href="https://www.xdc.dev/images/nRmaimo_sjpcP7Ac4-n9MNBYLpjw3ev8SygIfcSDlvU/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3NuMmd0OXN5b2k1/aWlkenUwZnB3LnBu/Zw" class="article-body-image-wrapper"&gt;&lt;img src="https://www.xdc.dev/images/nRmaimo_sjpcP7Ac4-n9MNBYLpjw3ev8SygIfcSDlvU/w:880/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL2FydGljbGVz/L3NuMmd0OXN5b2k1/aWlkenUwZnB3LnBu/Zw" alt="Image description" width="880" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>windows</category>
      <category>installation</category>
    </item>
  </channel>
</rss>
