SEARCH C# PROGRAMMING CODES HERE

Statements in C#

Statements in C#
1-if statement
if statement consists of a boolean expression followed by one or more statements.
In some programming languages, several datatypes can be automatically converted into booleans, but in C#, you have to specifically make the result boolean.
So let's see how conditional logic can be used in c#.
syntax of an if statement
if(boolean_expression)
{
   /* Statement(s) will execute if the boolean expression is true */
}

If the boolean expression evaluates to true, then the block of code inside the if statement is executed. If the boolean expression evaluates to false, then the first set of code after the end of the if statement is executed.

Program
using System;

namespace if_Statement
  {
   class Program
 {
      static void Main(string[] args)
         {
         /* local variable definition */
         int x = 25;
         /* check the boolean condition using if statement */
         if (x < 45)
         {
            /* if condition is true then print the following statement */
            Console.WriteLine("x value is less than 45");
         }
                 Console.ReadLine();
         }
      }
}
Outputx  value is less than 45


2-If_ Else Statement in C#
if...else Statement can be followed by an optional else statement, which executes when the boolean expression is false.
syntax of an if…else statement
if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}
else
{
   /* statement(s) will execute if the boolean expression is false */
}

Program
using System;

namespace if_Statement
  {
   class Program
     {
      static void Main(string[] args)
        {
         int x = 25; /* Here x value is 25 */

            /* check the boolean condition using if statement */
            if (x > 45) /*Here condition x is greater then 45 so it is false */
            {
                /* if condition is true then print the following statement */
                Console.WriteLine("yes x value is 25");
            }
            else  /*this code will execute*/
            {
                /* if condition is false then print the following */
                Console.WriteLine("No x value is 25.");
            }
            Console.ReadLine();
            }
 }

Output- No x value is 25.

3-if...else… if...else Statement
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
syntax of an if… else..if..else statement


if( boolean_expression 1)
 {
   /* Executes when the boolean_expression 1 is true */
 }
else if(  boolean_expression 2)
 {
   /* Executes when the boolean_expression 2 is true */
 }
else if( boolean_expression 3)
 {
   /* Executes when the boolean_expression 3 is true */
}
else if( boolean_expression 4)
 {
   /* Executes when the  boolean_expression 4 is true */
}
else
 {
   /* executes when the none of the above condition is true */
 }
















See how to implement in the Program as shown below.

For Example
            /* local variable definition */
            int x = 100;

            /* check the boolean condition */
            if (x == 80)
            {
                /* if the condition is true then print the following */
                Console.WriteLine("Value of x is 80");
            }
            else if (x == 25)
            {
                /* if else if condition is true */
                Console.WriteLine("Value of x is 25");
            }
            else if (x== 15)
            {
                /* if else if condition is true  */
                Console.WriteLine("Value of x is 15");
            }
            else
            {
                /* if none of the conditions is true */
                Console.WriteLine("None of the values is matching.");
            }
            Console.WriteLine("Value of x is= {0}", x);
            Console.ReadLine();
            }


Output -None of the values is matching.
            Value of x is=100


4-Switch Statement in C#

The switch statement is like a set of if statements which allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

The syntax for switch statement

switch(expression)
 {
   case 1  :
      statement(s);
      break;
   case 2  :
   case 3  :
      statement(s);
      break;
   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}


Program 

namespace switch_statement
{
    class Program
{
         /* local variable definition */
        static void Main(string[] args)
        {
            char number = 'b';

            switch (number)
            {
                case 'a':
                    Console.WriteLine("Very Good!");
                    break;
                case 'b':
                case 'c':
                    Console.WriteLine("Good");
                    break;
            }
            Console.WriteLine("You are in {0}", number+" level");
            Console.ReadLine();
        }
    }
}

Output- Good
You are in b level