1 / 24

MATLAB Loops and Branching: For and While Loops

Learn about loop types in MATLAB, including counted loops (for loops) and conditional loops (while loops). Understand how to use loops in scripts and functions for efficient programming. Explore logical control programming constructs such as if statements and nested if statements.

etruby
Download Presentation

MATLAB Loops and Branching: For and While Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops and Branching MATLAB

  2. Loop types • Counted loops; called for loop • Conditional loops; called while loop

  3. 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

  4. For Loops • Common to other programming languages for variable = expression statement ... statement end

  5. 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 end

  6. 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

  7. 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

  8. While Loops • Similar to while loops in other languages while condition statement … end

  9. While Loops, con’t: 2 • Ex. (taken from help while) while (1+eps) > 1 eps = eps/2; end

  10. While Loops, con’t: 3 • Same notes apply to while loops. • Code is separated by the keyword “end”

  11. Looping conclusion • Some other aspects of looping exist • Use help while and help for to see them

  12. LOGICAL CONTROL PROGRAMMING CONSTRUCTS • Syntax of the if statement: if logical expression statements end

  13. EXAMPLE x = some given value if x >= 0 y = sqrt (x) end

  14. EXAMPLE • x = 10; • y = 20; • if x >= 0 & y >= 0 • z = sqrt(x) + sqrt(y); • w = log(x) – 3*log(y); • end

  15. LOGICAL PROGRAMMING CONTRUCTS • Nested “if” statements: if logical expression 1 statement group 1 if logical expression 2 statement group 2 end end • Note the indentions Nested Statement

  16. 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

  17. In-class Exercise (5 minutes) • Suppose y = x1/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.

  18. SOLUTION (Script File) • % Solution to In-Class Exercise • if x >= 0 • y = sqrt (x); • else • y = exp (x) -1; • end Did you indent properly?!

  19. LOGICAL PROGRAMMING CONSTRUCTS • The elseif statement: • When three actions can occur as a result of a decision, the else and elseifstatements are used along with the if statement. • Remember: ONLY ONE ACTION WILL ACTUALY OCCUR!!!

  20. LOGICAL PROGRAMMING CONSTRUCTS • if logical expression 1 • statement group1 • elseif logical expression 2 • statement group 2 • else • statement group 3 • end

  21. EXAMPLE • Given: • y = ln x for x > 10 • y = x1/2 for x >= 0 and x <= 10 • y = ex – 1 for x < 0 • Compute y if x has been assigned a scalar value.

  22. SOLUTION (Script file) • % Solution to example • if x > 10 • y = log (x) • elseif x >= 0 • y = sqrt (x) • else • y = exp (x) -1 • end Does the order that I check things matter? YES!

  23. LOGICAL PROGRAMMING CONSTRUCTS • Write the syntax for the if-elseif-else-end construct if there are more than three alternatives.

  24. SOLUTION • if logical expression1 • Statements group1 • elseif logical expression2 • Statements group2 • elseif logical expression3 • Statements group3 • elseif logical expression4 • Statements group4 • … • else • Statement if all other cases are false • end

More Related