Page 36 - Applied Numerical Methods Using MATLAB
P. 36

BASIC OPERATIONS OF MATLAB  25
            Table 1.4 Relational Operators and Logical Operators
             Relational               Relational                 Logical
                          Remark                    Remark              Remark
             operator                 operator                   operator
                <    less than           >    greater than         &      and
               <=    less than or equal to  >=  greater than or equal to  |  or

               ==    equal              ~=    not equal(=)         ~      not


              Let us see the following examples:
            Example 1. A Simple if-else-end Block

             %nm119_1: example of if-end block
             t=0;
             ift>0
                sgnt = 1;
              else
                sgnt = -1;
             end

            Example 2. A Simple if-elseif-end Block

             %nm119_2: example of if-elseif-end block
             ift>0
              sgnt = 1
              elseift<0
                sgnt = -1
             end

            Example 3. An if-elseif-else-end Block

             %nm119_3: example of if-elseif-else-end block
             ift>0, sgnt = 1
              elseif t<0, sgnt = -1
              else   sgnt = 0
             end

            Example 4. An if-elseif-elseif-..-else-end Block

             %nm119_4: example of if-elseif-elseif-else-end block
             point = 85;
             if point >= 90, grade = ’A’
               elseif point >= 80, grade = ’B’
               elseif point >= 70, grade = ’C’
               elseif point >= 60, grade = ’D’
               else               grade = ’F’
             end
   31   32   33   34   35   36   37   38   39   40   41