Download presentation
Presentation is loading. Please wait.
1
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
2
Conditional Statements
Note: display(arg) prints the value of arg back to you
3
Conditional Statements
function [ ] = PrintAgeInfo( age ) if (age >= 16) display('Can drive'); end if (age >= 18) display('Can vote'); if (age >= 21) display('Can drink'); if (age >= 65) display('Can collect Social Security'); How many strings are printed when we type PrintAgeInfo(30) in the Command Window? (a) (b) (c) (d) (e) 4 D Prints “can drive” “can vote” and “can drink”
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 When we type PrintAgeInfo(30) in the Command Window, is ‘Can vote’ printed? (a) yes (b) no (c) I don’t know/Other B – no. Prints “can drive” but then goes to the end of the conditional statement without checking other tests, because one test has already been True.
5
Conditional Statements
function [ ] = HelloPuzzle( x ) if (x >= 3) display(‘Hello!'); else display(‘Hello!’); if (x > 1) end How many strings are printed when we type HelloPuzzule(5) in the Command Window? (a) (b) (c) (d) (e) I don’t know/Other B 5 >= 3 so that test passes and “Hello!” is printed. Even though the second condition in the code (5>1) is true, we do not even run that test because it is in the else clause of the first test, and is thus skipped. This is a really hard concept for students.
6
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 function [y] =IsDrivingAge(age) if (age >= 16) display(‘true’); y = true; else display(‘false’); y = false; end This is really not an if/else question, it is a test to see if they understand the difference between displaying something and returning it. This is a REALLY common misunderstanding. C This displays the answer, does not return it This displays and returns. It is better than (a), but, generally, when you are given a function specification you should follow it exactly and not display things to the screen unless told to do so. Correct—returns the desired value NOT personal preference! (perhaps some leeway between B and C, but A is wrong) A function [y] =IsDrivingAge(age) if (age >= 16) y = true; else y = false; end B Any of these is fine, it is just personal preference. D C
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.