Illustration of switch statement, MATLAB Programming

Illustration of switch statement:

Here are the two illustrations of calling this function:

>> quiz = 22;

>> lg = switchletgrade(quiz)

lg =

X

>> quiz = 9;

>> switchletgrade(quiz)

ans =

A

Note that it is supposed that the user will enter an integer value. When the user does not, either an error message will be printed or a wrong result will be returned. As the similar action of printing 'A' is desired for more than one situation, these can be combined as shown below:

switch quiz

    case {10,9}

      grade = 'A';

    case 8

      grade = 'B';

     % etc.

(The curly braces around the case expressions 10 & 9 are essential.)

In this illustration, we checked the error first using an if-else statement, and then if the grade was in the valid range, used the switch statement to find the corresponding letter grade.

At times the otherwise clause is used rather than for the error message. For illustration, if the user is assumed to enter only a 1, 3, or 5, the script may be organized as shown below:

2081_Illustration of switch statement.png

In this situation, actions are taken if the user properly enters one of the valid options. When the user does not, the otherwise clause handles, by printing an error message.

Note that the use of two single quotes within the string to print one.

>> switcherror

Enter a 1, 3, or 5: 4

Posted Date: 10/19/2012 3:25:28 AM | Location : United States







Related Discussions:- Illustration of switch statement, Assignment Help, Ask Question on Illustration of switch statement, Get Answer, Expert's Help, Illustration of switch statement Discussions

Write discussion on Illustration of switch statement
Your posts are moderated
Related Questions
This project carries 50% of your ?nal mark. Please hand in your work to the Mathematical and Physical Sciences School O?ce, no later than 4pm Monday 21st January 2013. Please ?l

Creating matrix variables- arguments: The CAT arguments dimensions are not reliable. The Iterators can also be used for the values on the rows by using the colon operator;

Appending data to a data File: A text file once exists; the data can be appended to it. The format is similar as formerly, with the addition of the qualifier -append. For illu

if i have a 3x2 double.. how can i run a for loop for this matrix so tht it can subtracted values of he adjacent neighbors?



How to Open Simpower: 1. Open MATLAB 2. In the "Command Window" write ' simulink ' and press Enter or click on the Simulink icon. 3. Simulink Library Browser w

Illustration of a conditional loop - While loop: As an illustration of a conditional loop, we will write a function which will find the first factorial which is greater than t

Hello! How to cipher on Matlab the message consisting eight symbols. At first the message shares on blocks. Then in each of blocks shift of symbols is carried out. The initial text

Adaptive filters can also be used in other applications. Speech recognition, for example, is performed in a non-stationary environment, and therefore may require adaptive filtering