【正文】
[3] 石博強,[M].北京:.[4]郝紅偉,MATLAB 6,北京,中國電力出版社,2001[5]姜健飛,胡良劍,數(shù)值分析及其MATLAB實驗, 科學出版社,2004[6]薛毅,數(shù)值分析實驗,北京工業(yè)大學出版社,2005翻譯Switch語句在幾個case表達式基礎上的轉(zhuǎn)換。語法:case case_expr statement,...,statement case {case_expr1,case_expr2,case_expr3,...} statement,...,statement... otherwise statement,...,statementend用法討論:switch表達式的語法足一種有條件的執(zhí)行碼。特別地,switch執(zhí)行一系列任意的可供選強的數(shù)字下的語句,每一個可選項組成一個case,由以下3個部分組成:(1) case語句。(2) 一個或多個表達式。(3) 一個或多個語句。在基本語法中,switch執(zhí)行的語句必須滿足switch_expr=case_expr。當case表達式為單元數(shù)組時(如上面的第——個case所示),case_expr只有在單元數(shù)組元中有元素匹配switch表達式時才匹配上。如果沒有case表達式匹配switch表達式,則控制轉(zhuǎn)換到otherwise case(如果該項存在的話)。Case執(zhí)行完后,程序必須從switch末尾開始繼續(xù)執(zhí)行。switch_expr是一個標量或者一個字符串。如果switch_expr=case_expr成立的,標量switch_expr匹配上case_expr。當且僅當strcmp(switch_expr,case_expr)=1(真)時 ,一個字符串switch_expr與case_expr匹配。應用實例:執(zhí)行的列編碼塊基于what the string, method, is set to, methodmethod = 39。Bilinear39。switch lower(method) case {39。linear39。,39。bilinear39。} disp(39。Method is linear39。) case 39。cubic39。 disp(39。Method is cubic39。) case 39。nearest39。 disp(39。Method is nearest39。) otherwise disp(39。Unknown method.39。)endMethod is linear對照英文:Switch among several cases based on expression Syntaxswitch switch_expr case case_expr statement,...,statement case {case_expr1,case_expr2,case_expr3,...} statement,...,statement... otherwise statement,...,statementendDiscussionThe switch statement syntax is a means of conditionally executing code. In particular, switch executes one set of statements selected from an arbitrary number of alternatives. Each alternative is called a case, and consists of The case statement One or more case expressions One or more statements In its basic syntax, switch executes the statements associated with the first case where switch_expr == case_expr. When the case expression is a cell array (as in the second case above), the case_expr matches if any of the elements of the cell array matches the switch expression. If no case expression matches the switch expression, then control passes to the otherwise case (if it exists). After the case is executed, program execution resumes with the statement after the end. The switch_expr can be a scalar or a string. A scalar switch_expr matches a case_expr if switch_expr==case_expr. A string switch_expr matches a case_expr if strcmp(switch_expr,case_expr) returns 1 (true). Note for C Programmers .Unlike the C language switch construct, the MATLAB switch does not fall through. That is, switch executes only the first matching case。 subsequent matching cases do not execute. Therefore, break statements are not used. ExamplesTo execute a certain block of code based on what the string, method, is set to, method = 39。Bilinear39。switch lower(method) case {39。linear39。,39。bilinear39。} disp(39。Method is linear39。) case 39。cubic39。 disp(39。Method is cubic39。) case 39。nearest39。 disp(39。Method is nearest39。) otherwise disp(39。Unknown method.39。)endMethod is linear