Page 828 - Mechatronics with Experiments
P. 828

814   MECHATRONICS
                              while (condition)
                                   ...
                                   statements
                                   ...
                              end

                                   The “continue” and “break” statements are defined for “for” and “while” loops. The
                              “continue ; ” statement skips the rest of the block and continues the next iteration of the
                              loop in the “for” or “while” loop. The “break ” statement breaks out of the current loop.

                              Decision Blocks:      if and   switch   constructs.

                              if (expression1)
                                  statements1
                              %       execute the first block for which (expressionX) is true
                              %       or non-zero
                                     .  .   .
                              elseif (expression2)
                                  statements2
                                      .  . .
                              elseif (expression3)
                                  statements3
                                  .   . .

                              %  (repeat elseif (exp) as many times as necessary)
                              else
                                  statementsN
                                     .  . .
                              end

                              switch   (switch_expr)
                                case   (case_expr )
                              %  Execute the block for which case_expr = switch_expr
                                            statements
                                             ...
                                case {case_expr1,case_expr2,case_expr3,...}
                                            statements
                                             ...
                                otherwise
                                            statements
                                             ...
                              end

                              Example for “switch” construct,

                              method = ’Bilinear’;

                               switch lower(method)
                                    case {’linear’,’bilinear’}
                                        disp(’Method is linear’)
                                    case ’cubic’
                                        disp(’Method is cubic’)
                                    case ’nearest’
                                       disp(’Method is nearest’)
                                    otherwise
                                       disp(’Unknown method.’)
                              end
   823   824   825   826   827   828   829   830   831   832   833