ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.

Slides:



Advertisements
Similar presentations
MATLAB Conditional Statements ENGR 1181 Presentation by Annie Abell.
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
MATLAB Loops and Branching.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Conditional Statements ENGR 1187 MATLAB 7. Conditional Statements in Real Life A Fluorescence Activated Cell Sorter (FACS) sorts cells based on predetermined.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Decision Making Selection structures (if....else and switch/case)
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Introduction to Engineering MATLAB – 7 Script Files - 2 Agenda Script files continued.
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 2 in MATLAB Topics Covered: 1.More about 2-D plots  Placing multiple.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Computer Application in Engineering Design
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter 4 MATLAB Programming
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
More Selections BIS1523 – Lecture 9.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Conditional Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 3: Selection Structures: Making Decisions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Using Script Files and Managing Data
Chapter 3: Selection Structures: Making Decisions
Exam # 1 INFORMATION Scheduled for Thursday 7/20
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements. if–end if-else–end if-elseif-else–end 2.switch-case Programming in MATLAB / Chapter 6

ENG 1181 P. 2 CONDITIONAL STATEMENTS  Conditional statements enable MATLAB to make decisions.  The process is similar to the way we (humans) make decisions.  A condition stated. If the condition is met, one set of actions is taken. If the condition is not met, either nothing is done, or a second set of actions is taken. Example: If I win the Lottery, I will quit college, buy a new car, and go fishing. If I do not win the Lottery, I will study harder so that I can get a better job

ENG 1181 P. 3 THE FORM OF A CONDITIONAL STATEMENT if a < b if c >= 5 if a == b if a ~= 0 if (d 7) if (x ~= 13) | (y < 0) if Conditional expression consisting of relational and/or logical operators Examples: All variables must have assigned values. 174

ENG 1181 P. 4 FORMS OF THE if STATEMENT if conditional statement 1 command group 1 elseif conditional statement 2 command group 2 … (Optional) elseif conditional statement # command group # … (Optional) else %no conditional statement command group n+1 end if conditional statement command group 1 else command group 2 end if conditional statement command group end

ENG 1181 P. 5 THE if–elseif-else-end STATEMENT if conditional expression elseif conditional expression else end Group 1 of MATLAB commands. MATLAB program. Group 2 of MATLAB commands. Group 3 of MATLAB commands. Examples of if-end and if-else-end can be found on pages of the text MATLAB program.

ENG 1181 P. 6 Bill less than $10 Tip is $1.80 Bill between $10 and $60 Tip is %18 Bill above $60 Tip is %20 USING THE if–elseif-else-end STATEMENT Write program that calculates tip based on amount of bill, using the following rules

ENG 1181 P. 7 USING THE if–elseif-else-end STATEMENT % A script file that demonstrates the use of the % if-elseif-else-end statement. % The program calculates the tip in a restaurant % according to the amount of the bill. % % If the bill is less than $10 the tip is $1.80. % Between $10 and $60 the tip is 18% of the bill. % Above $60 the tip is 20% of the bill. format bank (The file continues on the next slide)

ENG 1181 P. 8 bill = input('Enter the amount of the bill (in dollars): '); if (bill <= 10) tip = 1.8; elseif (bill > 10) & (bill <= 60) tip = bill*0.18; else tip = bill*0.2; end disp('The tip is (in dollars):') disp(tip) USING THE if–elseif-else-end STATEMENT Note how tip’s value is control by ; and the disp command

ENG 1181 P. 9 >> Lecture8Example3 Enter the amount of the bill (in dollars): 15 The tip is (in dollars): 2.70 >> Lecture8Example3 Enter the amount of the bill (in dollars): 6 The tip is (in dollars): 1.80 >> Lecture8Example3 Enter the amount of the bill (in dollars): 100 The tip is (in dollars): USING THE if–elseif-else-end STATEMENT

ENG 1181 P. 10 COMMENTS ABOUT if–end STATEMENTS  For every if command must have an end command.  A program can have many if … end statements following each other.  A computer program can perform the same task using different combinations of if - end, if – else – end, and if– elseif – else – end statements.  Multiple elseif conditions are allowed within an if– elseif – else – end statement.  An else condition is not required.  When else is used, a conditional statement is NOT added

ENG 1181 P. 11 THE switch-case STATEMENT  switch-case is similar to if- elseif-end, except must be an exact match. You cannot use switch-case with a range, such as <0.  switch on scalar or string  otherwise == else (optional)

ENG 1181 P. 12 THE switch-case STATEMENT x = input (‘Resistance: ‘); switch x case 100 out= x; case 200 out= 2*x; case 300 out= 3*x; case 400 out= 4*x; otherwise out = 0; end

ENG 1181 P. 13 THE switch-case STATEMENT x = input (‘Purchase Class: ‘,’s’); x = upper(x) %change to upper case switch x case ‘A’ rate = 2.00; case ‘B’ rate = 3.00; case ‘C’ rate = 3.50; case ‘D’ rate = 4.00; otherwise rate = 0; end