50 likes | 204 Views
Introduction to Programming in MATLAB. Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution- NonCommercial - ShareAlike 3.0 Unported License . Based on a work at www.peerinstruction4cs.org . Choosing the most appropriate.
E N D
Introduction to Programming in MATLAB Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Based on a work at www.peerinstruction4cs.org.
Choosing the most appropriate Control Flow
Control Flow • I want to write a function that iterates over the pixels of a matrix to see if there is one that is pure green (0 255 0). If there is, I will return true, otherwise I will return false.Which type of control flow should I use? • if/elseif/else • switch/case • for loop • while loop • Other/none/more than one
Control Flow • I want to write a function that reads input that the user types, while the user types it (we haven’t learned this yet, but it is very easy to do—just a call to a function similar to display, but it reads in addition to writing). We call the read function once for each line of text read, and we want to keep calling the read function repeatedly until we detect that the user typed ‘STOP’.Which type of control flow should I use? • if/elseif/else • switch/case • for loop • while loop • Other/none/more than one
Control Flow • My BakeTime function takes as input one of five different values (‘pizza’, ‘casserole’, ‘bread’, ‘chicken’, ‘cupcakes’). Depending on which it is, we will return a different number of minutes that item should be baked in an oven. Which type of control flow should I use? • if/elseif/else • switch/case • for loop • while loop • Other/none/more than one