1 / 6

Understanding Conditional Statements in MATLAB Programming

This lecture on conditional statements in MATLAB by Dr. Cynthia Lee from UCSD covers key concepts essential for programming, such as the syntax and function of `if`, `elseif`, and `else` statements. It provides practical examples for checking age-related conditions, including driving eligibility and voting rights. Learners will also review problem-solving exercises that test their understanding of function definitions and return values, enhancing their practical skills in MATLAB programming. This resource is designed for beginner to intermediate learners seeking to deepen their knowledge.

rae-mccarty
Télécharger la présentation

Understanding Conditional Statements in MATLAB Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 www.peerinstruction4cs.org.

  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'); 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

  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

  5. 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

  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 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

More Related