Ch03 - Branching Statements and Program Design (Part 1)
Ch03 - Branching Statements and Program Design (Part 1)
Ming Jiang
www.wcomms.com
Chapter 3: Branching Statements and
Program Design (Part 1)
Ming Jiang
www.wcomms.com
MATLAB Programming for Engineers
Chapter 3: Branching Statements and Program Design
(Part 1)
Ming Jiang
Top-down Design Method (1/2)
Start
End
Start
End
Ming Jiang
Relational and Logical Operators
Examples
characters are
evaluated in
alphabetical order
Examples
Hierarchy of operations
• Relational operators are evaluated after all
arithmetic operators have been evaluated
Examples:
The following two expressions are equivalent
7 + 3 < 2 + 11
(7 + 3) < (2 + 11)
What does it
mean?
L1 || L2 v.s L1 | L2
• Similar differences as in the case of && and &
XOR operator
• The result of an exclusive OR operator is
true if and only if one operand is true
and the other one is false
• different -> true (1), same -> false (0)
• Both operands must always be evaluated
Example NOT operator
>> a = 10; • The NOT operator is a unary
>> b = 0; operator, having only one operand
>> x = xor(a, b) • The result of a NOT operator is
x = true (false) if its operand is
1 false (true)
Examples
Examples
Examples
Ming Jiang
Branches
Start
Terminator
Read width
Process Read length
If total ~= 0 No/false
Decision
Yes/true
Connector Show total
Real Life
If I have free time,
(then) I will go to visit my GF.(period)
MATLAB
Conditions if logical expressions
statements
True end
Statements • Typically, the control expressions are
some combination of relational and
logic operators
• The code block is executed only
when the control expression = true
a <= 30 MATLAB
if a <= 30
True
total = 2*a;
total = 2*a end
Real Life
If I have free time,
I will go to visit my GF;
otherwise (else),
I will have to do MATLAB coursework.(period)
Conditions
MATLAB
True False
if logical expressions
Statement1 Statement2
statement block 1
else
statement block 2
end
a <= 30
MATLAB
if a <= 30
True False total = 2*a;
total = 2*a total = a+10 else
total = a+10;
end
False
Condition 1
True MATLAB
Statement 1 if logical expression 1
statement block 1
False if logical expression 2
Condition 2
Statement block 2
True end
Statement 2 end
False
a <= 30
True MATLAB
c = a + 10 if a <= 30
c = a + 10;
False if c <= 12
c <= 12 c = 0;
True end
c=0 end
Real Life
if logical expression 1
statement block 1
elseif logical expression 2
MATLAB
statement block 2
else
statement block 3
end Condition 1
False
True
Condition 2
Statement 1
True False
Statement 2 Statement 3
MATLAB
if a <= 30
c = a * 2
a <= 30 elseif c >= 20
False
True d = c
c >= 20 else
c=a*2
True False d = 0
d=c d=0 end
Question:
How to differentiate the above three cases?
Pseudo Code
MATLAB Code
% Pseudo code for differentiating
% the discriminant of a quadratic equation
if (b^2 - 4*a*c) < 0
disp('This equation has two complex roots.');
elseif (b^2 - 4*a*c) == 0
disp('This equation has two identical real roots.');
else
disp('This equation has two distinct real roots.');
end
=
disp('The grade is D.'); end
else end Equivalence
disp('The grade is F.'); end • Multiple elseif clauses
end end • Nested if constructs
Statement 1
Statement 2
...
end
} Block N • If none of the various
conditions case_expr_x is
satisfied, the code block of
otherwise will be executed
Ming Jiang www.wcomms.com 43
The switch Construct - Example
Example
switch (value)
case {1,3,5,7,9}
disp('The value is odd.');
case {2,4,6,8,10}
disp('The value is even.');
otherwise
disp('The value is out of range.');
end
}
Statement 1
Statement 2 Block 1 errors within the
... program without causing
catch the program to stop
}
Statement 1
Statement 2 Block 2 • If no error occurs, the statements in
...
the catch block will be skipped
end
■ 本课程的课件
● 主要内容:本人制作
● 部分内容:参考了中山大学数据科学与计算机学院张雨浓教授
的2008年版课件
● 少量内容:参考了Kasetsart University的James Brucker博士
早期的课件
■ 下载地址:
http://www.wcomms.com/lectures/course-matlab.html
本课件及相关作业,仅限本课程教学使用
请勿上传互联网
谢谢合作!
WWW.WCOMMS.COM