Learn Solidity: Control Structure (if-else, for, while, Do-While)

In this post, we will understand the Control Structure (if-else, for, while, Do-While) in Solidity Language.

Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.
pragma solidity 0.4.8;

// Solidity follows the same syntax of controle structures as Java script or C.
// hence Most of the control structures from JavaScript are available in Solidity 
// except for switch and goto. So there is: if, else, while, do, for, break, continue, 
// return, ? :, with the usual semantics known from C or JavaScript.

// Let's quickly go through ther syntax in this lecture.


contract ControlStructure {
    /*
    * @title A Simple Example
    * @author Toshendra Sharma
    * @notice Example for the Solidity Course
    * @dev This is only for demo the simple Coin example
    * 
    */
    address public a;

    function ControlStructure(uint input1) {
        
        // if-else can be used like this
        if(input1 == 2)
            a = 1;
        else
            a = 0;

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


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

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

        // Conditional Operator can be used like this
        bool isTrue = (a == 1)? true: false;

        
        // will show an error because 
        //there is no type conversion from non-boolean to boolean   
        if (1) { 
            //some work
        }



    }

}
Previous: Arithmetic, Logical & Bitwise Operators in Solidity Next: Scoping and Declarations in Solidity