Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
66 views24 pages

Fundamental Data Types

The document discusses fundamental data types in C++, including variables, constants, and operators. It defines variables as storage locations that have names and hold values. Constants are variables defined with const that cannot change value. The document explains arithmetic, relational, logical, and bitwise operators and provides examples. It also covers variable naming rules, assignment statements, and comments.

Uploaded by

Neila
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)
66 views24 pages

Fundamental Data Types

The document discusses fundamental data types in C++, including variables, constants, and operators. It defines variables as storage locations that have names and hold values. Constants are variables defined with const that cannot change value. The document explains arithmetic, relational, logical, and bitwise operators and provides examples. It also covers variable naming rules, assignment statements, and comments.

Uploaded by

Neila
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/ 24

FUNDAMENTAL

DATA TYPES
Subtitle
LEARNING OUTCOMES
▪ To be able to define and initialize variables and constants
▪ To understand the properties and limitations of integer and
floating-point numbers
▪ To write arithmetic expressions and assignment statements in
C++
VARIABLES
A variable is a storage location in a computer program. Each
variable has a name and holds a value.
Like a variable in a computer program, a parking space has an
identifier
▪ First bullet point here
▪ Second bullet point here
▪ Third bullet point here
VARIABLE NAMES
When you define a variable, you should pick a name that explains its purpose. For
example, it is better to use a descriptive name, such as can_volume, than a terse name,
such as cv. In C++, there are a few simple rules for variable names:

1. Variable names must start with a letter or the underscore (_) character, and the
remaining characters must be letters, numbers, or underscores.

2. You cannot use other symbols such as $ or %. Spaces are not permitted inside names
either. You can use an underscore instead, as in can_volume.

3. Variable names are case-sensitive, that is, Can_volume and can_volume are different
names. For that reason, it is a good idea to use only lowercase letters in variable names.

4. You cannot use reserved words such as double or return as names; these words are
reserved exclusively for their special C++ meanings
ASSIGNMENT STATEMENT
An assignment statement stores a new value in a variable,
replacing the previously stored value.
There is an important difference between a variable definition and an
assignment statement:
int cans_per_pack = 6; // Variable definition
...
cans_per_pack = 8; // Assignment statement
The assignment operator = does not denote mathematical equality.
For example, in C++, it is perfectly legal to write
total_volume = total_volume + 2;
CONSTANTS
When a variable is defined with the reserved word const, its value
can never change. Constants are commonly written using capital
letters to distinguish them visually from regular variables:
const double BOTTLE_VOLUME = 2;
It is good programming style to use named constants in your
program to explain the meanings of numeric values.
For example, compare the statements
double total_volume = bottles * 2; and
double total_volume = bottles * BOTTLE_VOLUME;
COMMENTS
Use comments to add explanations for humans who read your
code. The compiler ignores comments. It ignores everything from a
// delimiter to the end of the line.
const double CAN_VOLUME = 0.355; // Liters in a 12-ounce can

NOTE: You use the // syntax for single-line comments. If you have a
comment that spans multiple lines, enclose it between /* and */
delimiters.
OPERATORS IN C++
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C++ is rich in built-in
operators and provide the following types of operators −
• Arithmetic Operators
Compiler – converts instruction into a
• Relational Operators machine code so that it can be read and
executed by the computer.
• Logical Operators The original program is called the “source
code”, and the resulting compiled code
• Bitwise Operators produced by the compiler is the “object file or
object code”
• Assignment Operators
ARITHMETIC OPERATORS
* for multiplication
/ for division
++ operator adds 1 to a variable (increment)
-- operator subtracts 1 (decrement)
If both arguments of / are integers, the remainder is
discarded. Division works as you would expect, as long
as at least one of the numbers involved is a floating-
point number
The % operator computes the remainder of an integer
division. The operator is called modulus. (Some people
call it modulo or mod.) It has no relationship with the
percent operation that you find on some calculators.
RELATIONAL OPERATORS
Operator Description Example
Checks if the values of two operands are equal or not, (A == B) is not true.
== if yes then condition becomes true.
Assume
Checks if the values of two operands are equal or not, (A != B) is true.
variable A
holds 10 != if values are not equal then condition becomes true.
and
Checks if the value of left operand is greater than the (A > B) is not true.
variable B
holds 20 > value of right operand, if yes then condition becomes
true.
Checks if the value of left operand is less than the (A < B) is true.
< value of right operand, if yes then condition becomes
true.
Checks if the value of left operand is greater than or (A >= B) is not true.
>= equal to the value of right operand, if yes then
condition becomes true.
Checks if the value of left operand is less than or equal (A <= B) is true.
<= to the value of right operand, if yes then condition
becomes true.
LOGICAL OPERATORS
There are following logical operators supported by C++ language.
Assume variable A holds 1 and variable B holds 0,

Operator Description Example

&& Called Logical AND operator. If both the (A && B) is false.


operands are non-zero, then condition
becomes true.
|| Called Logical OR Operator. If any of the two (A || B) is true.
operands is non-zero, then condition becomes
true.
! Called Logical NOT Operator. Use to reverses !(A && B) is true.
the logical state of its operand. If a condition is
true, then Logical NOT operator will make false.
BITWISE OPERATORS
Steps to performing Bitwise Operation
1.Convert the numbers into Binary
2.Perform Operation
3.Convert answer into Decimal
EXAMPLE
Assume if A = 60; and B = 13; now in binary format they will be as
follows −
▪ A = 0011 1100
▪ B = 0000 1101
-----------------
▪ A&B = 0000 1100
▪ A|B = 0011 1101
▪ A^B = 0011 0001
▪ ~A = 1100 0011
BITWISE OPERATORS
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both (A & B) will give 12 which
operands. is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which
is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but (A ^ B) will give 49 which
not both. is 0011 0001
~ Binary Ones Complement Operator is unary and has the effect (~A ) will give -61 which is
of 'flipping' bits. 1100 0011 in 2's
complement form due to
a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left A << 2 will give 240 which
by the number of bits specified by the right operand. is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved A >> 2 will give 15 which is
right by the number of bits specified by the right operand. 0000 1111
ASSIGNMENT OPERATORS
Operator Description Example
= Simple assignment operator, Assigns values from right C = A + B will assign
side operands to left side operand. value of A + B into C
+= Add AND assignment operator, It adds right operand to
C += A is equivalent to
the left operand and assign the result to left operand.
C=C+A
-= Subtract AND assignment operator, It subtracts right
C -= A is equivalent to
operand from the left operand and assign the result to
C=C-A
left operand.
*= Multiply AND assignment operator, It multiplies right
C *= A is equivalent to
operand with the left operand and assign the result to
C=C*A
left operand.
/= Divide AND assignment operator, It divides left operand
C /= A is equivalent to
with the right operand and assign the result to left
C=C/A
operand.
ASSIGNMENT OPERATORS (CONTINUATION)
%= Modulus AND assignment operator, It takes
modulus using two operands and assign the C %= A is equivalent to
result to left operand. C=C%A

<<= Left shift AND assignment operator. C <<= 2 is same as


C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as
C = C >> 2
&= Bitwise AND assignment operator.
C &= 2 is same as C = C & 2

^= Bitwise exclusive OR and assignment


C ^= 2 is same as C = C ^ 2
operator.
|= Bitwise inclusive OR and assignment
C |= 2 is same as C = C | 2
operator.
LABORATORY ACTIVITY ( Using \n and endl )

In this program, the output would be written


within the same line. To insert a new line, you can use
the \n character

Note: Two \n characters after each other will create a blank line
Another way of inserting line is by using endl manipulator

You might also like