chapter 8 branching statements and program design
TRANSCRIPT
Chapter 8Branching Statements and Program Design
Control StatementsBranches: 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
xor Exclusive OR ~ Logical NOT
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
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)ifelseifelse
if statementif logical expression statement group 1 if logical expression statements group 2 end end
else statementif logical expression statement group 1else statement group 2end
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
elseif statement if logical expression statement group 1elseif logical expression statement group 2else statement group 3end
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)]);
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.
Problem using IF statementWrite pseudocode and then write a program for the following problem:
z = √x + √yw = log10x – 3log10y
For nonnegative x,y
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
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
switch construct Code is executed based on the value of a single integer, character, or logical expression.
switch statement formswitch input expressioncase case1 values statement group 1case case2 values statement group 2otherwise statement group nend
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
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
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
try/catch constructtry statement group 1 statement group 2catch statement group 1 statement group 2end
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