"스위치 문"의 두 판 사이의 차이

 
(사용자 3명의 중간 판 13개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: 제어]]
[[분류: switch]]
[[분류: 제어문]]
{{다른뜻|스위치 문(switch statement)|스위치 문}}
{{다른뜻|스위치 문 (코드 스멜)}}
==개요==
;switch statement; switch
;switch statement; switch
;switch 문
;switch 문
8번째 줄: 12번째 줄:
[[category:bash]]
[[category:bash]]
{{참고|Bash case}}
{{참고|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
# Case hello
</syntaxhighlight>
</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]]
<source lang='csharp'>
<syntaxhighlight lang='csharp'>
int caseSwitch = 1;
int caseSwitch = 1;
switch (caseSwitch)
switch (caseSwitch)
40번째 줄: 71번째 줄:
         break;
         break;
}
}
</source>
</syntaxhighlight>
 
==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]]
<source lang='Java'>
<syntaxhighlight lang='Java'>
switch (age) {
switch (age) {
   case 1: System.out.printf("You're one."); break;
   case 1: System.out.printf("You're one."); break;
52번째 줄: 102번째 줄:
   default: System.out.printf("You're neither!"); break;
   default: System.out.printf("You're neither!"); break;
}
}
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
switch(obj){
switch(obj){
   case 'hello':
   case 'hello':
67번째 줄: 117번째 줄:
     console.log("default case");
     console.log("default case");
}
}
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='PHP'>
<syntaxhighlight lang='PHP'>
switch($i) {
switch($i) {
   case 'hello':
   case 'hello':
82번째 줄: 132번째 줄:
     echo "default case";
     echo "default case";
}
}
</source>
</syntaxhighlight>
* alternative syntax
* alternative syntax
<source lang='PHP'>
<syntaxhighlight lang='PHP'>
switch($i):
switch($i):
   case 'hello':
   case 'hello':
96번째 줄: 146번째 줄:
}
}
endswitch;
endswitch;
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='Python'>
<syntaxhighlight lang='Python'>
str = "world"
str = "world"
if str == "hello":
if str == "hello":
109번째 줄: 159번째 줄:
     print "default case"
     print "default case"
# case world
# case world
</source>
</syntaxhighlight>
<source lang='python'>
<syntaxhighlight lang='python'>
# http://code.activestate.com/recipes/410692/
# http://code.activestate.com/recipes/410692/
class switch(object):
class switch(object):
137번째 줄: 187번째 줄:
break
break
print 'default case'
print 'default case'
</source>
</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==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
str = "world"
str = "world"
case str
case str
152번째 줄: 225번째 줄:
end
end
# world
# world
</source>
</syntaxhighlight>
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
str = "world"
str = "world"
case str
case str
161번째 줄: 234번째 줄:
end
end
# world
# world
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2023년 8월 20일 (일) 02:40 기준 최신판

  다른 뜻에 대해서는 스위치 문(switch statement) 문서를 참조하십시오.
  다른 뜻에 대해서는 스위치 문 (코드 스멜) 문서를 참조하십시오.

1 개요[ | ]

switch statement; switch
switch 문
case
case ... when

2 Bash[ | ]

STR=hello

case "$STR" in
hello)
	echo "Case hello"
	;;
world)
	echo "Case world"
	;;
*)
	echo "default case"
	;;
esac

3 C[ | ]

#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!");
    }
}

4 C#[ | ]

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[ | ]

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");
	}
}

6 Java[ | ]

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[ | ]

switch(obj){
  case 'hello':
    console.log("hello");
    break;
  case 'world':
    console.log("world");
    break;
  default:
    console.log("default case");
}

8 PHP[ | ]

switch($i) {
  case 'hello':
    echo "hello";
    break;
  case 'world':
    echo "world";
    break;
  default:
    echo "default case";
}
  • alternative syntax
switch($i):
  case 'hello':
    echo "hello";
    break;
  case 'world':
    echo "world";
    break;
  default:
    echo "default case";
}
endswitch;

9 Python[ | ]

str = "world"
if str == "hello":
    print "case hello"
elif str == "world":
    print "case world"
else:
    print "default case"
# case world
# 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[ | ]

use Switch;
switch ($i) {
	case /\S/	{ print "character found"; next } # fall-through
	case 'hello'	{ print "hello" }
	case 'world'	{ print "world" }
	else		{ print "default case" }
}
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[ | ]

str = "world"
case str
when "hello"
    puts "hello"
when "world"
    puts "world"
else
    puts "default"
end
# world
str = "world"
case str
    when "hello" then puts "hello"
    when "world" then puts "world"
    else puts "default"
end
# world

12 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}