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

108번째 줄: 108번째 줄:
else
else
     puts "default"
     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
end
# world
# world

2014년 6월 1일 (일) 00:47 판

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

1 Bash

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

7 같이 보기

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