{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L3post - Previous Lecture(and lab Variables assignment...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Previous Lecture (and lab): Variables & assignment Built-in functions Input & output Good programming style (meaningful variable names; use comments) Today’s Lecture: Branching (conditional statements)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
September 3 Lecture 3 4 Quick review Variable A named memory space to store a value Assignment operator: = Let x be a variable that has a value. To give variable y the same value as x, which statement below should you write? x = y or y = x Script (program) A sequence of statements saved in an m-file ; (semi-colon) Suppresses printing of the result of assignment statement
Image of page 2
September 3 Lecture 3 5 Script execution (A script is a sequence of statements, an “m-file”) % Quad1 % Solves x^2 + 5x + 6 = 0 a = 1; b = 5; c = 6; d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) Memory space
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
September 3 Lecture 3 7 Statements in a program are executed in sequence % A program fragment ... x= 2*3.14 y= 1+x x= 5 % What is y now? A: 6 B: 7.28 C: some other value, or error
Image of page 4
September 3 Lecture 3 8 % Example 1: Surface area of a sphere % A: surface area of the sphere % r: radius of the sphere r= input('Enter the radius: '); A= 4*3.14159*r*r; fprintf('Surface area is %f.\n', A);
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
September 3 Lecture 3 10 Input & output variable = input (‘ prompt ’) r= input (‘Enter the angle: ’)
Image of page 6
September 3 Lecture 3 11 Input & output variable = input (‘ prompt ’) r= input (‘Enter radius: ’) fprintf (‘ message to print ’) fprintf (‘Increase ’) fprintf (‘is %f inches\n’, x) fprintf (‘Position ( %d , %d )\n’, x,y)
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
September 3 Lecture 3 12 Substitution sequences (conversion specifications) %f f ixed point (or floating point) %d d
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern