Information Technology Reference
In-Depth Information
A Switch Example
The following code executes the switch statement five times, with the value of x ranging from
1 to 5 . From the output, you can tell which case section was executed on each cycle through
the loop.
for( int x=1; x<6; x++ )
{
switch( x ) // Evaluate the value of variable x.
{
case 2: // If x equals 2
Console.WriteLine
("x is {0} -- In Case 2", x);
break; // Go to end of switch.
case 5: // If x equals 5
Console.WriteLine
("x is {0} -- In Case 5", x);
break; // Go to end of switch.
default:
// If x is neither 2 nor 5
Console.WriteLine
("x is {0} -- In Default case", x);
break;
// Go to end of switch.
}
}
The output of the preceding code is the following:
x is 1 -- In Default case
x is 2 -- In Case 2
x is 3 -- In Default case
x is 4 -- In Default case
x is 5 -- In Case 5
Search WWH ::




Custom Search