Skip to content

Instantly share code, notes, and snippets.

@KelsonBall
Last active April 29, 2018 19:21
Show Gist options
  • Save KelsonBall/81ddf7c1174a9b8874b08adafcd8c81d to your computer and use it in GitHub Desktop.
Save KelsonBall/81ddf7c1174a9b8874b08adafcd8c81d to your computer and use it in GitHub Desktop.
examples of switch case statements
// a switch-case must be in a method
public int GetColorRank(String color)
{
switch (color)
{
case "green":
return 1;
case "blue":
return 2;
case "yellow":
return Int32.MinValue;
}
return 0;
}
// a case in a switch-case can call other methods
public int GetColorRank(String color)
{
switch (color)
{
case "green":
return 1;
case "blue":
return 2;
default:
return GetIneferiorColorRank(color);
}
// methods can call themselves from switch cases too
public int Fibinacci(int n)
{
switch (n)
{
case 0:
return 0;
case 1:
case 2:
return 1;
default:
return Fibinacci(n - 1) + Fibinacci(n - 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment