Page 23 -
P. 23

A. The simplest form of this structure is:
                                   if expression
                                     commands evaluated if expression is True
                                   else
                                     commands evaluated if expression is False
                                   end

                             NOTES
                                1. The commands between the if and else statements are evaluated
                                   if all elements in the expression are true.
                                2. The conditional expression uses the Boolean logical symbols  &
                                   (and), | (or), and ~ (not) to connect different propositions.


                             Example 1.9
                             Find for integer 0 < a ≤ 10, the values of C, defined as follows:

                                                             ab for  a > 5
                                                        
                                                    C =  3
                                                             ab for  a ≤ 5
                                                         2

                             and b = 15.

                             Solution: Edit and execute the following script M-file:

                                for a=1:10
                                b=15;
                                   if a>5
                                     C(a)=a*b;
                                   else
                                     C(a)=(a*b)*(3/2);
                                   end
                                end

                             Check that the values of C that you obtain by typing C are:

                                22.5 45 67.5 90 112.50 90 105 120 135 150

                                B. When there are three or more alternatives, the if-else-end struc-
                                   ture takes the form:
                                   if expression 1
                                     Commands 1 evaluated if expression 1 is True


                             © 2001 by CRC Press LLC
   18   19   20   21   22   23   24   25   26   27   28