"Switch 문"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 16개는 보이지 않습니다)
1번째 줄: 1번째 줄:
 +
[[분류: 제어]]
 
;switch statement; switch
 
;switch statement; switch
 
;switch 문
 
;switch 문
;case
+
;case
 +
;case ... when
  
 
==Bash==
 
==Bash==
 
[[category:bash]]
 
[[category:bash]]
 +
{{참고|Bash case}}
 
<source lang='bash'>
 
<source lang='bash'>
 
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
 
# Case hello
 
</source>
 
</source>
49번째 줄: 54번째 줄:
 
   default: System.out.printf("You're neither!"); break;
 
   default: System.out.printf("You're neither!"); break;
 
}
 
}
 +
</source>
 +
 +
==JavaScript==
 +
[[category: JavaScript]]
 +
<source lang='JavaScript'>
 +
switch(obj){
 +
  case 'hello':
 +
    console.log("hello");
 +
    break;
 +
  case 'world':
 +
    console.log("world");
 +
    break;
 +
  default:
 +
    console.log("default case");
 +
}
 +
</source>
 +
 +
==PHP==
 +
[[category: PHP]]
 +
<source lang='PHP'>
 +
switch($i) {
 +
  case 'hello':
 +
    echo "hello";
 +
    break;
 +
  case 'world':
 +
    echo "world";
 +
    break;
 +
  default:
 +
    echo "default case";
 +
}
 +
</source>
 +
* alternative syntax
 +
<source lang='PHP'>
 +
switch($i):
 +
  case 'hello':
 +
    echo "hello";
 +
    break;
 +
  case 'world':
 +
    echo "world";
 +
    break;
 +
  default:
 +
    echo "default case";
 +
}
 +
endswitch;
 +
</source>
 +
 +
==Python==
 +
[[category: Python]]
 +
<source lang='Python'>
 +
str = "world"
 +
if str == "hello":
 +
    print "case hello"
 +
elif str == "world":
 +
    print "case world"
 +
else:
 +
    print "default case"
 +
# case world
 +
</source>
 +
<source 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'
 +
</source>
 +
 +
==Perl==
 +
[[category: Perl]]
 +
<source 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" }
 +
}
 +
</source>
 +
 +
<source 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 { }
 +
}
 +
</source>
 +
 +
==Ruby==
 +
[[category: Ruby]]
 +
<source lang='Ruby'>
 +
str = "world"
 +
case str
 +
when "hello"
 +
    puts "hello"
 +
when "world"
 +
    puts "world"
 +
else
 +
    puts "default"
 +
end
 +
# world
 +
</source>
 +
<source lang='Ruby'>
 +
str = "world"
 +
case str
 +
    when "hello" then puts "hello"
 +
    when "world" then puts "world"
 +
    else puts "default"
 +
end
 +
# world
 
</source>
 
</source>
  
54번째 줄: 191번째 줄:
 
*[[제어문]]
 
*[[제어문]]
 
*[[분기문]]
 
*[[분기문]]
*[[[CASE]]
+
*[[CASE]]
 
+
*[[else if]]
[[분류: 제어]]
 

2018년 8월 20일 (월) 09:30 기준 최신판

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

1 Bash[편집]

16px-Crystal_Clear_app_xmag.svg.png Bash case 문서를 참고하십시오.
STR=hello

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

# Case hello

2 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;
}

3 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;
}

4 JavaScript[편집]

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

5 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;

6 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'

7 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 { }
}

8 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

9 같이 보기[편집]

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