今天對于老師講的一個問題。始終有點疑惑
一個對于 switch與 if else的問題。
switch 比if else的執(zhí)行效率高。感覺可能是因為switch 執(zhí)行不向 if else 需要順序執(zhí)行。但是他也有限制,就是 swtich 與case的每個標簽必須是整形(包括字符型)的。所有對于 浮點型或是選擇涉及到取值范圍,就無法處理了。。。
比如一道簡單的題目:
假設(shè) a,x為整形;
a>10 x=1;
5<=a<=10, x=2;
0<=a <5,X=3;
a<0, x=4;
if(a>10)
x=1;
else
if (a>=5)
x=2;
else
if(a>=0)
x=3;
else
x=4;
這種選擇分支是不是一定無法轉(zhuǎn)為 switch case 結(jié)構(gòu)呢?
我覺得是。。。。