Presentation is loading. Please wait.

# Chapter 8 Branching Statements and Program Design.

## Presentation on theme: "Chapter 8 Branching Statements and Program Design."— 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 1 0 0 0 0 0 1 01 0 11 1 10 0 11 0 11 1 10 0

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=[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

Download ppt "Chapter 8 Branching Statements and Program Design."

Similar presentations

Ads by Google