Difference between revisions of "Switch"
esse quam videri
(Created page with " =Definition= =Relevance= =Explanation= =Resources= == See also == ==Notes== ==External Links== Category:Programming Language Concepts Category:Object Orient...") |
|||
Line 4: | Line 4: | ||
=Relevance= | =Relevance= | ||
− | = | + | =Example= |
+ | Below is an example of a switch for an adventure game | ||
+ | <syntaxhighlight lang ="csharp"> | ||
+ | |||
+ | console.WriteLine("You awake and find yourself at an intersection, which path will you take?"); | ||
+ | console.WriteLine("Use N, S, E, or W for North, South, East, or West"); | ||
+ | |||
+ | choice = console.ReadLine(); | ||
+ | |||
+ | switch(choice) | ||
+ | { | ||
+ | case "N": | ||
+ | player.moveNorth(); | ||
+ | break; | ||
+ | |||
+ | case "S:" | ||
+ | player.moveSouth(); | ||
+ | break; | ||
+ | |||
+ | case "E": | ||
+ | player.moveEast(); | ||
+ | break; | ||
+ | |||
+ | case "W": | ||
+ | player.moveWest(); | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | |||
+ | </syntaxhighlight> | ||
Revision as of 18:10, 21 August 2019
Definition
Relevance
Example
Below is an example of a switch for an adventure game
console.WriteLine("You awake and find yourself at an intersection, which path will you take?");
console.WriteLine("Use N, S, E, or W for North, South, East, or West");
choice = console.ReadLine();
switch(choice)
{
case "N":
player.moveNorth();
break;
case "S:"
player.moveSouth();
break;
case "E":
player.moveEast();
break;
case "W":
player.moveWest();
break;
}