Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead."— Presentation transcript:

1 CSC 107 – Programming For Science

2 Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead use your resources: notes, books, slides, etc.  Thumb drives helpful for labs, but not required  During the day, tutors available in WTC 206/208  Weekly assignment #2 available now on Angel

3 The Week’s Goal

4 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

5 Variables, Constants, & More General CasesExamples Variable Declaration dataType name; dataType name = value; dataType name, anotherName; dataType name = value, anotherName; int count; bool monkey = true; char help,letter; char a=‘a’,letter; Constant Declaration const dataType NAME= value; const double PI=3.1; Symbolic Constant #define NAME value #define AGE 34

6 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  char data type used to store a character

7 Variable Names  Begin with letter or underscore ( _ )  Then use any letters, numbers, or underscore  Unique name * needed for each variable  Computer wouldn't know which of 1,000 bob s to use  Reserved words are… reserved and can't be used  Includes all type names on p. 83 of book  void, unsigned, class also reserved words

8 Today’s Excitement!

9 Assignments  Variable declaration creates “box” to store data  Box can get values placed in it using assignments  General form of assignment is =; variable = expression;  Computer works by first evaluating expression  Single value must result from this expression  Once computed,value of variable set to this result

10 What Is The Expression ?  Simplest expressions are literal values  Examples: double doe; int re; char me; doe = 6; re = 7; doe = -7; doe = 34.5691; me = 'a'; me = '0'; 125612.345-56‘a’

11 Data Types  Assignments are legal only if always safe  C++ defines ordering of legal assignments long double double float long int short char Legal to assign to higher type

12 What Is The Expression ?  Examples of other simple expressions double fa; int so; char la; so = 6; so = 7; fa = -so; so = fa; fa = 34.5691; so = fa; la = 48; so = la; la = so;

13 What Is The Expression ?  Examples of other simple expressions double fa; int so; char la; so = 6; so = 7; fa = -so; so = fa; fa = 34.5691; so = fa; la = 48; so = la; la = so;

14 What Is The Expression ?  Examples of other simple expressions double fa; int so; char la; so = 6; so = 7; fa = -so; so = fa; fa = 34.5691; so = fa; la = 48; so = la; la = so;

15 What Is The Expression ?  Examples of other simple expressions double fa; int so; char la; so = 6; so = 7; fa = -so; so = fa; fa = 34.5691; so = fa; la = 48; // ASCII 48 = '0' so = la; la = so;

16 What Is The Expression ?  Can also include basic arithmetic operators  Addition +i = 4 + 6;  Subtraction -d = i – 2.3;  Multiplication *i = 120 * 8;  Division /d = 4.0 / i;  Modulus %i = 120 % 8;  Modulus computes remainder between two integers: 4 % 5 equals 4 5 % 4 equals 1 9 % 3 equals 0 12823 % 812 equals 643

17 Tracing A Program  Important for understanding & debugging code  Step-by-step execution of program shown  To see what is happening, done via pencil-and-paper  Execute each line of program like computer does  Within trace, add row whenever variable declared  Update variable’s value each time it is assigned  Off to side, show any output from cout statements

18 Program Trace int x = 4 + 2; int y = 8 * 1; double z = y – 3; x = x + 1; y = 7 % x; z = y + 1.0 / 2.0; z = 8.0 / 4 + x * x; y = (x – 3) * (y + 2); y = x / 4;

19 Integer Division  Dividing two integers computes an integer  Literals or variables does not matter, only their type.0  Important to remember, 12 is integer & 12.0 is not  C++ ignores result after decimal to get integer 2 / 5 equals 0 (the.4 was thrown away) 5 / 2 equals 2 (the.5 was thrown away) 16 / 4 equals 4 -5 / 2 equals -2 (the.5 was thrown away) 2.0 / 5 equals 0.4 ( 2.0 is not an integer!)

20 Floating Point Arithmetic  Operations using decimal has decimal result  Even if whole number is result of the operation all these assignments to are illegal  For example, all these assignments to i are illegal: int i; double d = i; i = 6.0 / 3.0; i = 2.0 * d; i = d + 1; i = 4 * 2.0; i = (d * 1) + 5; i = 8 + (9 * 3) – (2 / 1.0) * 4 + 2;

21 Priority of Operations  Equations can become very complex  4 + 5 * 6 * 9 - 2 + 1 = …?  Very  Very strict order of operations used by computer  ( ) Solve from inner- to outermost  + (positive) & - (negative) Solve from right to left  * & % & / (division) Solve from left to right  + (addition) & - (subtraction) Solve from left to right use lots of parentheses  My suggestion: use lots of parentheses

22 Compound Assignment Operators  Short simple operators that allow us to be lazy  Save some typing for several common actions  Lowest priority operation; expression evaluated first OperatorEquivalent C++ Expression a += 2;a = a + 2;a = a + 2; b -= d;b = b – d;b = b – d; c *= 4 + 5.6;c = c * (4 + 5.6); d /= 0.3 * e;d = d / (0.3 * e);

23 How To Shoot Yourself in Foot  Also increment ( ++ ) & decrement ( -- ) operators  Use with variables only; no exceptions possible  Used anywhere to save typing an additional line  Two different ways these operators applied v = ++b % c;  b = b + 1; v = b % c; c = f * --h;  h = h – 1; C = f * h; a = b++ * c;  a = b * c; b = b + 1;

24 Your Turn  Get in groups of 3 & work on following activity

25 For Next Lecture  Read sections 5.1 – 5.4 & 5.6 for Friday  What is this cout thing, anyway? How is it used?  Can we read in input from the keyboard? How?  Week #2 weekly assignment already posted  Next assignment will be due on Tuesday at 5PM  Problem #1 not from today; only time this happens


Download ppt "CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead."

Similar presentations


Ads by Google