Skip Navigation LinksArchmage Academy : Language Syntax : Switch Statement Login  

 
Skip Navigation Links.
Skip Navigation Links.  

Switch Statement

Switch statements (sometimes known as Select statements) also work very similar to if statements.  Instead of a condition, you supply a switch statement with a single variable and define different blocks of code to run depending on the value of that variable.  Most languages allow you to supply a default action, meaning how the switch statement would handle a value that you did not explicitly define code to handle.

Break Keyword:
Used to exit any loop or switch statement.  Most languages use this keyword at the end of each block of code that you have associated a value with.  Without this keyword, most switch statements will execute all blocks of code following the value it started from.

Examples:
var Card = new Object();
Card.FaceValue = "A";
Card.NumberValue = 1;
Card.Name = "Ace";
Card.Suit = "Spades";

switch(Card.Suit)
{
     case "Clubs":
     case "Spades":
          Card.Color = "Black";
          break;
     case "Diamonds":
     case "Hearts":
          Card.Color = "Red";
          break;
}
  Previous PageLoops, Next Page Functions.