KEMBAR78
4-Data Types and Operator-1 | PDF | Integer (Computer Science) | Boolean Data Type
0% found this document useful (0 votes)
4 views34 pages

4-Data Types and Operator-1

Uploaded by

Rafia Gulzar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views34 pages

4-Data Types and Operator-1

Uploaded by

Rafia Gulzar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming Fundamentals @ Lecture

Data Types and Operator-1

1
Integer Data Types
Defining Variables

• Variables of the same type can be defined


- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
• Variables of different types must be in different definitions
Integer Literals

• An integer literal is an integer value that is typed


into a program’s code. For example:

itemsOrdered = 15;

In this code, 15 is an integer literal.


Integer Types in Program
Integer Literals in Program
The char Data Type
The char Data Type

• Used to hold characters or very small integer


values
• Usually 1 byte of memory
• Numeric value of character from the character set
is stored in memory:
CODE: MEMORY:
char letter; letter
letter = 'C';
67
Character Literals

• Character literals must be enclosed in single quote


marks. Example:

'A'
Character Literals in Program
Floating-Point Data Types
Floating-Point Data Types
• The floating-point data types are:
float
double
long double

• They can hold real numbers such as:


12.45 -3.8
The bool Data Type
The bool Data Type

• Represents values that are true or false


• bool variables are stored as small integers
• false is represented by 0, true by 1:
bool allDone = true;
bool finished = false;
allDone finished

1 0
Boolean Variables in Program
Determining the Size of a Data Type
Determining the Size of a Data Type

The sizeof operator gives the size of any data type


or variable:
double amount;
cout << "A double is stored in "
<< sizeof(double) << "bytes\n";
cout << "Variable amount is stored
in "
<< sizeof(amount)
<< "bytes\n";
Variable Assignments and Initialization
Variable Assignments and Initialization

• An assignment statement uses the = operator to


store a value in a variable.

item = 12;

• This statement assigns the value 12 to the item


variable.
Assignment

• The variable receiving the value must appear on


the left side of the = operator.
• This will NOT work:

// ERROR!
12 = item;
Variable Initialization

• To initialize a variable means to assign it a value


when it is defined:

int length = 12;

• Can initialize some or all variables:


int length = 12, width = 5, area;
Variable Initialization in Program
Scope
Scope

• The scope of a variable: the part of the program in


which the variable can be accessed
• A variable cannot be used before it is defined
Variable Out of Scope in Program
Arithmetic Operators
Arithmetic Operators

• Used for performing numeric calculations


• C++ has unary and binary operators:
– unary (1 variable) --a, a--
– binary (2 operands) 13 - 7
Binary Arithmetic Operators

SYMBOL OPERATION EXAMPLE VALUE OF


ans
+ addition ans = 7 + 3; 10

- subtraction ans = 7 - 3; 4

* multiplication ans = 7 * 3; 21

/ division ans = 7 / 3; 2

% modulus ans = 7 % 3; 1
A Closer Look at the / Operator

• / (division) operator performs integer division if


both operands are integers
cout << 13 / 5; // displays 2
cout << 91 / 7; // displays 13
• If either operand is floating point, the result is
floating point
cout << 13 / 5.0; // displays 2.6
cout << 91.0 / 7; // displays 13.0
A Closer Look at the % Operator

• % (modulus) operator computes the remainder


resulting from integer division
cout << 13 % 5; // displays 3
• % requires integers for both operands
cout << 13 % 5.0; // error
Comments
Comments

• Used to document parts of the program


• Intended for persons reading the source code of
the program:
– Indicate the purpose of the program
– Describe the use of variables
– Explain complex sections of code
• Are ignored by the compiler
Single-Line Comments

Begin with // through to the end of line:


int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area

// calculate rectangle area


area = length * width;
Multi-Line Comments

• Begin with /*, end with */


• Can span multiple lines:
/* this is a multi-line
comment
*/
• Can begin and end on the same line:
int area; /* calculated area */

You might also like