32
LECTURE 3: VARIABLES PHY 107 – Programming For Science

PHY 107 – Programming For Science. Announcements Slides, activities, & solutions always posted to D2L Note-taking versions before class, for those

Embed Size (px)

Citation preview

Page 1: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

LECTURE 3:VARIABLES

PHY 107 – Programming For Science

Page 2: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Announcements

Slides, activities, & solutions always posted to D2L Note-taking versions before class, for those

who want Do get updated after class with any needed

updates Use them in labs, weekly assignments, and

projects There are also many other resources

available Links on D2L to a few useful websites Library’s closed reserve has textbook &

other books

Page 3: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Your First C Program

Page 4: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Pre-processor Directives

Code “pre-processed” before compilation No need to request it --- automatically

occurs Easier-to-read code results from this

process Just like using comments -- notice a recurring

theme?

Pre-processor directives start with # One directive per line & nothing else on the

line Directives should not span multiple lines

Page 5: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Symbolic Constants

Directive can be used to name a constant Any/all lines BELOW directive can use this

constant Pre-processor replaces name with value

Compiler sees value as if that were written there

When reading the code, programmer sees name

Makes code much easier to read, write, debug

Names traditionally in all CAPITAL letters THIS IS NOT REQUIRED, but “good style”

Page 6: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What You Write And Work With#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = PI * (r * r);printf(MY_NAME);printf(DUMB_EXAMPLE);

Page 7: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What The Compiler Sees

#define PI 3.1415962#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = PI * (r * r);printf(MY_NAME);printf(DUMB_EXAMPLE);

Page 8: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What The Compiler Sees

#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = 3.1415962 * (r * r);printf(MY_NAME);printf(DUMB_EXAMPLE);

Page 9: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What The Compiler Sees

#define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = 3.1415962 * (r * r);printf(MY_NAME);printf(DUMB_EXAMPLE);

Page 10: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What The Compiler Sees

#define MY_NAME “Matthew Hertz”#define DUMB_EXAMPLE MY_NAMEdouble area = 3.1415962 * (r * r);printf(MY_NAME);printf(DUMB_EXAMPLE);

Page 11: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What The Compiler Sees

#define DUMB_EXAMPLE “Matthew Hertz”double area = 3.1415962 * (r * r);printf(“Matthew Hertz)”;printf(DUMB_EXAMPLE);

Page 12: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What The Compiler Sees

double area = 3.1415962 * (r * r);printf(“Matthew Hertz)”;printf(“Matthew Hertz)”;

Page 13: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

What The Compiler Sees

double area = 3.1415962 * (r * r);printf(“Matthew Hertz)”;printf(“Matthew Hertz)”;

Page 14: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Symbolic Constants

Value copied where it is used

Page 15: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Symbolic Constants

Value copied where it is used:

Does not need equal sign

Does not need semicolon

Page 16: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Symbolic Constants

Value copied where it is used:

Does NOT need equal sign Does NOT

need semicolon

Page 17: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Variables

Variable gives name to address where data stored When variable created, its initial value is

unknown Assignments update memory location with

new value Locations in memory updated by assignment

ONLY When variable is used in program…

…uses current value at that memory location Just about everything (interesting) uses

variables

Page 18: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Variable Declarations

Variables must be declared before can be used Way of getting computer to make space for

variable States how to interpret memory in future

uses Allows the compiler to check if uses are

legal Declarations must include two pieces:

Each variable must have legal, unique name

Type of data that the variable stores

Page 19: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Variable Names

Begin with letter or underscore (_) Then use any letters, numbers, or

underscore C/C++ case-sensitive when naming

variables Will treat as different Mass, mass, & masS

Unique name* needed for each variable Computer wouldn't know which of 1,000 bobs to use

Reserved words are… reserved and can't be used Reserved words includes void, unsigned, class

Page 20: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Variable Name Conventions

Usually names begin with lowercase letter Helps clarify variables & symbolic constants

Best if name specifies datum variable stores Split multiple uses into multiple variables

Some things always make for bad names tmp, b, l (lowercase letter L) Anything would not say to grandparent,

priest, boss…

Page 21: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Variable Name Conventions

Usually names begin with lowercase letter Helps clarify variables & symbolic constants

Best if name specifies datum variable stores Split multiple uses into multiple variables

Some things always make for bad names tmp, b, l (lowercase letter L) Anything would not say to grandparent,

priest, boss…

Page 22: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Data Types

Each variable also has data type How program treats variable’s value

defined by this Single true or false value held by bool C/C++ defines 7 numeric data types

Integer types: short, int, long, long long Decimal types: float, double, long double Ranges for each type is not really

standardized Non-negative versions using unsigned

______ char data type can hold a character

Page 23: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Representing Text

Most computers you find follow ASCII standard American Standard Code for Information

Interchange 256 (= 28) possible characters in extended

definition Since computers are stupid, need to set

fixed size Computers use 0s & 1s ONLY – its all

they know Number still stored, but character is

displayed For number 97, a is printed Prints & for number 38 For number 55, 7 is printed

Page 24: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

ASCII Table

Page 25: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

There Is No Character

For computer, there are no characters Add to actual number just like normal

addition: ’M’+ 3 = 77 + 3 = 80 (’P’) ’0’+ 5 = 48 + 5 = 53 (’5’) 9 +’1’= 49 + 9 = 58 (’:’) ’1’+’0’= 49 + 48= 97 (’a’)

Can also use to subtract, divide, any other operation

Page 26: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Writing Variable Declarations Single variable declared as: type name;double goodNameExample;short bad;

Can also declare multiple variables at once:int i, j; long double k, l, m, n, o, p; float thisIsAReallyLongName, thisIsAnotherLongName;

Page 27: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Writing Variable Declarations Could also specify initial value for

variable Variable, constant, literal, or expression can

be used

int i = 0.0; long j = -1;long double k = -0.000232847812;long l = j, many, minusJ = -j;char c = 'i';char newLine = '\n';char tab = '\t';

Page 28: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Writing Variable Declarations Could also specify initial value for

variable Variable, constant, literal, or expression can

be used

int i = 0.0; long j = -1;long double k = -0.000232847812;long l = j, many, minusJ = -j;char c = 'i';char newLine = '\n';char tab = '\t';

Page 29: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Constants

Constants very similar to variables Must be declared with a data type and

unique name const data_type var_name; declares

variable Value of constant fixed when declared,

however Variables & constants treated and used

similarly

Page 30: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Variables, Constants, & More

General Cases Examples

Variable DeclarationdataType name;dataType name = value;dataType name, anotherName;dataType name = value, anotherName;

int count;bool monkey = true;char help,letter;char a=‘a’,letter;

Constant Declarationconst dataType NAME= value; const double PI=3.1;

Symbolic Constant #define NAME value #define AGE 34

Page 31: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

Your Turn

Work on activity in groups until 11:45 Each group will submit 1 copy at end of

class Professor chooses the copy; so must work

together

Page 32: PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those

For Next Lecture

Read pages 37 - 45 for Wed. What operations exist for us to use with

variables? What can we use these variable to do

anything? Why do data types matter in computations? What do we mean by order of operations?

Week #1 weekly assignment due Tuesday Problems available on D2L & should be

doable If problem takes more than 10 minutes,

TALK TO ME!