{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14-440-127 Lecture 8 Notes

# 14-440-127 Lecture 8 Notes - 14:440:127 Introduction to...

This preview shows pages 1–4. Sign up to view the full content.

14:440:127– Introduction to Computers for Engineers Notes for Lecture 8 Rutgers University, Fall 2008 Instructor- Blase E. Ur Remember: Problem Set 2 due on 10/27, Exam 2 given 11/5-11/11, and Project 2 assigned 10/24 (+/- a few days) and due 11/9. 1 Switch/Case Switch case (also known as select case) is an alternative to If Statements when you have a variable that only takes on a certain number of discrete values. In all honesty, you never need to use a switch case statement if you know if statements well, but you should recognize the statement when reading other programmers’ code. It’s sometimes easier to read a switch case statement than an if statement if you’re dealing with character strings. The syntax is as follows: switch VARIABLE case OPTION1 STATEMENTS case OPTION2 STATEMENTS otherwise STATEMENTS end You must replace ”VARIABLE” with the name of the variable you wish to test. Replace OP- TION1, OPTION2, etc. with values (you don’t need to put x==5, you JUST type 5). If you want to include more than one value, put all of the possibilities in a vector! Matlab will look up the value of the variable that follows the word ”switch,” and go down the list of possible cases until it finds one that is true (and executes the corresponding statements). Once it finds one that is true, it doesn’t even look at any of the others. The optional ”OTHERWISE” statement is entirely equivalent to ”else” in an if statement – if you get down to the otherwise statement, you will execute those statements. Here’s an example of switch case in action. Note that ”exciting” will be displayed:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
destination= ’ethiopia’; switch destination case ’florida’ disp(’boring!’) case ’france’ disp(’a little less boring’) case {’ethiopia’ , ’eritrea’ } disp(’exciting’) otherwise disp(’i don’’t know about that country’) end 1.1 Menus It’s common to use switch case to create menus to get user input, rather than using the input statement. The example below creates a menu and then uses switch case to identify which option was chosen. Note that you must set the menu command equal to a variable, just as you did with the input statement: food = menu(’What does your mom say when she gets this menu?’, ’Cheeseburger’,’Steak’,’Potatoes’,’Italian Ice’,’Ok’); switch food case 1 disp(’She ordered a cheeseburger’) case 2 disp(’She ordered steak’) case 3 disp(’She ordered potatoes’) case 4 disp(’She ordered italian ice’) case 5 disp(’Yo momma’’s so fat, she went to the restaurant...’) disp(’...looked at the menu... and said OK’); end 2 Cell Arrays You may have noticed in the ’florida’,’france’,’ethiopia’ example above that when we created a vector of the strings ’ethiopia’ and ’eritrea’, we used squigly braces. You may recall from the first project that this is a cell array . Well, what is a cell array and why do we use it here? First, let’s look at what happens when we put strings in a normal array
>> x = [ ’one’ ; ’two’ ] x = one two

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}