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

i want to write a program in mat lab for optimizing the design of lens help me how could i i am new user of matlab but previously i have work on C++.

2)''dbcont'' command for debugging is used to Select one: a. Continue normal code execution from the debug prompt. b. List all breakpoints. c. Execute (step) one or more lines

The Efficient Method: Though, in MATLAB, there is another built-in function which specifically produces random integers, i.e. randint. Calling the function with randint (1,1,n

Expressions: The Expressions can be formed using values, variables which have already been formed, operators, parentheses, and built-in functions. For numbers, these can invol

Use an expression for the far-field radiation from a plane circular piston to create a more accurate model of a column loudspeaker system - you will need to calculate individual so

The mass of the spring-mass-damper system shown below was initially displaced by 1 meter to the right and then released. The subsequent acceleration of the mass was measured using

Function definitions: There are various ways to organize the scripts and functions, but for now every function which we write will be stored in a separate M-file, that is why

It is desired to determine the DFT of the analogue signal x (t) = 25 cos(10 Πt)cos(400Πt). (a) Find the Nyquist rate of the signal.   (b) What is the record (signal) length i

write a computer program that will provide a user with the equilibrium concentration of vacancies in a metallic element as a function of temperature.Temperature range should be men