Flow Control

In C# Langualge there is several keywords that are used to modify the Execution flow of the program. When the program is run, the lines of statements are executed from top of the code to bottom of the code with line by line.

Flow control statements are if, if else,
for loop, while and switch

If Statement:

The if keyword is used to check if the expression is true or false. If the condition is true, then statement is executed. If the statement can be single statement or compond statement.

Example:

namespace IfStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number");
            int n = Convert.ToInt32(Console.ReadLine());

            if (n > 0)
            {
                Console.WriteLine("The n variable is positive");
            }
            Console.ReadLine();
        }
    }
}

Else Statement:

If the Condition is false, Then else block statement is executed. 

Example:

namespace IfStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number");
            int n = Convert.ToInt32(Console.ReadLine());

            if (n > 0)
            {
                Console.WriteLine("The n variable is positive");
            }
            else
            {
                 Console.WriteLine("The n variable is nagative");
            }
            Console.ReadLine();
        }
    }
}

If Else Statement:

If first Expression is true, First block is executed . Second expression is true, Second block of code is executed. If two statements are false else block is executed. 

Example:

namespace IfElseStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Name?");
            string n = Convert.ToString(Console.ReadLine());

            if (n == "Mahesh")
            {
                Console.WriteLine("The Mahesh is a developer");
            }
            else if (n == "Rakesh")
            {
                Console.WriteLine("The Rakesh is a Scientist");
            }
            else
            {
                Console.WriteLine("Nothing");
            }
            Console.ReadLine();
        }
    }
}

For Loop

For loop is used for iterating the value multiple times.

Syntax:

for (variable initialization; condition; increment or decrement)
{
    //execute this code block
}

Example:

namespace ForLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number");
            int n = Convert.ToInt32(Console.ReadLine());

            for(int i = 0; i < n; i++)
            {
                Console.WriteLine("The value of i:  "+ i +"");
            }
            Console.ReadLine();
        }
    }
}

Explonation: 

Suppose, Given input is 5. In for loop, will iterate 5 times and displays the 0,1,2,3,4

While Loop:

while loop execute repeatedly 

Syntax :

While(boolean expression)
{
    //execute code as long as condition returns true    
}

Example:

namespace WhileLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number");
            int n = Convert.ToInt32(Console.ReadLine());

            while (i < n)
              {
                Console.WriteLine("Value of i: {0}", i);
                 i++;
               }
            Console.ReadLine();
        }
    }
}
While loop includes boolean expression which will return true or false. Block of code is repeatedly executed until the condition is true. The initialization should be before the while loop and increment or decrement will be after the while loop. 

Switch Statement:

Switch statement is nothing but descision making statement. 

Syntax:

switch(expression)
{
    case <value1>
        // code block
        break;
    case <value2>
        // code block
        break;
    case <valueN>
        // code block
        break;
    default 
        // code block
        break;

}

Example:

namespace Switch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numbers like 5 or 10 or 15");
            int n = Convert.ToInt32(Console.ReadLine());

            switch (n)
             { 
               case 5:
                 Console.WriteLine("Value of n is 5");
               break;
               case 10:
                 Console.WriteLine("Value of n is 10");
               break;
              case 15:
                 Console.WriteLine("Value of n is 15");
              break;
             default:
                 Console.WriteLine("Unknown value");
             break;
           }
            Console.ReadLine();
        }
    }
}

Popular Posts