8
CSCI 161 Lecture 8 Martin van Bommel

CSCI 125 & 161 Lecture 8

Embed Size (px)

DESCRIPTION

CSCI 125 & 161 Lecture 8. Martin van Bommel. Crafting a Program. Use comments to tell readers what they need to know, including difficult code Use indentation to show bodies of loops Use meaningful names Use convention for names - lastNumber Use standard idioms when appropriate - PowerPoint PPT Presentation

Citation preview

Page 1: CSCI 125 & 161  Lecture 8

CSCI 161

Lecture 8

Martin van Bommel

Page 2: CSCI 125 & 161  Lecture 8

Crafting a Program

• Use comments to tell readers what they need to know, including difficult code

• Use indentation to show bodies of loops

• Use meaningful names

• Use convention for names - lastNumber

• Use standard idioms when appropriate

• Avoid unnecessary complexity

Page 3: CSCI 125 & 161  Lecture 8

Designing for Change

• #define construct - symbolic constants

#define symbol value

• symbol - name for symbolic constant

• value - replaces symbol in precompilation

• Why? Easier to modify program

Page 4: CSCI 125 & 161  Lecture 8

Named Constants

• A variable whose content cannot be changed while program is running

const double PI = 3.14159;

• Variable still has data type and memory location, but is read-only

Page 5: CSCI 125 & 161  Lecture 8

Simple Statements

• Expression followed by semicolon

• Assignments

total = n1 + n2;• Function calls

cout << ”Hello.\n”;• Useless statements

n1 + n2;

Page 6: CSCI 125 & 161  Lecture 8

Embedded Assignments

• Assignment expression can be used as part of a larger expression in a statement

• Its value is the value assigned

z = (x = 6) + y;• x is assigned value 6, then z assigned 6 + y• Difficult to read• Used rarely and only when makes sense

Page 7: CSCI 125 & 161  Lecture 8

Multiple Assignments

• Embedded assignments useful to set several variables to the same value

n1 = n2 = n3 = 0;• Assignment operator evaluated right to left

• Avoid mixed types; e.g. double d and int i

d = i = 1.5;• Assigns i value 1, thus 1 assigned to d

Page 8: CSCI 125 & 161  Lecture 8

Math Library <cmath>Functions for performing math operations

abs(x) absolute value of argument

sqrt(x) square root of argument

pow(y, x) yx

sin(x) sine (argument in radians)

cos(x) cosine (argument in radians)

tan(x) tangent (argument in radians)

log(x) natural logarithm

exp(x) ex