假设我有一个如下的switch语句
switch(alphabet) {
case "f":
//do something
break;
case "c":
//do something
break;
case "a":
//do something
break;
case "e":
//do something
break;
}
Run Code Online (Sandbox Code Playgroud)
现在假设我知道Alphabete 的频率最高,然后分别是a,c和f.所以,我只是重新构建了case语句顺序,并按如下方式进行了修改:
switch(alphabet) {
case "e":
//do something
break;
case "a":
//do something
break;
case "c":
//do something
break;
case "f":
//do something
break;
}
Run Code Online (Sandbox Code Playgroud)
第二个switch陈述会比第一个switch陈述更快吗?如果是的话,如果在我的计划中,我需要switch多次称这个声明,这会是一个实质性的改进吗?或者,如果没有,我如何使用我的频率知识来提高性能?
switch ($i) {
case A:
$letter = 'first';
break;
case B:
$letter = 'first';
break;
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}
Run Code Online (Sandbox Code Playgroud)
有没有办法缩短前三个案例?
它们内部具有相同的值.
我正在使用这样的东西:
case referer
when (referer.include? "some_string")
redirect_link = edit_product_path
when (referer.include? "some_other_string")
redirect_link = other_product_path
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,即使字符串some_string存在于变量中,它也会返回nil referer.
这是我在Ruby控制台中尝试的内容:
ruby-1.8.7-p334 :006 > jasdeep = "RAILS"
ruby-1.8.7-p334 :026 > case jasdeep
ruby-1.8.7-p334 :027?> when (jasdeep.include? "AI")
ruby-1.8.7-p334 :028?> puts "Hello"
ruby-1.8.7-p334 :029?> end
=> nil
Run Code Online (Sandbox Code Playgroud)
任何输入将不胜感激.
switch ($var) {
case 0:
// Do something...
break;
case 1:
// Do something...
break;
default:
// Do something...
break;
}
Run Code Online (Sandbox Code Playgroud)
我见过有些人在默认情况下使用break.由于默认情况是触发时执行的最后一种情况,是否需要在那里休息?我猜它只是出于常规做法,还是有另一个原因?
g ++ 4.9.0接受以下代码:
enum E { foo };
struct C {
operator E() const { return foo; }
operator E() { return foo; }
};
int main() {
C c;
switch (c) {
case foo: break;
}
}
Run Code Online (Sandbox Code Playgroud)
但是clang 3.4.1通过以下诊断拒绝它:
12 : error: multiple conversions from switch condition type 'C' to an integral or enumeration type
switch (c)
^ ~
5 : note: conversion to enumeration type 'E'
operator E() const { return foo; }
^
6 : note: conversion …Run Code Online (Sandbox Code Playgroud) c++ switch-statement language-lawyer implicit-conversion c++11
假设这个示例代码(源代码):
#include <stdio.h>
void playgame()
{
printf( "Play game called" );
}
void loadgame()
{
printf( "Load game called" );
}
void playmultiplayer()
{
printf( "Play multiplayer game called" );
}
int main()
{
int input;
printf( "1. Play game\n" );
printf( "2. Load game\n" );
printf( "3. Play multiplayer\n" );
printf( "4. Exit\n" );
printf( "Selection: " );
scanf( "%d", &input );
switch ( input ) {
case 1: /* Note the colon, not a semicolon */ …Run Code Online (Sandbox Code Playgroud) 作为 Java SE 12 的一部分,引入了switch表达式,并且从 Java SE 14 开始,它们已经标准化。它们与switch陈述有何不同?
语法太多,语法相同:
switch (someValue) {
case OPTION_ONE:
case OPTION_LIKE_ONE:
case OPTION_ONE_SIMILAR:
doSomeStuff1();
break; // EXIT the switch
case OPTION_TWO_WITH_PRE_ACTION:
doPreActionStuff2();
// the default is to CONTINUE to next case
case OPTION_TWO:
doSomeStuff2();
break; // EXIT the switch
case OPTION_THREE:
doSomeStuff3();
break; // EXIT the switch
}
Run Code Online (Sandbox Code Playgroud)
现在所有人都知道break语句是必需的,因为当语句丢失时,语句switch将继续到下一个语句.我们有一个例子,和.问题是我们只需要非常非常非常需要"跳到下一个案例".而且我们经常在结束时放下休息时间.casebreakOPTION_LIKE_ONEOPTION_ONE_SIMILAROPTION_TWO_WITH_PRE_ACTIONcase
初学者很容易忘记它.我的一位C老师甚至向我们解释过,好像这是C语言中的一个错误(不想谈论:)
我想问一下,我是否还有其他任何语言,我不知道(或忘记)处理这样的开关/案例:
switch (someValue) {
case OPTION_ONE: continue; // CONTINUE to next case
case OPTION_LIKE_ONE: continue; // CONTINUE to next case
case OPTION_ONE_SIMILAR:
doSomeStuff1(); …Run Code Online (Sandbox Code Playgroud) c java language-agnostic programming-languages switch-statement
可能重复:
为什么不能在switch语句中声明变量?
我的代码中有一个奇怪的错误:
char choice=Getchar();
switch(choice)
{
case 's':
cout<<" display tree ";
thetree->displaytree();
break;
case 'i':
cout<<" enter value to insert "<<endl;
cin>>value;
thetree->insert(value);
break;
case 'f' :
cout<< "enter value to find ";
cin>>value;
int found=thetree->find(value);
if(found!=-1)
cout<<" found = "<<value<<endl;
else
cout<< " not found " <<value <<endl;
break;
default:
cout <<" invalid entry "<<endl;;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2010编译器说:
1>c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(317): error C2361: initialization of 'found' is skipped by 'default' label
1> c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(308) : …Run Code Online (Sandbox Code Playgroud) 无法编译:
void test(Integer x) {
switch (x) {
case 'a':
}
}
Run Code Online (Sandbox Code Playgroud)
编译OK:
void test(Byte x) {
switch(x) {
case 'a':
}
}
Run Code Online (Sandbox Code Playgroud)