Last active
April 29, 2018 19:21
-
-
Save KelsonBall/81ddf7c1174a9b8874b08adafcd8c81d to your computer and use it in GitHub Desktop.
examples of switch case statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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