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 am doing project "Recognizing people in video based on their body" the system will use MATLAB to detect people based on thier body and then recognize them from their clothes "us

Hold and legend function: hold: is a toggle which freezes the present graph in the figure window, so that the new plots will be superimposed on the present one. Just hold

Problem 1:   What are the different methods that can be used to solve a differential equation? Please provide a numbered list. Problem 2:   For a first order linear d

Explain how the wavelet transform based on the real biorthogonal "decomposition" wavelet bior 1.5 (the wavelet shape by typing waveinfo ('bior') at the MATLAB command prompt) can

In many applications, including ?nancial mathematics, ?nding zeros of a function f(x) = 0 (4) is paramount. One of the simplest method is the Bisection Method. The bisection

Operators: There are in general two types of operators: unary operators that operate on a single value or operand; and binary operators, that operate on two values or operands

Write a function called threshold2 (in a file called 'threshold2.m'). The function takes an arbitrary number of input variables. The first input variable, t, is required and is the


The Format Function and ellipsis: The default in MATLAB is to show numbers which have decimal places with four decimal places. The format command can be used to identify the o