Data Structure and Control Structure in Solidity

Reading Time: 3 minutes

Hello Reader, Do you know what are data structures? If not then here is what data structures mean.

data structure is a particular way of organising data in a computer memory so that it can be used effectively.

Solidity provides three types of data structures:

  1. Structs
  2. Arrays
  3. Mappings
Solidity Data Structures
Solidity Data Structures

One by one let us look at each data structure.

Structs:

Solidity provides a way to define new types in the form of structs. A struct is a special data type that allows the programmer to group a list of variables.

pragma solidity ^0.4.0;
contract Ballot {
struct Voter { // Struct
uint weight1, weight2, weight3;
bool voted;
address delegate1, delegate2, delegate3, delegate4;
string name;
uint vote1, vote2, vote3, vote4, vote5;
uint height1, height2, height3   } }

Structs allow you to create more complicated data types that have multiple properties.

Structs can only have 16 members, exceeding which the following error might occur: Stack too Deep.

Arrays

Now, what if you need a collection of something, say addresses. Well, just like most of the languages, Solidity also has Arrays.

When you want a collection of something, you can use an array. There are two types of arrays in Solidity : fixed arrays and dynamic arrays.

Array with a fixed length of 2 elements, containing integers :
uint[2] fixedArray;

Another fixed Array, can contain 5 strings :
string[5] stringArray;

A dynamic Array — has no fixed size, can keep growing:
uint[] dynamicArray;

An array of structs :
StructName[] variablename;

Working With Structs and Arrays

Consider the Struct we previously created

struct MyProfile{
 string firstname;
 string lastname;
 uint16 age;
 bool isMarried;
}
MyProfile[] public people;

Now let’s see how to create new Person’s objects and add them to our people array.

Create a New Person:

Person James = Person(“James”,”Ryan”,35,true);

Add that person to the Array:

people.push(James);

We can also combine these together and do them in one line of code to keep things clean:

people.push(Person(“James”,”Ryan”,35,true));

Mappings

Mappings allow the programmer to create key-value pairs for storing(as a list) and looking up data.

It can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value.

Two commonly used data types [ key_type ] for mapping keys are address and uint.
It is important to note that not every data type can be used as a key. For instance, structs and other mappings cannot be used as keys.
Unlike with keys, Solidity does not limit the data type for values [ key_values ]. It can be anything, including structs and other mappings.

Mappings are declared as:

Mapping(_Keytype => _ValueType )

_Keytype can be almost any type except for a dynamically sized array, a contract, an enum and a struct.

contract MappingExample {
mapping(address => uint) public balances;
function update(uint newBalance) {
balances[msg.sender] = newBalance;  }}
contract MappingUser {
function f() returns (uint) {
MappingExample m = new MappingExample();
m.update(100);
return m.balances(this);
}}

Control Structures

Solidity follows the same syntax of control structures as Java script or C.
So there is: if, else, while, do, for, break, continue, with the usual semantics known from C or JavaScript.

There is no type conversion from non-boolean to boolean types as there is in C and JavaScript.

Now let’s see how these Control structures are used in Solidity.

contract ControlStructure {
address public variable1;
function ControlStructure>){

// if-else can be used like this
if(input1==2)
     variable1=1;
else
     variable1=0;


// while can be used like this
while(input1>=0){
if(input1==5)
     continue;
input1=input1-1;
     variable1++;
}


// for loop can be used like this
for(uint i=0;i<=50;i++) 
{ 
    variable1++; if(variable1==4) break; 
}

 
//do while can be used like this 
do { variable1--; } (while variable1>0);


// Conditional Operator can be used like this
bool IsTrue = (variable1 == 1)?true: false;
/*will show an error because
there is no type conversion from non-boolean to boolean
*/
if(1)
{
}

So here we wrap up control structures and Data Structures in Solidity.

Stay connected for more blogs on Solidity.

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.


Knoldus-blog-footer-image

Written by 

Ayushi is a Software Developer having more than 1.5 year of experience in RUST. Her practice area is Rust and Go. She loves to solve daily coding challenges.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading