Class 10.1 04c Version A
-
Upload
tusharmhuskey -
Category
Documents
-
view
217 -
download
0
Transcript of Class 10.1 04c Version A
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 1/32
Fall 2004 ENGR 111A - 10.1 1
MatLab – Palm Chapter 4, Part 2The if and switch structure
Class 10.1
Sections: 4.4 and 4.6
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 2/32
Fall 2004 ENGR 111A - 10.1 2
RAT 10.1
Take out a piece of paper, write your name, team #,today’s date and RAT 10.1 .
As an INDIVIDUAL, you have 2-minutes to determine thevalue of y in the MatLab code shown below.
x = 10;
if x >= 12
y = sqrt(x);
else
y = x^2;
end
Pass your answer to the center aisle
Answer: y = 100
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 3/32
Fall 2004 ENGR 111A - 10.1 3
Learning Objectives
Students should be able to:
Use conditional statements to developlogical program flow. if, elseif, and else commands
switch structure
Develop flow charts in standard notation.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 4/32
Fall 2004 ENGR 111A - 10.1 4
4.4 Conditional Statements
The MatLab conditional statements enableus to write programs that make decisions
Understanding the IF-THEN-ELSE logic isfundamental to all software development.
Make sure that you understand: if statement on p. 201
else statement on p. 202/203
elseif statement on p. 205
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 5/32
Fall 2004 ENGR 111A - 10.1 5
LOGICAL CONTROLPROGRAMMING CONSTUCTS
A conditional (Boolean) statement is anexpression which tests the validity of aspecified condition
e.g., z = I ==J
z = I > J
These are used in selection structures(conditional statements) to control theflow of a program.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 6/32
Fall 2004 ENGR 111A - 10.1 6
LOGICAL CONTROLPROGRAMMING CONSTRUCTS
Syntax of the if statement:
if logical expression
statements
end
Proper indentation is MANDATORY oryou will receive NO CREDIT! Note you can right click and choose smart
indent in the m-file editor.
See page 187 for a flow chart
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 7/32Fall 2004 ENGR 111A - 10.1 7
Flowchartrepresentation of the
if statement.
Figure 4.1 –2
LOGICAL CONTROLPROGRAMMING CONSTRUCTS
Conditionalstatement
SequentialStatement(s)
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 8/32Fall 2004 ENGR 111A - 10.1 8
LOGICAL CONTROLPROGRAMMING CONSTRUCTS
MATLAB starts at the beginning of the if
sequence
It proceeds one condition to the next
When it finds a true statement, theappropriate section of the code isexecuted
THE SEQUENCE IS THEN TERMINATED!!!
The last section of code is closed using thekeyword end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 9/32Fall 2004 ENGR 111A - 10.1 9
EXAMPLE
x = some given value
if x >= 0y = sqrt (x)
end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 10/32Fall 2004 ENGR 111A - 10.1 10
EXAMPLE
x = 10;
y = 20;
if x >= 0 & y >= 0
z = sqrt(x) + sqrt(y);
w = log(x) – 3*log(y);
end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 11/32Fall 2004 ENGR 111A - 10.1 11
LOGICAL PROGRAMMINGCONTRUCTS Nested “if ” statements:
if logical expression 1
statement group 1if logical expression 2
statement group 2
end
end
Note the indentions – an absolute must
NestedStatement
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 12/32Fall 2004 ENGR 111A - 10.1 12
Typical flow Chart fornested if…end Logic
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 13/32
Fall 2004 ENGR 111A - 10.1 13
LOGICAL PROGRAMMINGCONTRUCTSTHE else STATEMENT:
If two mutually exclusive actions can occur as a resultof a decision, use the else statement.
if logical expression
statement group 1
else
statement group 2
end
See page 204 for a flow chart of a typical if-else structure.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 14/32
Fall 2004 ENGR 111A - 10.1 14
Flowchart of the
else structure.
Figure 4.4 –2
LOGICAL PROGRAMMINGCONTRUCTS
Write thesewords
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 15/32
Fall 2004 ENGR 111A - 10.1 15
In-class Exercise (5 minutes)
Suppose y = x1/2 for x >= 0
and y = ex – 1 for x < 0
Write a program (.m script file) tocalculate y assuming that x already hasa scalar value.
Test your program for x = 3 and x = -2.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 16/32
Fall 2004 ENGR 111A - 10.1 16
SOLUTION (Script File)
% Solution to In-Class Exercise
if x >= 0
y = sqrt (x);else
y = exp (x) -1;
end Did you indentproperly?!
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 17/32
Fall 2004 ENGR 111A - 10.1 17
LOGICAL PROGRAMMINGCONSTRUCTS
The elseif statement:
When three actions can occur as a resultof a decision, the else and elseif statements are used along with the if statement.
Remember: ONLY ONE ACTION WILL ACTUALY OCCUR!!!
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 18/32
Fall 2004 ENGR 111A - 10.1 18
LOGICAL PROGRAMMINGCONSTRUCTS
if logical expression 1
statement group1
elseif logical expression 2
statement group 2
else
statement group 3
end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 19/32
Fall 2004 ENGR 111A - 10.1 19
if part
elseif check
else is here
Note: else isNOT a
conditionalstatement
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 20/32
Fall 2004 ENGR 111A - 10.1 20
EXAMPLE
Given:
y = ln x for x > 10
y = x1/2
for x >= 0 and x <= 10y = ex – 1 for x < 0
Compute y if x has been assigned a scalarvalue.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 21/32
Fall 2004 ENGR 111A - 10.1 21
SOLUTION (Script file)
% Solution to example
if x > 10
y = log (x)elseif x >= 0
y = sqrt (x)
elsey = exp (x) -1
end
Does the orderthat I check
things matter? YES!
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 22/32
Fall 2004 ENGR 111A - 10.1 22
LOGICAL PROGRAMMINGCONSTRUCTS
As a TEAM , take three minutes tocomplete the following exercise.
Write the syntax for the if-elseif-else-endconstruct if there are more than three
alternatives.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 23/32
Fall 2004 ENGR 111A - 10.1 23
SOLUTION
if logical expression1
Statements group1
elseif logical expression2
Statements group2elseif logical expression3
Statements group3
elseif logical expression4
Statements group4
…
else
Statement if all other cases are false
end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 24/32
Fall 2004 ENGR 111A - 10.1 24
In-class Assignment 10.1-1
As an INDIVIDUAL , you have 10 minutes .
Write an .m script file that converts a numericaltest score to a letter grade.
(90 –100) – A
(80 – 89) – B
(70 – 79) – C
(60 – 69) – DLess than 60 – F
Test your program for the grades of 95 and 72.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 25/32
Fall 2004 ENGR 111A - 10.1 25
SOLUTION (Script file)% Program grades.m
grade = 72;
if grade >=90
letter = 'A'
elseif grade >= 80letter = 'B'
elseif grade >= 70
letter = 'C'
elseif grade >= 60letter = 'D'
else
letter = 'F'
end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 26/32
Fall 2004 ENGR 111A - 10.1 26
4.6 The switch Structure
THE “switch” STATEMENT:
Provides a convenient way to execute
conditional code when there are manycases to choose from.
This construct can replace series of if-else-end statements
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 27/32
Fall 2004 ENGR 111A - 10.1 27
LOGICAL PROGRAMMINGCONSTRUCTS
SYNTAX:
switch expression (scalar or string)
case value1
statement group 1case value2
statement group 2
…
otherwisestatement group n
end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 28/32
Fall 2004 ENGR 111A - 10.1 28
EXAMPLE for “switch”
Suppose you are given a variable anglein degrees to represent the followingdirections: Northeast, Southeast,
Southwest, and Northwest.
Use the switch statement to display the
desired direction given the angle.
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 29/32
Fall 2004 ENGR 111A - 10.1 29
SOLUTIONswitch angle
case 45
disp('Northeast')
case 135
disp('Southeast')case 225
disp('Southwest')
case 315
disp('Northwest')otherwise
disp('Direction Unknown')
end
Decision variable name
Value to test
Default case
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 30/32
Fall 2004 ENGR 111A - 10.1 30
EXAMPLE #2 for “switch”
You input a numerical value of aquantity in one set of units (e.g.,centimeters) and you desire an output
in another set of units (e.g., inches,feet, or meters, etc…).
Write a program using the switch-case
construction that transforms a length incentimeters, inches, feet, meters, etc…to length in centimeters
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 31/32
Fall 2004 ENGR 111A - 10.1 31
SOLUTIONswitch unitscase {'inch','in'} % ‘units’ contains type of
y = x*2.54; % input, output is in cm
case {'feet','ft'}
y = x*2.54*12;
case {'meter','m'}y = x*100;
case {'centimeter','cm'}
y = x;
case {'millimeter','mm'}
y = x/10;otherwise
disp(['Unknown Units: ' units])
y = NaN;
end
8/11/2019 Class 10.1 04c Version A
http://slidepdf.com/reader/full/class-101-04c-version-a 32/32
F ll 2004 ENGR 111A 10 1 32
Assignment 10.1
Individual assignment.
Due: Nov. 9, 2004
Palm’s MatLab: Chaper 4; #16, 19a,and 35.
Read Section 4.5 in the Palm MATLABbook