1 / 13

CS149D Elements of Computer Science

CS149D Elements of Computer Science. Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 10/24/2002. Outline. Variables and Data types Assignment statement Arithmetic operators Increment/Decrement operators. Variables Names.

elgin
Télécharger la présentation

CS149D Elements of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 16: 10/24/2002 CS149D Fall 2002

  2. Outline • Variables and Data types • Assignment statement • Arithmetic operators • Increment/Decrement operators CS149D Fall 2002

  3. 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 CS149D Fall 2002

  4. 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 CS149D Fall 2002

  5. 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; CS149D Fall 2002

  6. 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++ The textbook lists 2 bytes for int on a Borland Turbo C++ 3.0 compiler See Table 2.2 on page 29 CS149D Fall 2002

  7. 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 double long double • Precision 6 15 19 see Table 2.2 CS149D Fall 2002

  8. 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”; CS149D Fall 2002

  9. 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 CS149D Fall 2002

  10. Arithmetic Operators1/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; CS149D Fall 2002

  11. Arithmetic Operators2/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; // explicit type casting CS149D Fall 2002

  12. Arithmetic Operators3/3 Practice problems page 34 int a = 27, b= 6; float c; c = a/(float)b; int b = 6; float a, c = 18.6; a = (int) c/b; CS149D Fall 2002

  13. 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 CS149D Fall 2002

More Related