lecturenotes9_17 - 9/17/2008 Conditional Programming...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9/17/2008 Conditional Programming Structures Wednesday Sept. 17th, 2008 Simple “IF” Syntax • IF statement is the principle conditional structure we will use • Syntax: if <comparison> <statement(s)> end %if statement (Note the use of the indent to clarify the “inner” commands) 1 9/17/2008 Simple “IF” Logic • If the comparison (relational or logical) returns a value of “true” the commands between the IF…END keywords are executed • If the comparison is false, execution skips to the first line after the END keyword • If statements are easy to interpret when using scalar values, but require more care when using arrays. “IF” with array arguments • When using an array as part of the condition, MATLAB will assess whether the condition is true or false in the same manner • Recall the relational operator A > B when A = [ 1 2 3 4] B = [‐2 0 2 6] • A > B evaluates to FALSE in this case and inner commands are not executed. 2 9/17/2008 Example 1 if G>50 disp(‘G is greater than 50. \n’) count = count + 1; end What is the result of this statement if 1) G = 80 ? 2) G = (0:10:80) ? Example 1 (cont) • In the second case, the condition evaluates to true for only G(7:9) • Note that you can also pass a single element and get a “true” ex. G(7) or G(7:9) 3 9/17/2008 IF/ELSE Syntax • A minor modification on the Simple IF, is the IF/ELSE statement, slightly more common than the simple IF • Syntax: if <comparison> <statement to execute if true> else <statement to execute if it is not true> end %if statement How is this executiong different than the previous statement?? The ELSEIF Structure • This structure allows us to “nest” if statements within the structure • Syntax: if <comparison> <statements to execute if comparison1 is true> elseif <comparison2> <statements to execute if comparison2 is true> else <statements to execute if both 1 and 2 are false> end %if statement 4 9/17/2008 Example 2 if G>50 disp(‘G is greater than 50. \n’) elseif (G<40) disp(‘G is less than 40. \n’) else disp(‘G is between 40 and 50. \n’) end disp(‘Now we know something about G. \n’) Example 3 • Consider the problem of assigning letter grades based on the following criteria: Letter Grade A B C D F Numeric Grade 90‐100 80‐90 70‐80 60‐70 < 60 • Write a function to assign a letter grade based on a scalar score input 5 9/17/2008 Switch/Case Structure • Another method for assigning or converting values is the SWITCH/CASE statement • The structure is similar, but slightly more streamlined • Looks for EXACT matches, using either scalars or strings SWITCH/CASE Syntax switch <variable> case <val1> <statement to execute if variable=val1> case <val2> <statement to execute if variable=val2> ... case <valn> <statement to execute if variable=valn> otherwise <statement to execute if variable = none of previous vals> end % switch statement 6 9/17/2008 Example 4 • Develop a SWITCH/CASE statement to convert menu inputs to actual turbine sweep areas for the wind power example. Your menu should look like this: 7 ...
View Full Document

This note was uploaded on 02/25/2009 for the course BEE 1510 taught by Professor Staff during the Fall '05 term at Cornell.

Ask a homework question - tutors are online