chapter 8 branching statements and program design

27
Chapter 8 Branching Statements and Program Design

Upload: douglas-cummings

Post on 22-Dec-2015

245 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 8 Branching Statements and Program Design

Chapter 8Branching Statements and Program Design

Page 2: Chapter 8 Branching Statements and Program Design

Control StatementsBranches: Selects specific sections

of code to execute

Loops: Cause specific code to repeat

Page 3: Chapter 8 Branching Statements and Program Design

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

Page 4: Chapter 8 Branching Statements and Program Design
Page 5: Chapter 8 Branching Statements and Program Design

True / False Operators > Relational > Logic

Page 6: Chapter 8 Branching Statements and Program Design

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

Page 7: Chapter 8 Branching Statements and Program Design

Logic Operators & AND | OR

xor Exclusive OR ~ Logical NOT

Page 8: Chapter 8 Branching Statements and Program Design

Truth TablesL1 L2 L1&L2 L1|L2 L1xorL2 ~L1

0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1 0 0

Page 9: Chapter 8 Branching Statements and Program Design

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)

Page 10: Chapter 8 Branching Statements and Program Design

Conditional Statements (Branches)ifelseifelse

Page 11: Chapter 8 Branching Statements and Program Design

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

Page 12: Chapter 8 Branching Statements and Program Design

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

Page 13: Chapter 8 Branching Statements and Program Design

Example: if statement (nested)Example: if statement (nested)

resp=input(‘Do you want to continue? Y/N:’,’s’);resp=input(‘Do you want to continue? Y/N:’,’s’);if ((resp==‘Y’)|(resp==‘y’))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:’); endend

elseelse disp(‘You have decided to stop’);disp(‘You have decided to stop’);

endend

Page 14: Chapter 8 Branching Statements and Program Design

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

Page 15: Chapter 8 Branching Statements and Program Design

Example: if and elseif statementsExample: if and elseif statementsx=input(‘Enter value for x:’);x=input(‘Enter value for x:’);y=input(‘Enter value for y:’)y=input(‘Enter value for y:’)if (x >= 0) & (y >= 0)if (x >= 0) & (y >= 0)

fxy=x+y;fxy=x+y;elseif (x >= 0) & (y < 0)elseif (x >= 0) & (y < 0) fxy=x+y^2;fxy=x+y^2;elseif (x<0) & (y >= 0)elseif (x<0) & (y >= 0) fxy=x^2+y;fxy=x^2+y;else else fxy=x^2+y^2;fxy=x^2+y^2;endendDisp([‘The value of the function is ‘ num2str(fxy)]);Disp([‘The value of the function is ‘ num2str(fxy)]);

Page 16: Chapter 8 Branching Statements and Program Design

Problem using IF statementWrite pseudocode and then write a program for the following problem:

y = √x for x ≥ 0.y = ex – 1 for x < 0.

Page 17: Chapter 8 Branching Statements and Program Design

Problem using IF statementWrite pseudocode and then write a program for the following problem:

z = √x + √yw = log10x – 3log10y

For nonnegative x,y

Page 18: Chapter 8 Branching Statements and Program Design

Problem using IF statementWrite pseudocode and then write a program for the following problem:

y = ln x for x > 5y = √x for 0 ≤ x ≤ 5

Page 19: Chapter 8 Branching Statements and Program Design

Problem using IF statementWrite pseudocode and then write a program for the following problem:

y = ln (x) for x > 10z = 4y for y > 3z = 2y for 2.5 < y ≤ 3z = 0 for y ≤ 2.5

y = 5x for x ≤ 10z = 7x

Page 20: Chapter 8 Branching Statements and Program Design

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

Page 21: Chapter 8 Branching Statements and Program Design

switch statement formswitch input expressioncase case1 values statement group 1case case2 values statement group 2otherwise statement group nend

Page 22: Chapter 8 Branching Statements and Program Design

Example: Switcht=[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

Page 23: Chapter 8 Branching Statements and Program Design

Example: Switchswitch value

case {1,3,5,7,9}disp(‘Value is Odd’)

case {2,4,6,8,10}disp(‘Value is Even’)

otherwisedisp(‘Value is out of range’);

end

Page 24: Chapter 8 Branching Statements and Program Design

Switch Example:Given the integer angle measured from North; use the switch structure to display the corresponding point on the compass.

point >> integer angleNorth >> 0 or 360South >> -180 or 180East >> -270 or 90West >> -90 or 270

Page 25: Chapter 8 Branching Statements and Program Design

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

Page 26: Chapter 8 Branching Statements and Program Design
Page 27: Chapter 8 Branching Statements and Program Design

Example: Try/Catcha=[0 1 3 5 7 9];try index=input(‘Enter index: ’); disp( [‘a(‘ int2str(index) ‘) = ‘ num2str(a(index))] ); catch disp( [‘Illegal Selection: ‘ int2str(index)] );end