최신판 |
당신의 편집 |
1번째 줄: |
1번째 줄: |
| [[분류: switch]] | | [[분류: 제어]] |
| [[분류: 제어문]]
| |
| {{다른뜻|스위치 문(switch statement)|스위치 문}}
| |
| {{다른뜻|스위치 문 (코드 스멜)}}
| |
| ==개요==
| |
| ;switch statement; switch | | ;switch statement; switch |
| ;switch 문 | | ;switch 문 |
| ;case | | ;case 문 |
| ;case ... when
| |
|
| |
|
| ==Bash== | | ==Bash== |
| [[category:bash]] | | [[category:bash]] |
| {{참고|Bash case}}
| | <source lang='bash'> |
| <syntaxhighlight lang='bash' run> | |
| STR=hello | | STR=hello |
|
| |
| case "$STR" in | | case "$STR" in |
| hello) | | hello) |
| echo "Case hello" | | echo "Case hello" |
| ;; | | ;; |
| world) | | world) |
| echo "Case world" | | echo "Case world" |
| ;; | | ;; |
| *) | | *) |
| echo "default case" | | echo "default case" |
| ;; | | ;; |
| esac | | esac |
| </syntaxhighlight>
| | # Case hello |
| | | </source> |
| ==C==
| |
| [[분류: C]]
| |
| {{참고|C switch}}
| |
| <syntaxhighlight lang='c' run>
| |
| #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!");
| |
| }
| |
| }
| |
| </syntaxhighlight> | |
|
| |
|
| ==C#== | | ==C#== |
| [[category:csharp]] | | [[category:csharp]] |
| <syntaxhighlight lang='csharp'> | | <source lang='csharp'> |
| int caseSwitch = 1; | | int caseSwitch = 1; |
| switch (caseSwitch) | | switch (caseSwitch) |
71번째 줄: |
38번째 줄: |
| break; | | break; |
| } | | } |
| </syntaxhighlight> | | </source> |
| | |
| ==Go==
| |
| [[category:Go]]
| |
| {{참고|Go switch}}
| |
| <syntaxhighlight lang='go' run>
| |
| 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");
| |
| }
| |
| }
| |
| </syntaxhighlight>
| |
|
| |
|
| ==Java== | | ==Java== |
| [[분류: Java]] | | [[분류: Java]] |
| <syntaxhighlight lang='Java'> | | <source lang='Java'> |
| switch (age) { | | switch (age) { |
| case 1: System.out.printf("You're one."); break; | | case 1: System.out.printf("You're one."); break; |
102번째 줄: |
50번째 줄: |
| default: System.out.printf("You're neither!"); break; | | default: System.out.printf("You're neither!"); break; |
| } | | } |
| </syntaxhighlight> | | </source> |
|
| |
|
| ==JavaScript== | | ==JavaScript== |
| [[category: JavaScript]] | | [[category: JavaScript]] |
| <syntaxhighlight lang='JavaScript'> | | <source lang='JavaScript'> |
| switch(obj){ | | switch(obj){ |
| case 'hello': | | case 'hello': |
117번째 줄: |
65번째 줄: |
| console.log("default case"); | | console.log("default case"); |
| } | | } |
| </syntaxhighlight> | | </source> |
|
| |
|
| ==PHP== | | ==PHP== |
| [[category: PHP]] | | [[category: PHP]] |
| <syntaxhighlight lang='PHP'> | | <source lang='PHP'> |
| switch($i) { | | switch($i) { |
| case 'hello': | | case 'hello': |
132번째 줄: |
80번째 줄: |
| echo "default case"; | | echo "default case"; |
| } | | } |
| </syntaxhighlight> | | </source> |
| * alternative syntax
| | <source lang='PHP'> |
| <syntaxhighlight lang='PHP'> | |
| switch($i): | | switch($i): |
| case 'hello': | | case 'hello': |
146번째 줄: |
93번째 줄: |
| } | | } |
| endswitch; | | endswitch; |
| </syntaxhighlight> | | </source> |
| | |
| ==Python==
| |
| [[category: Python]]
| |
| <syntaxhighlight lang='Python'>
| |
| str = "world"
| |
| if str == "hello":
| |
| print "case hello"
| |
| elif str == "world":
| |
| print "case world"
| |
| else:
| |
| print "default case"
| |
| # case world
| |
| </syntaxhighlight>
| |
| <syntaxhighlight lang='python'>
| |
| # 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'
| |
| </syntaxhighlight>
| |
| | |
| ==Perl==
| |
| [[category: Perl]]
| |
| <syntaxhighlight lang='Perl'>
| |
| use Switch;
| |
| switch ($i) {
| |
| case /\S/ { print "character found"; next } # fall-through
| |
| case 'hello' { print "hello" }
| |
| case 'world' { print "world" }
| |
| else { print "default case" }
| |
| }
| |
| </syntaxhighlight>
| |
| | |
| <syntaxhighlight lang='perl'>
| |
| 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 { }
| |
| }
| |
| </syntaxhighlight>
| |
| | |
| ==Ruby==
| |
| [[category: Ruby]]
| |
| <syntaxhighlight lang='Ruby'>
| |
| str = "world"
| |
| case str
| |
| when "hello"
| |
| puts "hello"
| |
| when "world"
| |
| puts "world"
| |
| else
| |
| puts "default"
| |
| end
| |
| # world
| |
| </syntaxhighlight>
| |
| <syntaxhighlight lang='Ruby'>
| |
| str = "world"
| |
| case str
| |
| when "hello" then puts "hello"
| |
| when "world" then puts "world"
| |
| else puts "default"
| |
| end
| |
| # world
| |
| </syntaxhighlight>
| |
|
| |
|
| ==같이 보기== | | ==같이 보기== |