L3post - Previous Lecture (and lab): Variables &...

Info iconThis 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)
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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);
Background image of page 5

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

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

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

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

Page1 / 33

L3post - Previous Lecture (and lab): Variables &...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online