Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Advertisements

CS&E 1111 ExIFs IFs and Nested IFs in Excel Objectives: Using the If function in spreadsheets Nesting If functions.
Flow Charts, Loop Structures
Selection (decision) control structure Learning objective
 Control structures  Algorithm & flowchart  If statements  While statements.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Programming with MATLAB
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Lecture 13 Decision structures
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Linear Simultaneous Equations
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Image Slides.
Non-Linear Simultaneous Equations
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6 Finding the Roots of Equations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Branches and Program Design
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Basic Control Structures
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Newton’s Method, Root Finding with MATLAB and Excel
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
COMP Loop Statements Yi Hong May 21, 2015.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 13 Transportation Demand Analysis. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Decision making If.. else statement.
Chapter 4 MATLAB Programming
Chapter 2.1 Control Structures (Selection)
Control Structures.
Microsoft Visual Basic 2005 BASICS
Decision making If statement.
Conditional Statements
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Presentation transcript:

Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Logical if Statements We previously examined if statements in Excel: IF(condition, value if true, value if false) Since there are different values corresponding to true and false conditions, this type of logic is termed if-else MATLAB has several types of if constructs, beginning with the most basic – a simple if statement Engineering Computation: An Introduction Using MATLAB and Excel

if Statement The simple if statement specifies that a command or group of commands will be executed only if a condition exists If the condition does not exist, then the command(s) are simply bypassed, and the execution of the program continues All if statements must have an end statement as well Engineering Computation: An Introduction Using MATLAB and Excel

Example Let’s consider the example of grades again With a if statement, we can mark a passing grade: Note that nothing is printed if the grade is not passing Engineering Computation: An Introduction Using MATLAB and Excel g = input( 'Enter numerical grade '); if g >= 60 grade = 'P' end

Flowchart Engineering Computation: An Introduction Using MATLAB and Excel

Example Engineering Computation: An Introduction Using MATLAB and Excel >> Grades Enter numerical grade 75 grade = P >> Grades Enter numerical grade 55 >>

if-else Statement This form is similar to the Excel IF statement in that two alternative paths are presented If the condition is true, then one set of commands is executed. If the condition is not true (else), then another set of commands is executed The same result can be achieved by “stacking” simple if statements, but this form is more compact Also, it guarantees that one of the alternative paths is followed Engineering Computation: An Introduction Using MATLAB and Excel

Example Adding to the previous commands, we can designate a grade as either passing or failing Note that one of the two options must be chosen Engineering Computation: An Introduction Using MATLAB and Excel g = input( 'Enter numerical grade '); if g >= 60 grade = 'P' else grade = 'F' end

Flowchart Engineering Computation: An Introduction Using MATLAB and Excel Note that one of the output paths must be followed

Example Engineering Computation: An Introduction Using MATLAB and Excel >> Grades Enter numerical grade 75 grade = P >> Grades Enter numerical grade 55 grade = F >>

if-elseif Statement This structure allows for more than two paths to be considered More compact structure than nested if statements As soon as a condition is satisfied, then the flow of calculations proceeds to the end statement Engineering Computation: An Introduction Using MATLAB and Excel

Example Grades example modified to report letter grades: Engineering Computation: An Introduction Using MATLAB and Excel g = input( 'Enter numerical grade '); if g >= 90 grade = 'A' elseif g >= 80 grade = 'B' elseif g >= 70 grade = 'C' elseif g >= 60 grade = 'D' else grade = 'F' end

Flowchart Engineering Computation: An Introduction Using MATLAB and Excel

Example Engineering Computation: An Introduction Using MATLAB and Excel >> Grades Enter numerical grade 85 grade = B >> Grades Enter numerical grade 68 grade = D >> Grades Enter numerical grade 55 grade = F

Summary if statement: execute a command or set of commands if a condition is true, otherwise skip the commands if-else statement: execute one set of commands if a condition is true, execute a different set of commands if the condition is false if-elseif-else: allows the checking of multiple conditions Engineering Computation: An Introduction Using MATLAB and Excel