Download presentation
Presentation is loading. Please wait.
Published byAmie McDowell Modified over 8 years ago
1
CSC 107 – Programming For Science
2
The Week’s Goal
3
Variables Variable names location to store data Memory location's initial value is unknown Assignments update memory location with new value Memory location updated by assignment ONLY When variable is used in program… …uses current value at that memory location Just about everything (interesting) uses variables
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
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
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 not really standardized Ranges for each type is not really standardized Non-negative versions using unsigned ______ char data type can hold a character
7
Making Programs Interesting Declaring & printing boring after 1 st 6 years Want to do something with variable to use it C++ has large variety of statements to do this First statement covered in today’s lecture
8
Making Programs Interesting
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 Value of variable set to this result
10
What Is The Expression? Simplest expressions are literal values Examples: double d; int i; char doe; i = 6; i = 7; d = -7; d = 34.5691; doe = ‘a’; doe = ‘0’; 125612.345-56‘a’
11
What Is The Expression? Examples of other simple expressions double d; int i; char doe; i = 6; i = 7; d = -i; i = d; d = 34.5691; i = d; doe = ‘0’; // 0 == ASCII 48 i = doe; doe = i;
12
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
13
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
14
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
15
Program Trace 1 int x = 4 + 2; 2 int y = 8 * 1; 3 double z = y – 3; 4 x = x + 1; 5 y = 7 % x; 6 z = y + 1.0 / 2.0; 7 z = 8.0 / 4 + x * x; 8 y = (x – 3) * (y + 2); 9 y = x / 4; 10 cout << x << “ ” << y << “ ” << z << endl;
16
Integer Division Dividing two integers computes an integer Literals or variables does not matter, only their type 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!)
17
Floating Point Arithmetic Operations using decimal has decimal result Even if whole number is result of the operation For example, all the 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;
18
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
19
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);
20
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;
21
Your Turn Get in groups of 3 & work on following activity
22
For Next Lecture Read sections 6.8 – 6.9, 9.8 for Friday How do we call these built-in functions? What types of data do they work with and return? What does “typecasting” mean? Week #2 weekly assignment due Tuesday Problems available on Angel If problem takes more than 10 minutes, TALK TO ME!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.