[note: + items prefixed with "!" are items that are covered differently than they were in lecture but that you are expected to learn. + these are lecture *sketches*, not lecture *notes*, and thus are no substitute for attending lecture. + [1/25] things you can type in directly into Matlab are prefixed by >> -- remember to omit the ">>" when entering into Matlab ] 1/23 Outline + 3 important principles + course overview ! J vs M + basics: datatypes (int, double) and operators (+ - * / ^) ! built-in functions, precedence, associativity + variables and assignment ! memory model, identifiers ! semicolon for supressing output of computed results ------------------------------------------------------------------------------- principle 1: learn and use strategies to avoid pitfalls + ex: start early + ex: read all instructions + ex: pronounce assignment "=" as "gets", "becomes", or "is assigned", NEVER "equals" principle 2: repetitious code is an opportunity for improvement + ex: do not overparenthesize + ex: variables and abstraction help reduce redundancy + ex: loops help reduce redundancy principle 3: indent substructures + ex: indent loop bodies ------------------------------------------------------------------------------- + course overview + please feel free to ask questions -- if i don't notice, please DO call out + *intro* to *programming* + no programming experience is required or expected + although we use java and matlab, knowledge is generally applicable, not specific to Matlab/Java + pseudocode: high-level, not-too-language specific, human-friendly code + follow instructions on Overview handout principle 1: don't wait until the last minute and find out you can't do the project -- start NOW so that you have time to deal with problems! read all instructions! -- look at course webpage -- i mean it! -- complete online registration form -- print out and complete waiver form -- software demos will be scheduled; attend one Thursday or Friday evening (we believe) -- do P1 -- sections start this week (M only) + P1 is due Tue -- encourages you to + look carefully at webpages and learn how the course is run + learn how to use course newsgroup + learn how to use Matlab + learn how to use CodeWarrior + J vs M + J: about 12 weeks Java, 2 weeks Matlab, assumes high school math + M: about half and half, assumes calculus and general comfort with math + other than that, very similar, which is reflected in the webpages + which is better for you is a matter of personal taste + J pro: spend lots of time on Java + J con: spend little time on Matlab + M pro: teaches you two programming languages in some depth + M con: less time spent explicitly on Java note: right from the start, I will point out many key differences between Java and Matlab so that Java will not come as a complete surprise; also, as stated earlier, much of what you learn about Matlab will also apply to Java + one potentially key difference: AEW ------------------------------------------------------------------------------- datatypes + int: >> 31 >> 0 >> -47 + double (double precision floating point approximation to real number), scientific notation: >> 23.48 >> 0.5 >> -28.3 >> 1.7e6 operators + addition - subtract * multiplication / division -- 9/4 = 2.25, not "2 remainder 1" <--+ ^ exponentiation ("raised to the power of") | | built-in functions | | rem remainder, e.g. rem(9,4) = 1 <--------------+ note: negative numbers are tricky min minimum, e.g. min(2,3) = 2, min(-3,4) = -3, min(2,2) = 2 max maximum exp e to the power of log natural logarithm (log base e) sin sine (argument in radians, not degrees) cos cosine (argument in radians, not degrees) tan tangent (argument in radians, not degrees) precedence + - have lowest precedence, then * /, then ^ example: >> 1+2*3^4 is the same as >> 1 + (2 * (3^4)) use parenthese to change the order, e.g. >> ((1+2)*3)^4 note: when in doubt, parenthesize, but do learn the precedence and associativity rules (see below) so that you don't overparenthesize: principle 2: unneeded parentheses should be omitted note: if parentheses aid clarity, that are not unnecessary question: what if 2 people disagree about how clear something is? answer: often there are no clear cut right or wrong answers. left-associativity: + - * / are left-associative, i.e. when used without parentheses, it is the same as parenthesizing from left to right: >> 1+2+3+4, 1-2-3-4, 1*2*3*4, 1/2/3/4 are the same as >> (((1+2)+3)+4), (((1-2)-3)-4), (((1*2)*3)*4), (((1/2)/3)/4) right-associativity: ^ is right-associative, i.e. when used without parentheses, it is the same as parenthesizing from right to left: >> 2^3^4^5 is the same as >> (2^(3^(4^5))) remark: robert heinlein, the author got this wrong in his book _The Number of the Beast_, where he claimed 6^6^6 was (6^6)^6=6^36 instead of 6^(6^6) = 6^46656, which is rather larger! _______________________________________________________________________________ motivation for variables and assignment + suppose want to see what happens if you take a number, square it, subtract 2 and the original number, square the result, subtract 2 and the original number, and square the result. suppose you want to do it for primes up to 10. so, we start off at 2: >> ((2^2-2-2)^2-2-2)^2 + suppose now want to do this for 3. now what? - retype? lot of work. - copy/paste and search/replace 2 by 3? grr, easy to replace 2 when not supposed to! + principle 2 -- redundancy is opportunity abstract out operation ((n^2-2-n)^2-2-n)^2 and somehow plug in n = 2: >> n = 2; ((n^2-2-n)^2-2-n)^2 but what about n = 3? n = 5? n = 7? variables + a variable is a named *location*/*place* for storing a value +---+ n | | +---+ identifiers + a variable is identified by its name, just like people are by their names + name needs to be unambiguous, so that you know which variable is meant. therefore, cannot have 2 variables with the same name "at the same time". + the name is technically called an *identifier*. + identifiers must start with an upper/lowercase letter that is followed by 0 or more letters, digits, or underscores examples: tHomas, ThomasYan, Thomas_Yan, C3po, david + note: in Java, the dollar sign "$" is technically like a letter and thus can be used anywhere in an identifier. however, in CS100M, we will NEVER use $ as part of an identifier. memory model: (conceptual) picture of computer memory (=storage) for now, our memory model is just a piece of paper with named boxes, i.e. a bunch of variables. assignment, aka destructive update: copy a value into a variable + in pseudocode: lhs <-- rhs pronounced "gets", "becomes", or "is assigned" means "put the value RHS into the box LHS", NOT "put lhs into rhs" do NOT confuse direction! + Matlab and Java syntax lhs = rhs is confusing since equality is symmetric (lhs = rhs and rhs = lhs are the same in math) principle 1: keep the pseudocode notation <-- in mind and always pronounce assignment as stated above, NEVER as "equals", to remind yourself of how assignment works Java versus Matlab + note: in Matlab, the first time a variable is asigned to, the assignment + implicitly *declares* the variable, i.e. tells the computer "hey computer, please create this variable" + *initializes* the variable, i.e. gives it its initial (starting) value + java has a different, explicit construct for doing *declaration*, that includes the datatype. don't worry about the syntax for now, but in java, a declaration tells the computer, "hey computer, give me a variable for holding integers" or "hey computer, give me a variable for holding doubles" _______________________________________________________________________________ semicolon to suppress display of computated results + consider the 4 following lines of code. what would happen if you typed them into matlab, one after the other? try it! (if you have questions, post to the newsgroup.) >> x = 5 >> y = x + 1 >> x = 2 * y >> z = y + observe: results are printed out. + to suppress printing, use a semicolon: >> x = 5; >> y = x + 1; >> x = 2 * y; >> z = y; + to display z, either type it by itself with no semicolon >> z or use the "disp" function to dipslay it: >> disp(z) (it does not matter if you put a semicolon after disp or not) _______________________________________________________________________________ motivation of for-loops + back to example motivating variables -- evaluate the procedure for primes up to 10: >> n = 2; ((n^2-2-n)^2-2-n)^2 >> n = 3; ((n^2-2-n)^2-2-n)^2 >> n = 5; ((n^2-2-n)^2-2-n)^2 >> n = 7; ((n^2-2-n)^2-2-n)^2 + note: semicolon here means "don't print the result of the computation" + note: still redundant! principle 2: redundancy can often be removed by using loops + for-loop (we will later see other kinds of loops, e.g. while-loops) >> for n = [2 3 5 7] >> ((n^2-2-n)^2-2-n)^2 >> end + n is loop variable + [2 3 5 7] is loop range; in general, of the form [ values ] + ((n^2-2-n)^2-2-n)^2 is loop body + code formatting follows from principle 3: indent loop bodies