Wednesday, 18 July 2012

HOW TO USE C# CONTROLS AND LOOPS


For a software it’s functionality can be inbuilt by using expressions. You need to do a lot of decision and looping for your purpose. For Example if you want to assign values to an array you need to implement a loop and assign the values to the corresponding elements of the array. Or if you need to take a decision you need to check for special condition to match one of your requirements.
So here we are going to discuss control statements and loops.

The If Statement

The if statement evaluates an expression and branches the control . C# having there forms of statement.

Simple If statement

The simple if statement takes the following form

If(Boolean Expression)
{
// execute statement if it is true.
}

For Eg. See the sample

namespace ControlStructure
{
class Program
{
static void Main(string[] args)
{
int a = 10;
if (a == 10)
{
Console.WriteLine("Hi You are in a = 10");
}
}
}
}
If-Else statement
The simple if statement  is capable to evaluate your expression if it is evaluate to   true. If you want to evaluate the expression to false and execute another statement we can if-then-else

The format of if else is

If(Boolean expression
{
// execute if it evaluate to true
}
else
{
// execute if evaluateas false
}

For eg

namespace ControlStructures
{
class Program
{
static void Main(string[] args)
{
int a = 10;
if (a == 10)
{
Console.WriteLine("Hi You are in a = 10");
}
else
{
Console.WriteLine("Hai you in not a != 10");
}
}
}
}

If Else IF else Statement
Some times you need to evaluate multiple conditions conditional checking on that case you have to use the if else statement like the following.

If(Boolean expression)
{
}else if(Boolean expression)
{
}
….
……
Else if(Boolean expression)
{
}

For eg.

namespace ControlStructures
{
class Program
{
static void Main(string[] args)
{
int a = 10;
if (a == 10)
{
Console.WriteLine("Hi You are in a = 10");
}
else if(a == 20)
{Console.WriteLine("Hai you in not a == 20");
}
else if (a == 30)
{
Console.WriteLine("Hai you in not a == 30");
}
}
}
}

Switch Statement

if you are having a lot of if condition that will be same of condition then it will be a big . In that case C# is provided with another control statement named swith. The basic form of switch is
switch(integral or string expression)
{
Case <literal 1>:
Statement(s)
Break;
..
...
Case <literal – n> :
Statement(s)
Break;
Default:
Statement (s)
Break;
}

For Example:
namespace args
{
int testno;
Console.Write("Enter the no (1 - 10) for your input ");
string str = Console.ReadLine();
testno = Convert.ToInt32(str);
ControlStructures
{
class Program
{
static void Main(string[]
switch (testno)
{
case 1:
{
Console.WriteLine(testno);
break;
}
case 2:
{
Console.WriteLine(testno);
break;
}
case 3:
{
Console.WriteLine(testno);
break;
}
case 4:
{
Console.WriteLine(testno);
break;
}
case 5:
{
Console.WriteLine(testno);
break;
}
case 6:
{
Console.WriteLine(testno);
break;
}
case 7:
{
Console.WriteLine(testno);
break;
}
case 8:
{
Console.WriteLine(testno);
break;
}

case 9:
{
Console.WriteLine(testno);
break;
}
default:
{
Console.WriteLine("wrong entry");
break;
}
}
}
}
}


In switch it is using of the break statement si mandatory. The switch statement can be also be used as like as

Switch (choice)
{
Case “exp”:
Case “exp2”:
{
// statement
Break;
}
Case “exp3”:
Case “exp4”:
{
// statement
Break;
}
}

C# loops

Lopps are using to perform a sequence of operations in a program. In C3 we are having four types of loops.  The while loop, the do loop, the for loop and foreach loop. Let’s see how it can be implemented.



The while Loop


If you want to continuesly execute a group of statements while a condition is true you can use the while loop. It can be used on the following format.



While(Boolean expression)

{

 
// statement to be executed.



}

For eg.



class Program

{

static void Main(string[] args)

{

string str1 = "0";

Console.Write("Enter Your option Y/N to continue ");
while (str1 != "N")
{
Console.Write("Enter The no to Display ");
str1 = Console.ReadLine();
Console.WriteLine("You Enetered " + str1);
}
}
}

The Do loop

The do loop is also similar to while but sometimes you need to execute the statement at least once. On this case you need to use the do loop. The format of do loop is like

Do
{

}
while(Boolean expression);

For example

class Program
 
{
        static void Main(string[] args)
        {
            string str1 = "0";
            Console.WriteLine("Enter Your option N to Exit ");
            do
            {
                Console.Write("Enter The no to Display ");
                str1 = Console.ReadLine();
                Console.WriteLine("You Enetered " + str1);
            }
            while (str1 != "N");
         }
    }




The for Loop
              
The for loop is used when you know how many times a sequence of statement is executed. The for loop format is like
For(initialiser;Boolean expression; modifier/increment)
{
            //statements to execution.
}

class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i <= 10; i++)
       {
            Console.WriteLine("The current value is " + i.ToString());

            }
         }
    }

The foreach statement

           The foreach statement is used when you want to iterate through collections.

The syntax is

Foreach(type identifier in collection)
{
            // statements

}
For eg;
class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[10];

          for (int i = 0; i<10;i++)
            {

             arr[i] = i;
            }
            foreach(int ar in arr)
            {
               Console.WriteLine(ar);
}
        }
    }





No comments:

Post a Comment