Chapter 8 Branching Statements and Program Design.

Slides:



Advertisements
Similar presentations
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Advertisements

Decision-Making Programs
Programming with MATLAB
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Chapter 4 The If…Then Statement
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 4 Programming with MATLAB PowerPoint to accompany Copyright © The.
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.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
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)
Chapter 4 Programming with MATLAB. Algorithms and Control Structures Algorithm: an ordered sequence of precisely defined instructions that performs some.
Branches and Program Design
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
CSE123 Lecture 3 Files and File ManagementScripts.
MATLAB 及其工程应用 MATLAB and It’s Engineering Application 主讲教师: 胡章芳
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 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
CPS120: Introduction to Computer Science Decision Making in Programs.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
1 Structured Programming EEN170 Programming in MATLAB.
Beginning Programming for Engineers Matlab Conditional Computation.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Introduction to MATLAB for Engineers, Third Edition
EEE 161 Applied Electromagnetics
Chapter 3: Decisions and Loops
Selection Statements by Ahmet Sacan
Introduction to MATLAB
Week 8 - Programming II Today – more features: Loop control
Lecture 3 MATLAB programming (1)
Logical Operations In Matlab.
Programming Concepts and Database
REPETITION Why Repetition?
Presentation transcript:

Chapter 8 Branching Statements and Program Design

Control Statements Branches: Selects specific sections of code to execute Loops: Cause specific code to repeat

Design Process 1. State problem 2. Define Inputs & Outputs 3. Design Algorithm of Steps 4. Turn Algorithm into MATLAB statements 5. Test

True / False Operators > Relational > Logic

Relational Operators ==Equal To ~=Not Equal To > Greater Than >=Greater Than or Equal To <Less Than <=Less Than or Equal TO

Logic Operators & AND | OR xorExclusive OR ~Logical NOT

Truth Tables L 1 L 2 L 1 &L 2 L 1 |L 2 L 1 xorL 2 ~L

Logical Functions ischar(a) Returns 1 for Character isempty(a) Returns 1 for empty array isinf(a) Returns 1 for infinite (Inf) isnan(a) Returns 1 for Not a Number isnumeric(a) Returns 1 for numeric array any(a) Returns 1 if any elements  0 all(a) Returns 1 if all elements  0 logical(a) Converts numerical values to logical values (true/false)

Conditional Statements (Branches) if elseif else

if statement if logical expression statement group 1 if logical expression statements group 2 end end

else statement if logical expression statement group 1 else statement group 2 end

Example: if statement (nested) resp=input(‘Do you want to continue? Y/N:’,’s’); if ((resp==‘Y’)|(resp==‘y’)) disp(‘You have decided to continue’); disp(‘You have decided to continue’); dt=input(‘Enter your change in time:’); dt=input(‘Enter your change in time:’); if (isempty(resp)) if (isempty(resp)) disp(‘Enter continues this program’) disp(‘Enter continues this program’) dt=input(‘Enter your change in time:’); dt=input(‘Enter your change in time:’); end endelse disp(‘You have decided to stop’); disp(‘You have decided to stop’);end

elseif statement if logical expression statement group 1 elseif logical expression statement group 2 else statement group 3 end

Example: if and elseif statements x=input(‘Enter value for x:’); y=input(‘Enter value for y:’) if (x >= 0) & (y >= 0) fxy=x+y; elseif (x >= 0) & (y = 0) & (y < 0) fxy=x+y^2; fxy=x+y^2; elseif (x = 0) fxy=x^2+y; fxy=x^2+y;else fxy=x^2+y^2; fxy=x^2+y^2;end Disp([‘The value of the function is ‘ num2str(fxy)]);

Problem using IF statement Write pseudocode and then write a program for the following problem: y = √x for x ≥ 0. y = e x – 1 for x < 0.

Problem using IF statement Write pseudocode and then write a program for the following problem: z = √x + √y w = log 10 x – 3log 10 y For nonnegative x,y

Problem using IF statement Write pseudocode and then write a program for the following problem: y = ln xfor x > 5 y = √x for 0 ≤ x ≤ 5

Problem using IF statement Write pseudocode and then write a program for the following problem: y = ln (x) for x > 10 z = 4y for y > 3 z = 2yfor 2.5 < y ≤ 3 z = 0for y ≤ 2.5 y = 5x for x ≤ 10 z = 7x

switch construct Code is executed based on the value of a single integer, character, or logical expression.

switch statement form switch input expression case case1 values statement group 1 case case2 values statement group 2 otherwise statement group n end

Example: Switch t=[0:100]; y=exp(-t).*sin(t); resp=input(‘min, max, or sum.’,’s’) switch resp case min minimum=min(y) case max maximum=max(y) case sum total=sum(y) otherwise disp(‘Proper choice not entered.’) end

Example: Switch switch value case {1,3,5,7,9} disp(‘Value is Odd’) case {2,4,6,8,10} disp(‘Value is Even’) otherwise disp(‘Value is out of range’); end

Switch Example: Given the integer angle measured from North; use the switch structure to display the corresponding point on the compass. point >> integer angle North >> 0 or 360 South >> -180 or 180 East >> -270 or 90 West >> -90 or 270

try/catch construct try statement group 1 statement group 2 catch statement group 1 statement group 2 end

Example: Try/Catch a=[ ]; try index=input(‘Enter index: ’); disp( [‘a(‘ int2str(index) ‘) = ‘ num2str(a(index))] ); catch disp( [‘Illegal Selection: ‘ int2str(index)] ); end