------------------------------------------------------------------------------- Selection Statements + also known as condition statements + so far, programs seen as top-down sequence of instructions + often problems require choices + need to express situations as TRUE and FALSE + use tests to cause branches in the program ------------------------------------------------------------------------------- Logic + Boolean: test logical TRUTH or FALSEHOOD + Boolean Expressions: expressions with boolean values and operators + logical values: 0 (false) and 1 (true) + logical operators: & (and), | (or), ~ (not), xor (exclusive or) + relational operators: >, >=, <, <=, ==, ~= + examples) 1 < 2 % less than 1 == 1 % equal (true) 1 == 2 % equal (false) 1==1 & 2==2 % true 1 > 2 | 1 < 2 % true xor(1 < 2,1 < 3) % false ------------------------------------------------------------------------------- Branches: + making choices + use $if$ (skip $switch$) + see $help if$ General form of the $if$ statement is IF expression statements ELSEIF expression statements ELSE statements END + Must have $if$ and $end$ + $else$ and $elseif$s are optional ------------------------------------------------------------------------------- % Example 1 % Test if a number is positive x = input('Enter a number: '); if x > 0 disp(['Your number is positive.']) end ------------------------------------------------------------------------------- % Example 2 % Test if a number is positive x = input('Enter a number: '); if x > 0 disp(['Your number is positive.']) else disp(['Your number isn't positive.']) end ------------------------------------------------------------------------------- % Example 3 % Test sign of a number (see $help sign$ for built-in function) x = input('Enter a number: '); if x > 0 disp(['Your number is positive.']) elseif x < 0 disp(['Your number is negative.']) else disp(['Your number is zero.']) % but can you guarantee that??? end ------------------------------------------------------------------------------- % Example 4 % Test sign of a number (see $help sign$ for built-in function) x = input('Enter a number: '); if x > 0 disp(['Your number is positive.']) elseif x < 0 disp(['Your number is negative.']) elseif abs(x) < eps disp(['Your number is zero.']) % but can you guarantee that??? else disp(['I don't know what the hell your number is.']) end ------------------------------------------------------------------------------- % Example 5 % Test sign of a number test = input('Enter the number to test: '); if isnumeric(test) if x > 0 disp(['Your number is positive.']) elseif x < 0 disp(['Your number is negative.']) elseif abs(x) < eps disp(['Your number is zero.']) else disp(['I don't know what the hell your number is.']) end else disp(['I said "Enter a number." Now look what you have done!']) end ------------------------------------------------------------------------------- More about selection statements % example) swap values disp(['swapping values']) v1 = input('give me value1: '); v2 = input('give me value2: '); % swapping using a "temporary" variable % $tmp$ acts as a placeholder tmp = v1; v1 = v2; v2 = tmp; disp(['v1 now has v2 value: ' num2str(v1)]) disp(['v2 now has v1 value: ' num2str(v2)]) disp(['press any key to continue']) % now, apply to problem: % Find the maximum of stored value and input value % Keep the smaller value in variable called $min$ % input disp(['finding a max value']) max = input('give me a value: '); val = input('give me another value: '); % testing and swapping if (val > max) min = max; max = val; else min = val; end % output min max % curiosity val % what happens to $v1$? nothing clear val % if you want to erase $v1$ % val % now unassigned! ------------------------------------------------------------------------------- % Example 7 % Temperature range t = input('Enter the temperature: '); if isnumeric(t) if t > 70 if t < 80 disp('comfy'); else disp('hot!'); end end end -------------------------------------------------------------------------------