SlideShare une entreprise Scribd logo
1  sur  13
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
Can not contain blanks
elements)

(Invalid: 1First)
(Invalid: num

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

double long double
15

19

Lecture 16: 10/24/2002
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;

// explicit type casting

Lecture 16: 10/24/2002
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

Contenu connexe

Tendances

Lab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsLab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressions
AksharVaish2
 
Introduction_modern_fortran_short
Introduction_modern_fortran_shortIntroduction_modern_fortran_short
Introduction_modern_fortran_short
Nils van Velzen
 

Tendances (18)

SMU BCA SPRING 2014 SOLVED ASSIGNMENTS SEM-1
SMU BCA SPRING 2014  SOLVED ASSIGNMENTS SEM-1SMU BCA SPRING 2014  SOLVED ASSIGNMENTS SEM-1
SMU BCA SPRING 2014 SOLVED ASSIGNMENTS SEM-1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Assignment
AssignmentAssignment
Assignment
 
Lab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsLab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressions
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
Enumerated data types
Enumerated data typesEnumerated data types
Enumerated data types
 
Introduction to Excel, basic financial functions
Introduction to Excel, basic financial functionsIntroduction to Excel, basic financial functions
Introduction to Excel, basic financial functions
 
Les formules et moi, ça fait 3!
Les formules et moi, ça fait 3!Les formules et moi, ça fait 3!
Les formules et moi, ça fait 3!
 
Introduction_modern_fortran_short
Introduction_modern_fortran_shortIntroduction_modern_fortran_short
Introduction_modern_fortran_short
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Doc 20180130-wa0006
Doc 20180130-wa0006Doc 20180130-wa0006
Doc 20180130-wa0006
 
5.1part2 foil
5.1part2 foil5.1part2 foil
5.1part2 foil
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Three Address code
Three Address code Three Address code
Three Address code
 
Computer architecture data representation
Computer architecture  data representationComputer architecture  data representation
Computer architecture data representation
 
decoder and encoder
 decoder and encoder decoder and encoder
decoder and encoder
 

Similaire à Data type

presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 

Similaire à Data type (20)

Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
 
C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
 
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
C_BASICS FOR C PROGRAMMER WITH  SRIVATHS PC_BASICS FOR C PROGRAMMER WITH  SRIVATHS P
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
 
CHAPTER-2.ppt
CHAPTER-2.pptCHAPTER-2.ppt
CHAPTER-2.ppt
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming part2
C programming part2C programming part2
C programming part2
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
Mit6 087 iap10_lec02
Mit6 087 iap10_lec02Mit6 087 iap10_lec02
Mit6 087 iap10_lec02
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
Chapter02-S11.ppt
Chapter02-S11.pptChapter02-S11.ppt
Chapter02-S11.ppt
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
6276830.ppt
6276830.ppt6276830.ppt
6276830.ppt
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Data type

  • 1. 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
  • 2. Outline •Variables and Data types •Assignment statement •Arithmetic operators •Increment/Decrement operators Lecture 16: 10/24/2002 CS149D Fall 2002 2
  • 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 Can not contain blanks elements) (Invalid: 1First) (Invalid: num 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
  • 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 Lecture 16: 10/24/2002 CS149D Fall 2002 4
  • 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; Lecture 16: 10/24/2002 CS149D Fall 2002 5
  • 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++ 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
  • 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 Precision 6 double long double 15 19 Lecture 16: 10/24/2002 CS149D Fall 2002 see Table 2.2 7
  • 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”; Lecture 16: 10/24/2002 CS149D Fall 2002 8
  • 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 Lecture 16: 10/24/2002 CS149D Fall 2002 9
  • 10. 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
  • 11. 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; // explicit type casting Lecture 16: 10/24/2002 CS149D Fall 2002 11
  • 12. 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
  • 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 Lecture 16: 10/24/2002 CS149D Fall 2002 13