MATLAB Loops and Branching Loop types Counted loops
MATLAB Loops and Branching
Loop types • Counted loops; called for loop • Conditional loops; called while loop
Looping! • Scripts and functions also allow the ability to loop using conventional for and while loops. • Note that the interpreter also lets you do it, it is simply less easy to grasp
For Loops • Common to other programming languages for variable = expression statement. . . statement end
For Loops, con’t: 2 • Example: (taken from MATLAB help) • a = zeros(k, k) % Preallocate matrix for m = 1: k for n = 1: k a(m, n) = 1/(m+n -1); end
For Loops, con’t: 3 • The looping variable is defined in much the same way that we defined arrays/vectors. • Ex. m = 1: k • Or m = 1: 10
For Loops, con’t: 4 • Loops are shown to end by the keyword “end” • Curly braces are not present to subdivide packets of code • Make use of adequate white-space and tabbing to improve code readability
While Loops • Similar to while loops in other languages while condition statement … end
While Loops, con’t: 2 • Ex. (taken from help while) while (1+eps) > 1 eps = eps/2; end
While Loops, con’t: 3 • Same notes apply to while loops. • Code is separated by the keyword “end”
Looping conclusion • Some other aspects of looping exist • Use help while and help for to see them
LOGICAL CONTROL PROGRAMMING CONSTRUCTS • Syntax of the if statement: if logical expression statements end
EXAMPLE x = some given value if x >= 0 y = sqrt (x) end
EXAMPLE x = 10; y = 20; if x >= 0 & y >= 0 z = sqrt(x) + sqrt(y); w = log(x) – 3*log(y); end
LOGICAL PROGRAMMING CONTRUCTS • Nested “if” statements: if logical expression 1 statement group 1 if logical expression 2 statement group 2 end • Note the indentions Nested Statement
LOGICAL PROGRAMMING CONTRUCTS THE else STATEMENT: If two mutually exclusive actions can occur as a result of a decision, use the else statement. if logical expression statement group 1 else statement group 2 end
In-class Exercise (5 minutes) • Suppose y = x 1/2 for x >= 0 and y = ex – 1 for x < 0 • Write a program (. m script file) to calculate y assuming that x already has a scalar value. • Test your program for x = 3 and x = -2.
SOLUTION (Script File) % Solution to In-Class Exercise if x >= 0 y = sqrt (x); else y = exp (x) -1; Did you indent end properly? !
LOGICAL PROGRAMMING CONSTRUCTS The elseif statement: When three actions can occur as a result of a decision, the else and elseif statements are used along with the if statement. Remember: ONLY ONE ACTION WILL ACTUALY OCCUR!!!
LOGICAL PROGRAMMING CONSTRUCTS if logical expression 1 statement group 1 elseif logical expression 2 statement group 2 else statement group 3 end
EXAMPLE Given: y = ln x for x > 10 y = x 1/2 for x >= 0 and x <= 10 y = ex – 1 for x < 0 Compute y if x has been assigned a scalar value.
SOLUTION (Script file) % Solution to example if x > 10 Does the order that I check y = log (x) things matter? elseif x >= 0 YES! y = sqrt (x) else y = exp (x) -1 end
LOGICAL PROGRAMMING CONSTRUCTS Write the syntax for the if-else-end construct if there are more than three alternatives.
SOLUTION if logical expression 1 Statements group 1 elseif logical expression 2 Statements group 2 elseif logical expression 3 Statements group 3 elseif logical expression 4 Statements group 4 … else Statement if all other cases are false end
- Slides: 24