Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Similar presentations


Presentation on theme: "CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING."— Presentation transcript:

1 CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

2 Branching statements If Switch

3 switch, case, otherwise end switch expression case testval1 statements1 case testval2 statements2 otherwise statementsOther end Let VAL be value of expression. Assume VAL is a scalar double. Does VAL equal testval1 ? Yes: Execute statements1 Jump past end. No: Does VAL equal testval2 ? Yes: Execute statements2 Jump past end No: Execute statementsOther Jump past end Move to code beyond end Any number of cases are allowed. There does not have to be an otherwise (and associated statements).

4 switch, case, otherwise end switch expression case testval1 statements1 case testval2 statements2 otherwise statementsOther end VAL (the value of expression) need not be a scalar double. It can also be a char array. Matlab uses strcmp (string compare) to check equality of VAL to the various testval1, testval2, etc.

5 switch, case, otherwise end switch expression case testval1 statements1 case value2 statements2 otherwise statementsOther end testval can also be cell arrays. The equality of VAL (value of expression ) is tested for any of the contents of the cell array testval.

6 Switch example if x == 1 fprintf('One'); elseif x == 2 fprintf('Two'); elseif x == 3 fprintf('Three'); else fprintf('%d', x); end switch x case 1 fprintf('One'); case 2 fprintf('Two'); case 3 fprintf('Three'); otherwise fprintf('%d', x); end

7 More plots Subplot: putting multiple plots in one window x=0:.1:2*pi; subplot(2,2,1); plot(x,sin(x)); subplot(2,2,2); plot(x,cos(x)); subplot(2,2,3) plot(x,exp(-x)); subplot(2,2,4); plot(peaks);

8 QUESTIONS??

9 Resources “Introduction to Programming with Matlab”, J. Michael Fitzpatrick and John D. Crocetti Lecture slides E77, Andy Packard, http://jagger.me.berkeley.edu/~pack/e77 http://jagger.me.berkeley.edu/~pack/e77


Download ppt "CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING."

Similar presentations


Ads by Google