Difference between revisions of "Switch"

esse quam videri
Jump to: navigation, search
(Definition)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  
 
=Definition=
 
=Definition=
 +
A switch is a type of '''statement''' that controls the '''logical flow''' of an application. It allows for the value stored within a variable or expression to control said flow through the use of multiple "cases".
  
 
=Relevance=
 
=Relevance=
Line 12: Line 13:
  
 
choice = console.ReadLine();
 
choice = console.ReadLine();
 +
choice.ToUpper(); // This will make the content of the string upper case so we don't have to check for both lower and upper case in our switch.
  
 
switch(choice)
 
switch(choice)
Line 19: Line 21:
 
         break;
 
         break;
  
case "S:"
+
    case "S":
 
         player.moveSouth();
 
         player.moveSouth();
 
         break;
 
         break;
  
case "E":
+
    case "E":
 
         player.moveEast();
 
         player.moveEast();
 
         break;
 
         break;
  
case "W":
+
    case "W":
 
         player.moveWest();
 
         player.moveWest();
 +
        break;
 +
 +
    default:
 +
        getPlayerInput();
 
         break;
 
         break;
 
}
 
}
Line 34: Line 40:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
  
 
=Resources=
 
=Resources=
 
== See also ==
 
== See also ==
 
+
- [[Statement]]
 
 
==Notes==
 
 
 
  
 
==External Links==
 
==External Links==

Latest revision as of 18:38, 16 April 2020

Definition

A switch is a type of statement that controls the logical flow of an application. It allows for the value stored within a variable or expression to control said flow through the use of multiple "cases".

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();
choice.ToUpper(); // This will make the content of the string upper case so we don't have to check for both lower and upper case in our switch.

switch(choice)
{
    case "N":
        player.moveNorth();
        break;

    case "S":
        player.moveSouth();
        break;

    case "E":
        player.moveEast();
        break;

    case "W":
        player.moveWest();
        break;

    default:
        getPlayerInput();
        break;
}

Resources

See also

- Statement

External Links