Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Control Flow C and Data Structures Baojian Hua
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 5: Control Structures II (Repetition)
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ISBN Chapter 8 Statement-Level Control Structures.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
UNIT II Decision Making And Branching Decision Making And Looping
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120 Introduction to Computer Science Iteration (Looping)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Matlab Programming for Engineers
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 7 JavaScript: Control Statements, Part 1
Control Flow (Python) Dr. José M. Reyes Álamo.
CNG 140 C Programming (Lecture set 3)
Chapter 3: Decisions and Loops
Matlab Training Session 4: Control, Flow and Functions
Chapter 4: Control Structures I (Selection)
Logical Operations In Matlab.
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Chapter8: Statement-Level Control Structures April 9, 2019
Presentation transcript:

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith CHAPTER 4: Execution Control

Copyright © 2010 Pearson Addison-Wesley 1-2 Objectives This chapter discusses techniques for changing the flow of control of a program, which may be necessary for two reasons: ■ You may want to execute some parts of the code under certain circumstances only - If and switch statements ■ You may want to repeat a section of code a certain number of times - for and while statements

Copyright © 2010 Pearson Addison-Wesley Concept: Code Blocks A code block is a collection of zero or more MATLAB instructions identified for one of two reasons: 1.you wish to execute them only under certain circumstances, or 2.You wish to repeat them a certain number of times Some languages identify code blocks by enclosing them in braces ({...}); others identify them by the level of indentation of the text. MATLAB uses the occurrence of key command words in the text to define the extent of code blocks: – if, switch, while, for, case, otherwise, else, elseif, end Code blocks are identified with blue coloring by the MATLAB text editor. They are not part of the code block, but they serve both –as instructions on what to do with the code block, and –as delimiters that define the extent of the code block.

Copyright © 2010 Pearson Addison-Wesley Conditional Execution in General Basic conditional execution requires two things: –A logical expression, and –A code block If the expression is true, the code block is executed. Otherwise, execution is resumed at the instruction following the code block

Copyright © 2010 Pearson Addison-Wesley 1-5 Compound conditionals By introducing elseif and else, we allow for the possibility of either conditional or unconditional execution when a test returns false as illustrated.

Copyright © 2010 Pearson Addison-Wesley if Statements The general template for if statements is: if elseif. elseif else end

Copyright © 2010 Pearson Addison-Wesley 1-7 General Observations A logical expression is any statement that returns a logical result. If that result is a logical vector, v, the if statement behaves as: if all(v) While indentation has no effect on the logical flow, it helps to clarify the logical flow. The MATLAB editor automatically creates suitable indentation as you type.

Copyright © 2010 Pearson Addison-Wesley switch Statements The template for a switch statement is: switch case case. case otherwise end

Copyright © 2010 Pearson Addison-Wesley 1-9 General Observations The switch statement is looking for the parameter to have an exact match to one of the cases. One case specification may have multiple values enclosed in braces( {…}). The default case catches any values of the parameter other than the specified cases. The default case should trap bad parameter values.

Copyright © 2010 Pearson Addison-Wesley Iteration in General Iteration allows controlled repetition of a code block. Control statements at the beginning of the code block specify the manner and extent of the repetition: –The for loop is designed to repeat its code block a fixed number of times and largely automates the process of managing the iteration. –The while loop is more flexible in character. Its code block can be repeated a variable number of times. It is much more of a “do-it-yourself” iteration kit.

Copyright © 2010 Pearson Addison-Wesley for Loops The template for a for loop is: for = end The for loop automatically sets the value of the variable to each element of the vector in turn and executes the code block with that value.

Copyright © 2010 Pearson Addison-Wesley while Loops The code block will be repeated as long as the logical expression returns true. The while loop template is: while % must make some changes % to enable the loop to terminate end

Copyright © 2010 Pearson Addison-Wesley Engineering Example—Computing Liquid Levels Give a tank as shown, how do you calculate the volume of liquid? The answer of course is “it depends on h.” If h <= r, do one calculation; otherwise if h < (H-r) do a second; Otherwise if h <= H, do a third; Otherwise there is an error!

Copyright © 2010 Pearson Addison-Wesley 1-14 The Solution if h < r v = (1/3)*pi*h.^2.*(3*r-h); elseif h < H-r v = (2/3)*pi*r^3 + pi*r^2*(h-r); elseif h <= H v = (4/3)*pi*r^3 + pi*r^2*(H-2*r)... - (1/3)*pi*(H-h)^2*(3*r-H+h); else disp(‘liquid level too high’) continue end fprintf(... 'rad %0.2f ht %0.2f level %0.2f vol %0.2f\n',... r, H, h, v);

Copyright © 2010 Pearson Addison-Wesley 1-15 Summary This chapter presented techniques for changing the flow of control of a program for condition execution and repetitive execution: ■ The most general conditional form is the if statement, with or without the accompanying elseif and else statements ■ The switch statement considers different cases of the values of a countable variable ■ A for loop in its most basic form executes a code block for each of the elements of a vector ■ Awhile loop repeats a code block a variable number of times, as long as the conditions specified for continuing the repetition remain true

Copyright © 2010 Pearson Addison-Wesley 1-16 Questions?

Copyright © 2010 Pearson Addison-Wesley 1-17