Friday, December 12, 2008

Switch Statement :Simple Program

Definition:

1.Declare some int or string or anyother datatype. Here in the below example i declared int i variable.
2.For 'switch' statement use switch keyword to declare the statement, under the switch statement declare the different cases as per the requirements.
3.For Example switch(i), based on the input it will redirect to the proper cases.
4.If input value "1" it redirect into case 1, if input value 2 it redirect into case 2, if input value is other than these two cases , it will move into default case.

Example:

class Program

{

static void Main(string[] args)

{

int i;

Console.Write("i:");

i = Convert.ToInt32(Console.ReadLine());

switch (i)

{

case 1:

{

Console.WriteLine("This is 1");

Console.ReadLine();

break;

}

case 2:

{

Console.WriteLine("This is 2");

Console.ReadLine();

break;

}

default:

{

Console.WriteLine("This is Default");

Console.ReadLine();

break;

}

}

}

}

Output:
i=2
This is 2

i=4
This is Default

No comments: