Lecture 13 Decision structures

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Lecture 4 Structure plan Program design process Program design process Basic elements of code: Basic elements of code: –Input (assignment of variables)
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Lecture 8 Logical Operations Logical functions & Relational operators © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Basic Control Structures
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Agenda Basic Logic Purpose if statement if / else statement
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
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.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Week 4 Program Control Structure
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
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.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Controlling Program Flow with Decision Structures.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
1 Structured Programming EEN170 Programming in MATLAB.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Computer Science Up Down Controls, Decisions and Random Numbers.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 4 MATLAB Programming
Operator Precedence Operators Precedence Parentheses () unary
A computer program is a sequence of computer commands.
MATLAB: Structures and File I/O
Microsoft Visual Basic 2005 BASICS
Conditional Statements
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Logical Operations In Matlab.
Visual Basic – Decision Statements
Selection Statements.
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Conditional Statements
Week 3 – Program Control Structure
Life is Full of Alternatives
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Basic Concepts of Algorithm
Matlab Basics.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Lecture 13 Decision structures if construct, else statement & elseif statement. © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Uses of the if construct The if-end decision construct is useful when relational operations are used to decide whether or not to execute a sequence of commands. You can use them for displaying messages. You can use them to prevent undesired operations, e.g., you may wish to skip the log(x) command for x≤0. (Of course, since MATLAB deals with complex numbers, the log of a negative number is a legitimate mathematical operation.)

The if construct The basic if construct has the form: if comparison action end When the comparison is true, it moves on to the action. When the comparison is false, the program skips to the end statement.

if constructs with arrays When the comparison includes an array, all elements are compared. An if construct requires all elements to be true. Since some elements of g are greater than 50 the statement is false so it skips to end. There is no output.

else The else command is another element in an if construct. When the if comparison is false, the commands after else are executed. Note: MATLAB has a function to make the computer beep. You may use this as an error notice. You need to have your volume up to hear it.

elseif There can be any number of elseif clauses, however there may only be one else clause. elseif is useful when you have multiple comparisons to test. if grade>=90 fprintf('A\n'); elseif grade>=80 fprintf('B\n'); elseif grade>=70 fprintf('C\n'); elseif grade>=65 fprintf('D\n'); else fprintf('F\n'); end Note that the first elseif argument that is true will be executed.

Hands on Write a function to determine if the length of a part is 10 cm with a 2% tolerance. In the previous problem write a function to permit input in inches. Note that 1 inch = 2.54 cm. Write a program to ask for input of a temperature. If the temperature is > 25ºC, print ‘fan on’, otherwise print ‘fan off’. In previous problem ask for input in Fahrenheit.

Summary Decision making The if construct if logical operation (i.e., comparison) possible action one elseif logical operation possible action two else possible action three end