%%Switch Statements
% A switch statement is a second type of conditional control statement (the
% first type is if statements)

% A switch statement tests for equality between a single expression and
% multiple cases.

% More restrictive than an if statement because it only tests for equality.
% However, it is generally easier to read and debug than many if statements
% if all you need is to test for equality

% Tests equality between scalars using == and test equality between strings
% using strcmp (case sensitive).

%General syntax:
%switch expression
% case value1
%   statements1
% case value2
%   statements2
% case value3
%   statements3
% otherwise
%   statements4
%end

%How this works:
%Compares each case in order and tests if expression is true to the value
%of the case
%First switch tests if expression is equal to value1, executes statements1
%if it is equal
%Next, if value1 is not equal to expression, tests value2 and expression,
%executes statements2 if they are equal
%Continues for each case
%If no cases are equal to expression, then we executes the statements after
%otherwise (statements4)

%Can have any number of cases but only one otherwise statement. Otherwise
%is optional
%Can enclose multiple choices in a single value using {}

%Example 1: Rock, Paper, Scissors (again)
ROCK = 1; PAPER = 2; SCISSORS = 3;
p1 = SCISSORS; p2 = PAPER;
switch p1
  case ROCK
    switch p2
      case PAPER
        winner = 2;
      case SCISSORS
        winner = 1;
      case ROCK
        winner = 0;
    end
    
  case PAPER
    switch p2
      case ROCK
        winner = 1;
      case SCISSORS
        winner = 2;
      case PAPER
        winner = 0;
    end
    
  case SCISSORS
    switch p2
      case ROCK
        winner = 2;
      case PAPER
        winner = 1;
      case SCISSORS
        winner = 0;
    end
end
disp(winner)

%Example 2: User input
choice = input('Yes or no? (y/n) ', 's');
switch choice
  case {'Y', 'y'}
    disp('yes')
  case {'N', 'n'}
    disp('no')
  otherwise
    disp('unknown choice')
end
