/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class ValuesFallThrough
{
static void Main()
{
String myChoice = "NewLeft";
// switch on the string value of myChoice
switch (myChoice)
{
case "NewLeft":
Console.WriteLine(
"The NewLeft members are voting Democratic.");
goto case "Democrat";
case "Democrat":
Console.WriteLine("You voted Democratic.
");
break;
case "CompassionateRepublican": // fall through
case "Republican":
Console.WriteLine("You voted Republican.
");
Console.WriteLine("Don't you feel compassionate?");
break;
case "Progressive":
Console.WriteLine("You voted Progressive.
");
break;
default:
Console.WriteLine("You did not make a valid choice.");
break;
}
Console.WriteLine("Thank you for voting.");
}
}
Language Basics
Switch With Default Values
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class SwitchWithDefaultValues
{
static void Main()
{
const int Democrat = 0;
const int Republican = 1;
const int Progressive = 2;
// hard wire to Republican
int myChoice = 5;
// switch on the value of myChoice
switch (myChoice)
{
case Democrat:
Console.WriteLine("You voted Democratic.
");
break;
case Republican:
Console.WriteLine("You voted Republican.
");
break;
case Progressive:
Console.WriteLine("You voted Progressive.
");
break;
default:
Console.WriteLine("You did not make a valid choice.");
break;
}
Console.WriteLine("Thank you for voting.");
}
}
Switch Values
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class SwitchValues
{
static void Main()
{
const int Democrat = 0;
const int Republican = 1;
const int Progressive = 2;
// hard wire to Republican
int myChoice = Republican;
// switch on the value of myChoice
switch (myChoice)
{
case Democrat:
Console.WriteLine("You voted Democratic.");
break;
case Republican:
Console.WriteLine("You voted Republican.");
break;
case Progressive:
Console.WriteLine("You voted Progressive.");
break;
}
Console.WriteLine("Thank you for voting.");
}
}
Switch statement containing a branch with no statements: causes a 'fall-through' to the next branch
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_7.cs illustrates the use of
the switch statement containing a branch
with no statements: causes a "fall-through"
to the next branch
*/
public class Example4_7
{
public static void Main()
{
int value = 1;
switch (value)
{
case 0:
System.Console.WriteLine("Zero");
break;
case 1:
case 2:
System.Console.WriteLine("One or two");
break;
case 3:
System.Console.WriteLine("Three");
break;
default:
System.Console.WriteLine("Other number");
break;
}
}
}
Illustrates the use of the switch statement to compare string values
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_6.cs illustrates the use of
the switch statement to compare string values
*/
public class Example4_6
{
public static void Main()
{
string planetName = "Saturn"; // sixth planet from the Sun
switch (planetName)
{
case "Mercury":
System.Console.WriteLine(1);
break;
case "Venus":
System.Console.WriteLine(2);
break;
case "Earth":
System.Console.WriteLine(3);
break;
case "Mars":
System.Console.WriteLine(4);
break;
case "Jupiter":
System.Console.WriteLine(5);
break;
case "Saturn":
System.Console.WriteLine(6);
break;
case "Uranus":
System.Console.WriteLine(7);
break;
case "Neptune":
System.Console.WriteLine(8);
break;
case "Pluto":
System.Console.WriteLine(9);
break;
default:
System.Console.WriteLine("Planet unknown");
break;
}
}
}
Illustrates the use of the switch statement
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_5.cs illustrates the use of
the switch statement
*/
public class Example4_5
{
public static void Main()
{
int planetPosition = 4; // Mars
switch (planetPosition)
{
case 1:
System.Console.WriteLine("Mercury");
break;
case 2:
System.Console.WriteLine("Venus");
break;
case 3:
System.Console.WriteLine("Earth");
break;
case 4:
System.Console.WriteLine("Mars");
break;
case 5:
System.Console.WriteLine("Jupiter");
break;
case 6:
System.Console.WriteLine("Saturn");
break;
case 7:
System.Console.WriteLine("Uranus");
break;
case 8:
System.Console.WriteLine("Neptune");
break;
case 9:
System.Console.WriteLine("Pluto");
break;
default:
System.Console.WriteLine("Planet unknown");
break;
}
}
}
Empty cases can fall through
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Empty cases can fall through.
using System;
public class EmptyCasesCanFall {
public static void Main() {
int i;
for(i=1; i < 5; i++) switch(i) { case 1: case 2: case 3: Console.WriteLine("i is 1, 2 or 3"); break; case 4: Console.WriteLine("i is 4"); break; } } } [/csharp]