- 다른 뜻에 대해서는 스위치 문(switch statement) 문서를 참조하십시오.
- 다른 뜻에 대해서는 스위치 문 (코드 스멜) 문서를 참조하십시오.
1 개요[ | ]
- switch statement; switch
- switch 문
- case
- case ... when
2 Bash[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
Bash
Copy
STR=hello
case "$STR" in
hello)
echo "Case hello"
;;
world)
echo "Case world"
;;
*)
echo "default case"
;;
esac
Loading
3 C[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
C
Copy
#include <stdio.h>
int main()
{
int age = 3;
switch (age)
{
case 1:
printf("You're one.");
break;
case 2:
printf("You're two.");
break;
case 3:
printf("You're three.");
case 4:
printf("You're three or four.");
break;
default:
printf("You're not 1, 2, 3 or 4!");
}
}
Loading
4 C#[ | ]
C#
Copy
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}
5 Go[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
Go
Copy
package main
import "fmt"
func main() {
caseSwitch := 1
switch caseSwitch {
case 1:
fmt.Println("Case 1");
case 2:
fmt.Println("Case 2");
default:
fmt.Println("Default case");
}
}
Loading
6 Java[ | ]
Java
Copy
switch (age) {
case 1: System.out.printf("You're one."); break;
case 2: System.out.printf("You're two."); break;
case 3: System.out.printf("You're three."); break;
case 4: System.out.printf("You're four."); break;
default: System.out.printf("You're neither!"); break;
}
7 JavaScript[ | ]
JavaScript
Copy
switch(obj){
case 'hello':
console.log("hello");
break;
case 'world':
console.log("world");
break;
default:
console.log("default case");
}
8 PHP[ | ]
PHP
Copy
switch($i) {
case 'hello':
echo "hello";
break;
case 'world':
echo "world";
break;
default:
echo "default case";
}
- alternative syntax
PHP
Copy
switch($i):
case 'hello':
echo "hello";
break;
case 'world':
echo "world";
break;
default:
echo "default case";
}
endswitch;
9 Python[ | ]
Python
Copy
str = "world"
if str == "hello":
print "case hello"
elif str == "world":
print "case world"
else:
print "default case"
# case world
Python
Copy
# http://code.activestate.com/recipes/410692/
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
yield self.match
raise StopIteration
def match(self, *args):
if self.fall or not args:
return True
elif self.value in args:
self.fall = True
return True
else:
return False
str = 'world'
for case in switch(str):
if case('hello'):
print 'case hello'
break
if case('world'):
print 'case world'
break
print 'default case'
10 Perl[ | ]
Perl
Copy
use Switch;
switch ($i) {
case /\S/ { print "character found"; next } # fall-through
case 'hello' { print "hello" }
case 'world' { print "world" }
else { print "default case" }
}
Perl
Copy
use 5.010; no warnings 'experimental';
given ( $q ) {
when ($_ =~ /\S/) { print "character found"; continue; } # fall-through
when ($_ eq 'hello') { print "hello"; }
when ($_ eq 'world') { print "world"; }
default { }
}
11 Ruby[ | ]
Ruby
Copy
str = "world"
case str
when "hello"
puts "hello"
when "world"
puts "world"
else
puts "default"
end
# world
Ruby
Copy
str = "world"
case str
when "hello" then puts "hello"
when "world" then puts "world"
else puts "default"
end
# world