For/Switch/While/Try UC Berkeley Fall 2004, E77 me

Slides:



Advertisements
Similar presentations
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Advertisements

Spring R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Where we areWhere we are Strings and TextStrings and Text ConditionalsConditionals.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Solving ODEs UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Regression UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Spring Final Review Excel and PowerPoint. Excel Functions – IF computer chooses between alternatives = if ( condition, true section, false section ) 
Sorting UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
While Loops ENGR 1181 MATLAB 10.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Intro to Simulink April 15, Copyright , Andy Packard. This work is licensed under the Creative Commons.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Intro to Simulink Modified by Gary Balas 20 Feb 2011 Copyright , Andy Packard. This work is licensed under.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Lecture 15: Theory of Automata:2014 Finite Automata with Output.
Decision Making: while loops and Boolean Logic. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as.
Matlab Programming for Engineers
Introduction to Java E177 April 29, me. berkeley
Repetition Structures Chapter 9
Chapter 4 MATLAB Programming
Numeric Arrays Numeric Arrays Chapter 4.
ME190L Nyquist Stability Criterion UC Berkeley Fall
Scripts & Functions Scripts and functions are contained in .m-files
Reusable Graphics Objects UC Berkeley Fall 2004, E77 me
Introduction to MATLAB
Week 8 - Programming II Today – more features: Loop control
Arrays, For loop While loop Do while loop
Unit 2 Programming.
Class Info E177 January 22, me. berkeley
Regular Expressions: Searching strings for patterns April 24, 2008 Copyright , Andy Packard and Trent Russi. This work is licensed under the Creative.
@ReferAssignGetSet February 21, me. berkeley
Matlab tutorial course
Black Box Software Testing Fall 2005 Overview – Part 1 of 3
Chapter 7 Conditional Statements
Logical Operations In Matlab.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Class Intro/TDD Intro 8/23/2005
CISC124 Labs start this week in JEFF 155. Fall 2018
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CMPE212 – Reminders The other four assignments are now posted.
E177, Reference/Assignment for Classes: Intro February 12, Copyright , Andy Packard. This work.
Dr. Sampath Jayarathna Cal Poly Pomona
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Chapter 3 Debugging Section 3.4
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

For/Switch/While/Try UC Berkeley Fall 2004, E77 http://jagger. me For/Switch/While/Try UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

for, end (page 163-174) Execute collection of statements a fixed number of times. for x=expression statements end The expression is evaluated once before the loop starts. The value is called the controlvalue. The statements are executed one time for each column in the controlvalue. In the code above, x is the loopvariable. Before each “execution pass,” the loopvariable (x) is assigned to the corresponding column of controlvalue 1st column on first pass, 2nd column on second pass, and so on Execution continues with any statements beyond the end

for loops, most common use(!) The most common value for expression is a row vector of integers, starting at 1, and increasing to a limit n for x=1:n statements end The controlvalue is simply the row vector [ 1 2 3 4 … n ] Hence, the statements are executed n times. The first time through, the value of x is set equal to 1; the k’th time through, the value of x is set equal to k.

for loops, most common use The expression can be created before the loop itself, so xValues = 1:n; for x=xValues statements end for x=1:n statements end is the same as

for loop example: loancalc.m function P = loancalc(L,R,N,MP) % P = loancalc(L,R,N,MP) computes the % history of amount owed on a loan of amount % L, interest rate R, duration N, and fixed % monthly payment MP. P = zeros(N+1,1); P(1) = L; % amount owed at Month=0 % interest rate R is annual, but applied % monthly, yielding a 1+R/12 factor. G = 1+R/12; for i=2:N+1 P(i) = P(i-1)*G – MP; end

switch, case, otherwise end Let VAL be value of expression. Assume VAL is a scalar double. switch expression case testval1 statements1 case testval2 statements2 otherwise statementsOther end Does VAL equal testval1? Yes: Execute statements1 Jump past end. No: Does VAL equal testval2? Yes: Execute statements2 Jump past end Execute statementsOther Move to code beyond end Any number of cases are allowed. There does not have to be an otherwise (and associated statements).

switch, case, otherwise end switch expression case testval1 statements1 case testval2 statements2 otherwise statementsOther end VAL (the value of expression) need not be a scalar double. It can also be a char array. Matlab uses strcmp (string compare) to check equality of VAL to the various testval1, testval2, etc.

switch, case, otherwise end switch expression case testval1 statements1 case value2 statements2 otherwise statementsOther end testval can also be cell arrays. The equality of VAL (value of expression) is tested for any of the contents of the cell array testval.

while while expression statements end while expression statements end Executing commands an undetermined number of times. while expression statements end repeat while expression statements end Evaluate expression If TRUE, execute statements If FALSE, jump past end

tic is a built-in Matlab function that starts a timer. Example using tic/toc tic is a built-in Matlab function that starts a timer. Every subsequent call to toc (also Matlab built-in) returns the elapsed time (in seconds) since the originating call to tic. The code below will cause the program to “pause” for one second before proceeding. tic while toc<1 end It would be clumsy to do this without a while-loop

try/catch try statements1 catch statements2 end Try to execute these commands If a run-time error occurs while executing statements1, stop, and… Execute these commands If no error occurs executing statements1, jump beyond end. Do not execute statements2