1 / 15

CS 170 – Intro to Scientific and engineering Programming

CS 170 – Intro to Scientific and engineering Programming. Variables. Use names to assign result of an expression to a variable Variables do not need to be declared before assignment A single “equal” sign (=) is the assignment operator, LHS = RHS Read this as

varen
Download Presentation

CS 170 – Intro to Scientific and engineering Programming

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. CS 170 – Intro to Scientific and engineering Programming

  2. Variables • Use names to assign result of an expression to a variable • Variables do not need to be declared before assignment • A single “equal” sign (=) is the assignment operator, • LHS = RHS • Read this as • evaluate expression on the right-hand side, and then… • assign the result to the variable named on the left-hand-side • Therefore • The right-hand-side needs to be a legal Matlab expression • The left-hand-side needs to be a single variable name (this will get more sophisticated later on) • A semicolon at the end of the RHS expression suppresses the display, but the assignment still takes place.

  3. Examples of Variables and Assignment • Legal • >> A = sqrt(13) • >> B = exp(2); • >> A = 2*B • >> A = A + 1 • >> C = tan(pi/4) • Illegal (all for different reasons) • >> D = sqrt(E) + 1; • >> 3 = E • >> 3*A = 14 • >> F = 2 3

  4. Storing values in variables result = 4+8*sqrt(3) message = “Welcome to CS 170!!” radius = 10 • What does a variable mean? • Why are they useful? • Have you ever used variables before?

  5. Assignment statements • General formatvariable_name = expression • Right hand side can be any expression that evaluates to a value: >> hypotenuse = sqrt(3^2 + 4^2) • Executing an assignment statement: 1. evaluate the expression on the right side 2. assign value of expression to variable named on the left side

  6. Hunting for ‘s >> x = 17 >> x + 1 = 10 >> x = x / x + x >> diff = (x - y) / 2 >> end = x - 18 >> y = x = 2

  7. Assigning variables in a script • Statements in script are executed as if they were typed directly into the Command Window • Variables created in CW can be changed by assignments in scripts and vice versa! • What would happen if the script contained a syntax error?

  8. Variable names • Must start with a letter • May contain any number of letters of digits or underscore characters • Case matters! • Reserved words in Matlab:break case catch continue else elseifend for function global if otherwise persistent return switch try while

  9. Chose meaningful names! • The good, the bad, & the ugly maxArea xxyyzz_3b totalMonthlyHedgeFunds 2pi tf result • Camel-case:myfirsprogrammyFirstProgram

  10. Exercise! • Write a sequence of assignment statements that exchange the values of thing1 & thing2 • Before • After 5 thing1 thing2 3 3 thing1 thing2 5

  11. Floating point numbers • Decimal numbers are represented as a type called double >> number = 8.43 number = 8.4300 • MATLAB normally prints 4 digits after the decimal point and rounds numbers up or down to fit

  12. Really big and really little numbers • MATLAB uses scientific notation to print very large or very small numbers >> number = 1230000000 number = 1.2300e+09 >> number = 0.000789 number = 7.8900e-04 • MATLAB has a special representation for really big numbers >> number = 1.2e+9999 number = Inf • * What do get when you type: 1/0 or 1/infor 0*inf?

  13. Strings • Strings consist of letters, digits, symbols, and white space, surrounded by single quotes: myName= ‘Xenia I am’ eec= ‘ )&it:s;elf,’ thirteen = ‘6 + 7’ • Common ‘s callMe= Ishmael reply = ‘Madam, I’m Adam’

  14. Questions??

  15. Resources • Lecture slides CS112, Ellen Hildreth, http://cs.wellesley.edu/~cs112/

More Related