Professional Documents
Culture Documents
09-Feb-13
Selection Statement
Relational Expression if-else Statement if-elseif statement
Outline
Branching Relational Expression if-else statement if-elseif statement
Selection Statement
09-Feb-13
Branching Code
It is possible to have MATLAB choose between several branches of code Each branch decision is made based on a truth statement
If true, follow one branch If false, follow another
It is important to carefully construct these statements Most program logic errors appear in the truth statements controlling program branching
Selection (Branching)
An important computer operation available to the programmer is the ability to make choices as to whether statements are executed or not. The statements that accomplish this are called selection or branching statements. (if-Else Statement) The if statement uses expressions that are logically true or false. These expressions use relational and logical operators
Selection Statement
09-Feb-13
Relational Operators
Relational operators test the Relationship between two operands (which can be expressions) They are a Truth Statement, and resolve to either True (1) or False (0)
Operator == ~= > >= < <= Operation Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to
Relational Operators
Be careful! == is an equality test, but = is the assignment operator The equality test is strict equality; number precision errors can produce wrong answers from logically sound comparisons Try it out:
>> a = 0; >> b = sin(pi); >> a == b Instead use: >> abs(a b) < 1.0E-14
Selection Statement
09-Feb-13
Logic Operators
Logic Operators can combine truth statements together in helpful ways
and A&B false false false true A && B false false false true
Note on XOR: Returns true if and only if both operands are different.
Selection Statement
09-Feb-13
Evaluation order
1. 2. 3. 4. Parenthesis are evaluated inner to outermost Unary Operators are evaluated (^, -, then ~) Arithmetic operations are evaluated Relational operators are evaluated from left to right 5. Logical AND (& and &&) are evaluated from left to right 6. Logical OR (exclusive and inclusive) are evaluated from left to right
Practice
Determine if the following conditions are true or false. Assume the following variables have been declared and given these values: A=5.5 b=1.5 k=-3 2. ~(a == 3*b) 4. k <=k+6 5. a<10 && a>5 8. abs(k)>3 || k<b-a
INTEC CS160 - Jeanine Ingber
Selection Statement
09-Feb-13
Branching Example
A>B TRUE FALSE A == B TRUE FALSE disp(B is larger!)
disp(A is larger!)
if-else statements
If condition action1 else action2 end
if A>B disp('A is large'); else if A==B disp('Are equal'); else disp('B is large'); end end
Selection Statement
09-Feb-13
if-elseif example
if A>B disp('A is large'); elseif A==B disp('Are equal'); else disp('B is large'); end
Selection Statement
09-Feb-13
Practice
x=9, y=7, z=2, k=0, m=0, j=0; if(x > y) if(y < z) k=k+1; else m=m+1; end else j=j+1; end What is the end value of k, m, j?
Selection Statement