ForNext
Only Do What Only You Can Do
156. switch
Hyperpolyglot Interpreted Languages: PHP, Perl, Python, Ruby より (2012.05.08)
VBScript
JScript
Perl
use feature 'switch'; given ($n) { when (0) { print "no hits\n"; } when (1) { print "one hit\n"; } default { print "$n hits\n"; } }
PHP
switch ($n) { case 0: echo "no hits\n"; break; case 1: echo "one hit\n"; break; default: echo "$n hits\n"; }
Python
none
Ruby
case n when 0 puts "no hits" when 1 puts "one hit" else puts "#{n} hits" end