%clear variable or directive clc;clear;%allows the user to enter parameter disp ('This function exercises the switch statement');d ISP ('enter 1-10 for one of the numbers, and the system determines the parity.'); Count= Input ('Enter a number:');%according to the number of inputsSwitch(count) Case{1,2,3,4,5}, disp ('the number you entered is odd.'); Case{2,4,6,8,Ten}, disp ('the number you entered is even.'); Otherwise disp ('the number you entered is out of range.'); End
It is important to note that:
1. After swith, do not enclose the colon case with {} instead of brackets ()!!!! You can add a comma after a statement
2. The use of break is unclear, the feeling adds and does not add all the same? What's going on
-----------------
%Defining Variables%Input: Count Count%clear variable or directive clc;clear;%allows the user to enter parameter disp ('This function exercises the Try-catch statement');d ISP ('Enter an index and the system takes out the elements in the array.');%initialize an array of arr=[1-3 2-4];Try%displays the corresponding subscript content in the array inder= Input ('Enter a number:'); Content=num2str (arr (inder)); Disp ('the contents of the array are:'); Disp (content);Catch%if error disp ('Try partial Code program error'); End
Note that any error in the try section causes the program to enter the catch section, not just the subscript, but the display error, etc.
MATLAB switch case and try catch usage examples