14-440-127 Lecture 8 Notes

14-440-127 Lecture - 14:440:127– Introduction to Computers for Engineers Notes for Lecture 8 Rutgers University Fall 2008 Instructor Blase E Ur

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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: 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’ ]...
View Full Document

This note was uploaded on 11/09/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Fall '08 term at Rutgers.

Page1 / 8

14-440-127 Lecture - 14:440:127– Introduction to Computers for Engineers Notes for Lecture 8 Rutgers University Fall 2008 Instructor Blase E Ur

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online