70 likes | 188 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. Conditional Statements.
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.
Conditional Statements • Note: display(arg) prints the value of arg back to you
Conditional Statements function [ ] = PrintAgeInfo( age ) if (age >= 16) display('Can drive'); end if (age >= 18) display('Can vote'); end if (age >= 21) display('Can drink'); end if (age >= 65) display('Can collect Social Security'); end end How many strings are printed when we type PrintAgeInfo(30) in the Command Window? (a) 0 (b) 1 (c) 2 (d) 3 (e) 4
Conditional Statements function [ ] = PrintAgeInfo( age ) if (age >= 16) display('Can drive'); elseif (age >= 18) display('Can vote'); elseif (age >= 21) display('Can drink'); elseif (age >= 65) display('Can collect Social Security'); end end When we type PrintAgeInfo(30) in the Command Window, is ‘Can vote’ printed? (a) yes (b) no (c) I don’t know/Other
Conditional Statements function [ ] = HelloPuzzle( x ) if (x >= 3) display(‘Hello!'); else display(‘Hello!’); if (x > 1) display(‘Hello!'); end end end How many strings are printed when we type HelloPuzzule(5) in the Command Window? (a) 0 (b) 1 (c) 2 (d) 3 (e) I don’t know/Other
Review/check-up You get a job at the DMV and your boss asks you to write a function IsDrivingAge as follows: (1) takes one argument, a number giving the age in years, and (2) returns true if the age is 16 or more and false otherwise. Which of the following codes fulfills this request? function [] =IsDrivingAge(age) if (age >= 16) display(‘true’); else display(‘false’); end end function [y] =IsDrivingAge(age) if (age >= 16) display(‘true’); y = true; else display(‘false’); y = false; end end A function [y] =IsDrivingAge(age) if (age >= 16) y = true; else y = false; end end B Any of these is fine, it is just personal preference. D C