Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

if-elseif-else statements Switch-case-otherwise For loops by kaj11697

VIEWS: 18 PAGES: 3

									               COMP319 INTRODUCTION TO MULTI-MEDIA COMPUTING

                                        Lab 3: Execution Control

if-elseif-else statements:
i = 6; j=21;
if i > 5
   k=i;
elseif (i>1) & (j==20)
   k = 5*i+j;
else
    k=1;
end


Switch-case-otherwise:
color = input(‘color = ‘ ,’s’)
switch color
case ‘red’
    c = [1 0 0]
case ‘green’
   c = [ 0 1 0]
case ‘blue’
   c = [0 0 1]
otherwise
     error(‘invalid choice of color’)
end


For loops: repeat a statement for a fixed number of times
for m=1:100
    num = 1/ (m+1)
end


for n=100:-2:0, k = 1/(exp(n)), end




                                               Page 1 of 3
While loops: execute a statement for an indefinite number of times until the
condition is longer satisfied.
% let us find all powers of 2 below 10000
v = 1; num = 1; i=1;
while num < 10000
       num = 2^i;
       v = [v; num];
       i = i +1;
end




Break
x= exp(sqrt(163));
while 1
  n = input(‘Enter max. number of iterations ‘)
  if n <=0
     break;      %terminate loop execution
  end
  for i = 1:n
        x = log(x); %do something
  end
end




                                                  Page 2 of 3
Exercise:
1.    Write a script that uses input(…) to request a numerical grade in percentage and uses if
     statements to convert that grade to a letter grade according to the following table:

90% and better: A
80%-90%: B
70%-80%: C
60%-70%: D
Below 60%: F

Test your script by running it repeatedly for legal and illegal values of the grade percentage.

Repeat the exercise using switch statement

2. *Write a script file named sineseries.m that computes the value of sin(x) at a given x using n
   terms of the series expansion of sine function:
                                                       ����
                                     ���� 3 ���� 5                           ���� 2����−1
                        sin ���� = ���� − + − ⋯ =                (−1)����−1
                                     3! 5!                              2���� − 1 !
                                                      ����=1

Follow the steps given below.

    First, query MATLAB to see if the name ‘sineseries’ is already taken by some variable or
     function with the command exist(sineseries). What does the MATLAB response mean?
     [Hint see on-line help on exist. i.e. help exist]
    Include the following line as the header (H1 line) of your script file.
     %SINESERIES: computes sin(x) from series expansion
     Now code the formula so that it computes the sum of the series for a given scalar x and a
     given integer n.
    Save the file. Type help sineseries to see if MATLAB can access your script file. Now,
     compute sin(pi/6) with n = 1,5,10, and 20. Compute the results. Do the same for some other
     x of your choice.




                                             Page 3 of 3

								
To top