Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Section 4.2 Control Structures <strong>in</strong> <strong>Matlab</strong> 305<br />
However, <strong>in</strong>stead <strong>of</strong> us<strong>in</strong>g an if ...elseif...else...end control structure to select a<br />
block <strong>of</strong> statments to execute based on <strong>the</strong> user’s choice <strong>of</strong> menu item, we will<br />
implement a switch structure <strong>in</strong>stead.<br />
switch n<br />
case 1<br />
fpr<strong>in</strong>tf(’The sum <strong>of</strong> %.2f and %.2f is %.2f.\n’,a,b,a+b)<br />
case 2<br />
fpr<strong>in</strong>tf(’The difference <strong>of</strong> %.2f and %.2f is %.2f.\n’,a,b,a-b)<br />
case 3<br />
fpr<strong>in</strong>tf(’The product <strong>of</strong> %.2f and %.2f is %.2f.\n’,a,b,a*b)<br />
case 4<br />
fpr<strong>in</strong>tf(’The quotient <strong>of</strong> %.2f and %.2f is %.2f.\n’,a,b,a/b)<br />
o<strong>the</strong>rwise<br />
fpr<strong>in</strong>tf(’Not a valid choice.\n’)<br />
end<br />
Save <strong>the</strong> script as mymenuswitch.m, change to <strong>the</strong> command w<strong>in</strong>dow, and enter<br />
and execute <strong>the</strong> command mymenuswitch at <strong>the</strong> command prompt. Note that<br />
<strong>the</strong> behavior <strong>of</strong> <strong>the</strong> program mymenuswitch is identical to that <strong>of</strong> mymenu.<br />
Enter a number a: 15.637<br />
Enter a number b: 28.4<br />
1) Add a and b.<br />
2) Subtract b from a.<br />
3) Multiply a and b.<br />
4) Divide a by b.<br />
Enter your choice: 1<br />
The sum <strong>of</strong> 15.64 and 28.40 is 44.04.<br />
If <strong>the</strong> user enters an <strong>in</strong>valid choice, note how control is passed to <strong>the</strong> statment<br />
follow<strong>in</strong>g o<strong>the</strong>rwise.