Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMS 261 Computer Science I

Similar presentations


Presentation on theme: "COMS 261 Computer Science I"— Presentation transcript:

1 COMS 261 Computer Science I
Title: C++ Fundamentals Date: September 14, 2005 Lecture Number: 8

2 Announcements Homework Read Sections 2.6 – 2.11 and 3.1 – 3.6
Due Thursday, September 15, 2005 2.7, 2.15, 2.16, 2.19, 2.20 Read Sections 2.6 – 2.11 and 3.1 – 3.6 Read/scan Lab 2 before tomorrow Know what you did in Lab 1 Goal is to learn, not to just get it done!!

3 Review Standard data types Integer Real (float) Character

4 Outline Keywords Identifiers Operator Precedence

5 Names Used to denote program values or components
A valid name is a sequence of Letters (upper and lowercase) Digits A name cannot start with a digit Underscores A name should not normally start with an underscore

6 Names Names are case sensitive There are two kinds of names
MyObject is a different name than MYOBJECT There are two kinds of names Keywords Identifiers

7 Keywords Keywords are words reserved as part of the language
int, return, float, double, this Colored blue in CodeWarrior They cannot be used by the programmer to name (label) things in the program They consist of lowercase letters only They have special meaning to the compiler

8 Identifiers Identifiers should be
Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard abbreviations) Long enough to be understandable When using multiple word identifiers capitalize the first letter of each word Examples min, temperature, cameraAngle, currentNumPoints

9 Definitions All objects used in a program must be defined
An object definition includes Type Name General definition form Our convention is one definition per statement!

10 Examples char Response; int MinElement; float Score;
float Temperature; int i; int n; char c; float x; Objects are uninitialized with this definition form (Value of a object is whatever is in its assigned memory location)

11 Arithmetic Operators Common Addition + Subtraction - Multiplication *
Division / Mod % Write m*x + b not mx + b

12 Arithmetic Operators Note No exponentiation operator
Single division operator Integer and float Operators are overloaded to work with more than one type of object

13 Integer Division Integer division produces an integer result Examples
Truncates Throws away the fractional part of the result Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3

14 Mod Produces the remainder of the division Examples
5 % 2 evaluates to 1 2 goes into 5, 2 times with a remainder of 1 12 % 4 evaluates to 0 4 goes into 12, 3 times with a remainder of 0 4 % 5 evaluates to 4 5 goes into 4, 0 times with a remainder of 4

15 Operators and Precedence
Consider mx + b which of the following is it equivalent to (m * x) + b m * (x + b) Operator precedence tells how to evaluate expressions

16 Operators and Precedence
Standard precedence order () Evaluate first, if nested innermost done first * / % Evaluate second. If there are several, then evaluate from left-to-right + - Evaluate third. If there are several, then evaluate from left-to-right

17 Operator Precedence Examples 20 - 4 / 5 * 2 + 3 * 5 % 4 (4 / 5)
/ 5 * * 5 % 4 (4 / 5) ((4 / 5) * 2) ((4 / 5) * 2) (3 * 5) ((4 / 5) * 2) ((3 * 5) % 4) (20 -((4 / 5) * 2)) ((3 * 5) % 4) (20 -((4 / 5) * 2)) + ((3 * 5) % 4)

18 Defining and Initializing
When an object is defined using the basic form, the memory allotted to it contains random information Better idea to specify its desired value at the same time Exception is when the next statement is an extraction for the object Remember our convention of one definition per statement!

19 Examples int FahrenheitFreezing = 32; char FinalGrade = 'A';
cout << "Slope of line: "; float m; cin >> m; cout << "Intercept: "; float b; cin >> b; cout << "X value of interest: "; float x; cin >> x; float y = (m * x) + b;

20 Memory Depiction 1001 1002 1003 1004 1005 1006 1007 1008 1009 100a 100b . ffff

21 Memory Depiction float y = 12.5; float occupies 4 bytes
int Temperature = 32; char Letter = 'c'; 1001 1002 y 1003 12.5 1004 1005 1006 1007 Temperature 32 1008 1009 Letter c 100a 100b . ffff

22 Assignment Statement Basic form Action object = expression ;
Celsius = (Fahrenheit - 32) * 5 / 9; y = m * x + b; Action Expression is evaluated Expression value stored in object

23 Definition int NewStudents = 6;

24 Definition int NewStudents = 6; int OldStudents = 21;

25 Definition int NewStudents = 6; int OldStudents = 21;
int TotalStudents;

26 Assignment Statement int NewStudents = 6; int OldStudents = 21;
int TotalStudents; TotalStudents = NewStudents + OldStudents;

27 Assignment Statement int NewStudents = 6; int OldStudents = 21;
int TotalStudents; TotalStudents = NewStudents + OldStudents;

28 Assignment Statement int NewStudents = 6; int OldStudents = 21;
int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

29 Assignment Statement int NewStudents = 6; int OldStudents = 21;
int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

30 Consider int Value1 = 10;

31 Consider int Value1 = 10; int Value2 = 20;

32 Consider int Value1 = 10; int Value2 = 20; int Hold = Value1;

33 Consider int Value1 = 10; int Value2 = 20; int Hold = Value1;
Value1 = Value2;

34 Consider int Value1 = 10; int Value2 = 20; int Hold = Value1;
Value1 = Value2;

35 Consider int Value1 = 10; int Value2 = 20; int Hold = Value1;
Value1 = Value2; Value2 = Hold;

36 Consider int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2; Value2 = Hold; We swapped the values of objects Value1 and Value2 using Hold as temporary holder for Value1’s starting value!

37 Incrementing int i = 1; i = i + 1;
Assign the value of expression i + 1 to i Evaluates to 2

38 Const Definitions Modifier const indicates that an object cannot be changed Object is read-only Useful when defining objects representing physical and mathematical constants const float Pi = ;

39 Const Definitions Value has a name that can be used throughout the program const int SampleSize = 100; Makes changing the constant easy Only need to change the definition and recompile

40 Assignment Conversions
Floating-point expression assigned to an integer object is truncated Integer expression assigned to a floating-point object is converted to a floating-point value

41 Assignment Conversions
float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;


Download ppt "COMS 261 Computer Science I"

Similar presentations


Ads by Google