Thursday, 21 July 2016

MATLAB Programming 33 - switch case statement (Example)

Example


Create a script file and type the following code in it:


grade = 'B';

switch(grade)

case 'A'

fprintf('Excellent!\n' );

case 'B'

fprintf('Well done\n' );

case 'C'

fprintf('Well done\n' );

case 'D'

fprintf('You passed\n' );

case 'F'

fprintf('Better try again\n' );

otherwise

fprintf('Invalid grade\n' );

end


When you run the file, it displays:



Well done

Your grade is B

No comments:

Post a Comment