0% found this document useful (0 votes)
8 views30 pages

Data Types, Variables and Expressions

The document provides an introduction to programming concepts in C++, focusing on data types, variables, and expressions. It covers fundamental data types such as integers, floating-point numbers, characters, Booleans, and strings, along with their characteristics and usage. Additionally, it discusses variable declaration, constants, arithmetic operators, and operator precedence, along with examples and common pitfalls in programming.

Uploaded by

elmky.saif
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)
8 views30 pages

Data Types, Variables and Expressions

The document provides an introduction to programming concepts in C++, focusing on data types, variables, and expressions. It covers fundamental data types such as integers, floating-point numbers, characters, Booleans, and strings, along with their characteristics and usage. Additionally, it discusses variable declaration, constants, arithmetic operators, and operator precedence, along with examples and common pitfalls in programming.

Uploaded by

elmky.saif
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

INTRODUCTION TO

PROGRAMMING
DATATYPES, VARIABLES AND EXPRESSIONS
DATA TYPES

• A data type tells the computer:


• What kind of value a variable will store
• How much memory to reserve
• What operations can be performed on the data
• C++ includes several fundamental data types, such as integers, floating point
(single precision) numbers, double precision numbers, Boolean, and character
data.
INTEGERS

• An integer (int in C++) is any positive or negative number without a decimal


point. Examples of valid integers are:

• As these examples illustrate, integers may be signed (have a leading + or -


sign) or unsigned (no leading + or - sign). No commas, decimal points, or
special symbols, such as the dollar sign, are allowed.
FLOATING-POINT NUMBERS

• Floating-point (float or double in C++) constants are any signed or unsigned


numbers having a decimal point. Examples of floating numbers are:

• Examples of invalid floating-point constants are:


CHARACTER

• Characters (char in C++) are the letters of the alphabet, the ten digits 0
through 9, and special symbols such as + $ . , - !.
• A single character constant is anyone letter, digit, or special symbol enclosed
by single quotes.
• Examples of valid character constants are:
'A' 'b' '$' 'A' '!' '8'
BOOLEAN

• Boolean (bool in C++) is used to store only two possible values: a value
called true and a value called false.
• It is very useful in conditions and decision making.
STRING

• String (string in C++) is used to store text or a sequence of characters.


You can use a string to store your name, an address, or anything that contains
text. It’s also used for numbers that aren’t treated as numbers. Your Social
Security number, a zip code, a part number, or your student ID are all good
examples of numbers that aren’t really "numbers".
• Examples of valid String constants are:
FLOAT AND DOUBLE

• The difference between floating point and double precision numbers is the
amount of storage that a computer uses for each type.
• Most computers use twice the amount of storage for double precision numbers
than for floating point numbers. (for this reason, floating point numbers are
sometimes referred to as single precision numbers).
ASCII AND EBCDIC

• Character constants are typically stored in a computer using either the ASCII
or EBCDIC(EBB-SAH-DICK) codes. These codes assign individual characters to
a specific pattern of 0s and 1s.
• The table in the next slide lists the correspondence between bit patterns and
the uppercase letters of the alphabet used by the ASCII and EBCDIC codes.
ASCII AND EBCDIC
VARIABLES

• A variable is a location in your computer’s memory in which you can store a


value and from which you can later retrieve that value.
• You must tell the compiler what kind of variable it is (this is usually referred to
as the variable’s “type”).
• This information tells the compiler how much size to set aside and what kind of
value you want to store in your variable.
VARIABLES

• Naming a variable and specifying the data type that can be stored in it are
accomplished using declaration statements. A declaration statement has the
general form:
VARIABLES

• A variable name can consist of no more than 31 letters, digits, or underscore,


the first of which must be a letter or underscore and cannot be a keyword.
• C++ is case sensitive. A variable named x is different from X.
• Variable names should be mnemonics that give some indication of the
variable's use.
• Good functions and variables names tell you what the variables are for; using
good names makes it easier to understand the flow of your program.
CONSTANTS

• Constants cannot be changed while the program is running. If you need to


change a constant, you need to change the code and recompile.
• You must initialize a constant when you create it.
• Declaration
• Using #define
#define PI 3.14
• Using const
const float PI = 3.14;
ARITHMETIC OPERATORS

• Integers, floating point numbers, and double precision numbers may be


added, subtracted, multiplied, and divided. The symbols for performing these
arithmetic operations are called arithmetic operators.
ARITHMETIC OPERATORS
INTEGER EXPRESSIONS

• The division of two integers can produce rather strange results for the unwary.
For example, dividing the integer 15 by the integer 2 yields an integer result.
Since integers cannot contain a fractional part, the expected result, 7.5, is not
obtained.
• In C++, the fractional part of the result obtained when dividing two integers
is dropped (truncated). Thus, the value of 15/2 is 7, the value of 9/4 is 2,
and the value of 19/5 is 3.
INTEGER EXPRESSIONS

• There are times when we would like to retain the remainder of an integer
division. To do this C++ provides an arithmetic operator that captures the
remainder when two integers are divided. This operator, called the modulus
operator, has the symbol %.
ASSIGNMENT OPERATORS

• Assignment operators are used to assign values to variables.


INCREMENTING AND DECREMENTING

• The increment operator (++) increases the value of the variable by 1, and the
decrement operator (– –) decreases it by 1.
• The following statements are equivalent
Counter++;
++Counter;
Counter = Counter + 1;
Counter += 1;
POSTFIX AND PREFIX

int main() {
int i = 10, j = 10, k, l;
cout << "i = " << i << ", j = " << j;
k = i++; // postfix (first, k = i = 10, then i will be 11)
l = ++j; // prefix (first, j will be 11 then l = j = 11)
cout << endl << "now i = " << i << ", j = " << j << ", k = " << k << ", l = " << l ;
return 0;
}
IMPORTANT NOTE

• Avoid statements like x = x++; and x = x--;. Even though they may compile,
they do not give a clear or reliable result.
• This happens because x++ uses the old value of x and then changes it, while
the assignment also changes x in the same statement.
• The postfix operator returns the value before modification, and C++ does not
guarantee a safe meaning for such mixed use.
CONCATENATION IN C++

• Use + for concatenation to 2 string variables or at least one variable and


constants.
• Do not concatenate a string directly with an integer. Convert the integer to a
string first.
OPERATOR PRECEDENCE

Operators
()
++ -- from left to right
* / % from left to right
+ - from left to right
QUESTIONS

1) a+b/c-d is the same as


a) a+b/(c-d)
b) a+(b/c)-d
c) (a+b)/c-d
d) (a+b)/(c-d)
QUESTIONS

2) Which of the following expressions is zero


a) 8%3 - 1
b) 3 - 8%5
c) 9%3 - 1
d) 2 - 5%2
QUESTIONS

3) The following two C++ statements perform the same operation.


regWages = regPay + overTime;
regPay + overTime = regWages;
a) True
b) False
QUESTIONS

4) All C++ operators are evaluated from left to right.


a) True
b) False
QUESTIONS

5) The statement cout << "a = 5;"; is a typical example of an assignment


statement.
a) True
b) False
QUESTIONS

6) Given the algebraic equation y = ax3 + 7, which of the following, if any, are
correct C++ statements for this equation?
a) y = a * x * x * x + 7; b) y = a * x * x * ( x + 7 );
c) y = ( a * x ) * x * ( x + 7 ); d) y = (a * x) * x * x + 7;
e) y = a * ( x * x * x ) + 7; f) y = a * x * ( x * x + 7 );

You might also like