Control Statements

C# has the following mechanisms to conditionally control the flow of program execution
  • Selection statements (if, switch)
  • Loop statements (while, do-while, for, foreach)
If Statement:

An if statement executes a body of code depending on whether a bool expression is true.
Syntax:
         if(Conditions)
         {
            //statements
         }
         else
         {
            // Statements
         }

For Example:
         static void Main()
         {
            Console.WriteLine("Enter your age: ");
             int age=int.Parse(Console.ReadLine());
             if(age<=20)
             {
                Console.WriteLine("Your are eligible to apply..  ");
             }
             else
             {
                Console.WriteLine("Your are eligible to apply..  ");
             }
Multiple If:
          static void Main()
          {
             Console.WriteLine("Enter your age: ");
             int age=int.Parse(Console.ReadLine());
            if (age >= 35)
            Console.WriteLine ("You can be president!");
            else if (age >= 21)
            Console.WriteLine ("You can drink!");
            else if (age >= 18)
            Console.WriteLine ("You can vote!");
            else
           Console.WriteLine ("You can wait!");
       }
If the body of code is a single statement, you can optionally omit the braces.

Switch Statement:
When ever we want to execute a single case among multiple cases based on a condition we will use the switch condition.
Syntax:
         switch<expression >
         {
           case 1: statements for value 1;
                       break;
           case 2: statements for value 2;
                       break;
           case 3: statements for value 3;
                       break;
                  .........
           Default : statements for default case;
                        break;
          }
if the expression result is 1 then control will execute case 1 ie., 'statements for value 1' then it will break and default case executes when all the above cases fails.
break is a keyword, used to move control out of the switch block.
For Example:
         static void Main()
         {
            Console.WriteLine("Enter month number ")
             int month=int.Parse(Console.ReadLine());
             switch(month)
             {
                case 1: Console.WriteLine("January");
                            break;
                case 2: Console.WriteLine("February");
                            break;
                case 3: Console.WriteLine("March");
                            break;
                case 4: Console.WriteLine("April");
                            break;
                case 5: Console.WriteLine("May");
                            break;
                case 6: Console.WriteLine("June");
                            break;
                case 7: Console.WriteLine("July");
                            break;
                case 8: Console.WriteLine("August");
                            break;
                case 9: Console.WriteLine("Septemeber");
                            break;
                case 10: Console.WriteLine("October");
                            break;
                case 11: Console.WriteLine("November");
                            break;
                case 12: Console.WriteLine("December");
                            break;
                Default: Console.WriteLine("Invalid month number");
                            break;
             }  
          }

While Loop:
while loops repeatedly execute a body of code while a bool expression is true. The expression is tested before the body of the loop is executed.
Syntax:
       while(Condition)
        {
           //Statement(s)
         }
 For example:

          static void Main()
         {
            int i = 0;
            while (i < 3)
           {
             Console.WriteLine (i);
              i++;
           }
         }
do-while Loop:

Do-while loop executes the statement block first later tests the expression  (ensuring that the block is always
executed at least once).
For Example:
          static void Main()
         {
            int i = 0;
            do
            {
              Console.WriteLine (i);
               i++;
             } while (i < 3);
          }

For Loop:

For loops is used to execute set of statements repeatedly for specific number of times.
Syntax:
         for (initialization; condition; iteration)
         {
            //statement(s);
          }
A for loop contains three clauses as follows
Initialization clause: Executed before the loop begins, used to initialize one or more iteration
                             variables.
Condition clause: The bool expression, if true, will execute the body.
Iteration clause : Executed after each iteration of the statement block, used typically to update
                          the iteration variable.
For Example:
          static void Main()
         {
          for (int i =1; i < 5; i++)
            Console.WriteLine (i);
         }

For-Each loop:
For-Each loop is used to iterate with in a collection. Where collection is list of values.
Syntax:
       foreach(Variable in collection)
       {
          //Statement(s)
       }
For Example:
          static void Main()
         {
            foreach (char v in "dotnet")    // v is the iteration variable
            Console.WriteLine (v);
         }


Click here for more  : C#.NET Language Basics

No comments:

Post a Comment