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

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

± 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)

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

View Full Document
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
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

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

View Full Document
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
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);

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

View Full Document
September 3 Lecture 3 10 Input & output ± variable = input (‘ prompt ’) r= (‘Enter the angle: ’)
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)

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

View Full Document
September 3 Lecture 3 12 Substitution sequences (conversion specifications) %f f ixed point (or floating point) %d d ecimal—whole number
This is the end of the preview. Sign up to access the rest of the document.

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

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

View Full Document
Ask a homework question - tutors are online