Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 10/24/2002 Lecture 16: 10/24/2002 CS149D Fall 2002 1 Outline •Variables and Data types •Assignment statement •Arithmetic operators •Increment/Decrement operators Lecture 16: 10/24/2002 CS149D Fall 2002 2 Variables Names •Program variables correspond to memory spaces reserved for storage •A variable name is called an identifier •An identifier in C++ can be up to 255 characters long Can not begin with a digit (Invalid: 1First) Can not contain blanks (Invalid: num elements) Can not contain a hyphen, underscore is OK (Invalid: num-elements) Special symbols are not allowed (Invalid: cost$, cost!) Reserved words can not be used as identifiers (Invalid: int, const, float) A reserved word is a word that has a special meaning in C++. It can not be used as a programmer-defined identifier •C++ is case sensitive, so count is different than Count •To declare a variable, need to identify its data type Lecture 16: 10/24/2002 CS149D Fall 2002 3 Data Types C++ Data types (built-in data types) •Integers •Floating-point numbers •Characters •And more •The data type determines how data is represented in the computer and the kind of processing that the computer can perform on it •The number of bytes that a data type occupies in memory is system dependent Lecture 16: 10/24/2002 CS149D Fall 2002 4 Variable Declaration •DataType identifier; int x; •DataType identifier, identifier, …; int x,y,z; •The initial value stored in a variable is not know unless you initialize it with the declaration int x = 10; •Can mix declaration with declaration/initialization int x=10, y, z = 5; •Use const keyword to indicate that the value of this variable can not change const float PI = 3.141593f; Lecture 16: 10/24/2002 CS149D Fall 2002 5 Numeric Data Types •Integer numbers short, int, long Can use unsigned keyword (unsigned short) •Numbers with fractions float, double, long double (double is “double precision”) Can use scientific notation float x = 5.0e6; which means x 5 * 106 const float x = 2.3; //x is considered a double constant const float x = 2.3f; //or const float x = 2.3F; •Number of bytes and range of values is system dependent. In Microsoft VC++ Data Type #of Bytes Smallest value Largest Value int 4 -2,147,483,648 2,147,483,647 short 2 -32768 32767 float 4 3.4 * 10-38 3.4 * 1038 double 8 1.7 * 10-308 1.7 * 10308 Lecture 16: 10/24/2002 CS149D Fall 2002 The textbook lists 2 bytes for int on a Borland Turbo C++ 3.0 compiler See Table 2.2 on page 29 6 Scientific Notation •Rewrite the floating-point number as a mantissa times a power of 10 •Mantissa: absolute value greater than or equal to 1.0 and less than 10.0 25.6 2.56 * 101 •Letter e is used to separate mantissa from exponent 25.6 2.56e1 •Precision: number of digits allowed for decimal portion of mantissa •Exponent range: number of digits allowed for for exponent Float Precision 6 Lecture 16: 10/24/2002 double long double 15 19 CS149D Fall 2002 see Table 2.2 7 Alphanumeric Data Types •Single alphanumeric character char x = `a`; char y = `1`; is different than int y =1; Most programming languages use ASCII to represent the English alphabet and other symbols (1 byte/character) •Sequence of characters string name = “CS149 Lecture”; Lecture 16: 10/24/2002 CS149D Fall 2002 8 Assignment statement Identifier = expression Where expression can be a constant, another variable, or the result of an operation int x, y; x = 10; // x 10, x is assigned the value of 10 y = x; x = x –10; •Multiple assignments x = y = z = 0; •Look out for the data types of both sides of the assignment int a; a = 12.8; // actually a is assigned 12 •Numeric conversion with possibility of information loss Lecture 16: 10/24/2002 CS149D Fall 2002 9 Arithmetic Operators 1/3 Operators Unary operator: One operand Binary operator: two operands + Unary Plus + Addition - Unary minus - Subtraction * Multiplication / floating-point division or integer division (no fractional part) % modulus operator (remainder of integer division) Examples Area_triangle = 0.5 *base * height; Y = -x; Lecture 16: 10/24/2002 CS149D Fall 2002 10 Arithmetic Operators 2/3 Examples of integer division and modulus 7/2 = 3 7%2 = 1 2/7 = 0 2%7 = 2 7.0/2.0 = 3.5 5 % 2.3 = Error (both operands must be integers) •Note that the result of integer division is a truncated result, not a rounded result. 5/3 = 1 5.0/3.0 = 1.66666 •Operation between different types is a mixed operation Value with lower type is converted or promoted to the higher type int sum = 18, count = 5; float average; average = sum/count; // average assigned 3.0 and not 3.6 (Why?) The correct way to compute average would be average = (float) sum / (float) count; Lecture 16: 10/24/2002 // explicit type casting CS149D Fall 2002 11 Arithmetic Operators 3/3 Practice problems page 34 int a = 27, b= 6; int b = 6; float c; float a, c = 18.6; c = a/(float)b; a = (int) c/b; Lecture 16: 10/24/2002 CS149D Fall 2002 12 Increment and Decrement Operators •Unary operators for incrementing and decrementing variables ++, -- •Can be used in a prefix (++count) or postfix (count++) position int x; x++; is equivalent to x = x +1; is equivalent to ++x; •How about x = y++ * 3; x = ++y *3; //is the value assigned to x the same in both cases: NO •Will see what is the difference next lecture when we talk about operator precedence and compound arithmetic expressions Lecture 16: 10/24/2002 CS149D Fall 2002 13