[初学笔记] matlab中 switch和if 语句的区别和效率问题


因为写代码时候,发现这两个语句可以相互混着使用。然后搜了一下,这个blog总结的很好

http://www.cnblogs.com/pangchunlei/p/5420491.html


1 switch和if语句 的 使用的区别

下面是直接粘贴过来的

总结:switch语句和if语句的区别:

     

    ● 大于等于(>=)、小于等于(<=)的判断用if语句,而等于(=)的判断用switch语句。

    ● switch语句中的case类似于if…else…else if…else,但是离散值的判断。

       (离散值的判断自认为是等于情况的判断)。

    ● switch一般都可以及用if重写,但是if不一定能用switch重写。

    ●不要忘了break.C#中break不写是不行的,除了合并case的情况。

    ● case 中的值必须是常量,不能是变量、表达式。



2 switch和if语句 中是否加break的问题

(1)首先,if语句是可以不加break的,一般在for循环或者在while循环里面,会通过加入if语句的判断,从而进行break

比如下面这段代码,是我自己写的,是一个function里面的其中一段代码,在while循环里面加入if判断进行break,避免出现死循环,当然break语句前要加上输出命令,如果不赋值,那么即使break跳出,也会没有output


         while ((judcig1 == 0) || (cig2 == 1))
                fprintf('\n\n Error! Invalid input!\n\nPlease enter ''f'' for female, ''m'' for male, ''x'' for third sex.\r\n');
                % error report
                        if gbegin == 1 % judge the input of you       
                           gender_ = input ('\n\n please enter your gender. (f/m/x)\n\n','s');
                        elseif gbegin == 2 % judge the input of dreamy
                           gender_ = input ('\n\n please enter dreamy''s gender. (f/m/x)\n\n','s');
                        end
                cig2 = isempty(gender_); % if it is an empty input
                cig1 = strcmp(gender_,s1); % compare the input and the right answer
                judcig1 = any(cig1); % use any to test is it any "1" in the answer
                        if (judcig1 == 1) % until the any is "1", end this loop
                            ansgend = gender_; %%% 原来在这里break的时候,ansgend 没有赋值,所以ingend函数没有返回值,所以出错。
                            break; % break the while loop
                        end
                ansgend = gender_; % save the right answer        
            end % end, when it's the correct answer



(2)但是在switch语句里面就不是这样的了

switch语句中加break,是为了提高运算的效率问题

下面是从这个百度问里面摘的https://zhidao.baidu.com/question/542504321.html

switch起到的作用类似于跳转,满足switch的条件会跳转到对应的case下执行代码。

如果不加break,代码会从那开始执行,一直执行到最后,所以不符合case的语句也会被执行。


那么,如果要改,就相当于在每一个case语句后面加上一个break;,就可以了,提高效率

比如下面这行代码,依然是在一个function文件里面的,是我自己的代码


                            switch(inpn) % 可以用 if 判断来代替 switch语句,但是貌似 switch语句的效率会更高
                                case 1 % your name    
                                   inpc_ = input ('\n\n please enter your name or nickname.\n\n','s');
                                   break;
                                case 2 % your country
                                   inpc_ = input ('\n\n Where are you from? (country name)\n\n','s');
                                   break;
                                case 3 % your age
                                   inpc_ = input ('\n\n please enter your age\n\n','s'); 
                                   break;
                                case 4 % your major
                                   inpc_ =  input ('\n\n What''s your major?\n\n','s');
                                   break;
                                case 5 % dreamy's name
                                   inpc_ =  input ('\n\n please enter dreamy''s name or nickname.\n\n','s');
                                   break;
                                case 6 % dreamy's country                              
                                   inpc_ = input ('\n\n Where is he/she from? (country name)\n\n','s');
                                   break;
                                case 7 % dreamy's age
                                   inpc_ = input ('\n\n please enter dreamy''s age\n\n','s');
                                   break;
                                otherwise % dreamy's major
                                   inpc_ = input ('\n\n dreamy''s major?\n\n','s');
                                   break;
                            end






你可能感兴趣的:(Matlab学习,switch,if,break)