You are on page 1of 8

GENN004: Introduction to Computers

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

GENN004: Introduction to Computers

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

GENN004: Introduction to Computers

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

GENN004: Introduction to Computers

09-Feb-13

Logic Operators
Logic Operators can combine truth statements together in helpful ways

Logic Operators Truth Table

Inputs A false false true true B false true false true

and A&B false false false true A && B false false false true

or A |B false true true true A || B false true true true

xor xor(A,B) false true true false

not ~A true true false false

Note on XOR: Returns true if and only if both operands are different.

Selection Statement

GENN004: Introduction to Computers

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

1. a < 10.0+k 3. a+b >= 6.5 5. k ~= a-b 7. b-k > a

Selection Statement

GENN004: Introduction to Computers

09-Feb-13

Branching Example
A>B TRUE FALSE A == B TRUE FALSE disp(B is larger!)

disp(A is larger!)

disp(A equals B!)

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

GENN004: Introduction to Computers

09-Feb-13

Branching Code (Mutual Exclusive)


truth_statement_1? true false truth_statement_2? true truth_statement_3 true 1st Branch Statements 2nd Branch Statements 3rd Branch Statements Catch-all Branch Statements false false

if-elseif example
if A>B disp('A is large'); elseif A==B disp('Are equal'); else disp('B is large'); end

Selection Statement

GENN004: Introduction to Computers

09-Feb-13

More on the if construct


Branch statements can be any valid MATLAB instructions, including additional if-elseif-else-end statements if x > 0 if y < 0 end end
There is no practical upper limit to the number of nested if constructs Remember to close each if construct with the end statement If each branch represents mutually exclusive options, use a single if construct with several elseif clauses instead of nested if clauses

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

You might also like