VARIABLES
AND DATA TYPES
Cout statement syntax
cout<<item;
• cout object
• Output operator <<
• Item types:
– Number: integer and float
– Character: printed and non-printed – String
– Expression
– Variable and constants
Variable
• A variable: is a place in memory that has a name and can hold data
• A variable: is a symbol that represent a storage location in the computer’s memory.
•Its value is the information that stored in the memory in that location.
data
Memory Model:
Boxes
Boxes store data
Addressed boxes
Address
Variable declaration syntax:
DataType identifier[=initial value];
Declaring more than one variable:
DataType identifier1[=initial value], identifier2[=initial value], ...;
Notes:
o [] means optional
oseparate identifiers by commas oone semicolon at the end
Variable
Variable Identifier
To input any data you should define a variable.
DATA ≡ Value Address
≡
Variable In
memory
Identifiers
• Identifiers are variable names
•Identifiers naming rules:
o Consists of letters, digits, and the under score character (_)
o Must begin with a letter or underscore.
o Don’t start with a number o No spaces
o No punctuation except underscore
o Don’t use keywords (reserved words): int, float, char, void, main, cout, cin, …….
o C++ is case sensitive—uppercase and lower case letters are different: SALARY is different than salary
Example # 1
The following are legal identifiers in C++.
first
conversion Pay_Rate counter1
Exercise
Are the following identifiers valid?
Average
First_Name
Balanc?e
3GPA
Address
Last name
Data Types
C++ data types fall into three categories
• Simple Data Type.
• Structured Data Type.
• Pointers.
Simple Data Type
C++ simple data can be classified into three categories
1. Integral, which is a data type that deals with integers, or numbers without a decimal part.
2. Floating-point, which is a data type that deals with decimal numbers.
3. Enumeration type, which is a user-defined data type.
Integral data types
1. int Data Type
int identifier ;
-6728, -67, 0, 78, 36782, +763,...
• Defining an integer variable that stores integer numbers
• The memory allocated for the int, unsigned int is 2 to 4 bytes, while the memory allocated for long and unsigned long data types are 4 bytes.
• Positive integers do not necessarily have a + sign in front of them.
• No commas are used within an integer.
Example int num;
long num;
unsigned int num;
2. float Data Types
• Defining a floating-point variable that stores floating point numbers
• Scientific notation
43872918 = 4.3872918 *10^7 {10 to the power of seven}, .0000265 = 2.65 * 10^(-5) {10 to the power of minus five}
47.9832 = 4.7983 * 10^1 {10 to the power of one}
• There are two notation to represent real numbers in C++:
– Decimal notation 35.5 – Exponential notation 3.55E1
float identifier ;
The memory allocated for the float data type is 4 bytes , for the double data type is 8 bytes, and for the long double 10 bytes.
Example
float conversion;
double payRate;
1. Storing Data
Storing data in the computer’s memory is like :
int x = 10 ;
Find place in the memory with size 4 byte As integer
type
Identifier :The name of this place
Optional : the value inside x int x ;
the computer will allocate default value
Storing Data into Variables
In C++ there are two ways that data can be placed into a variable:
1. Initializing Variables
Variables can be initialized when they are declared int first=13, second=10;
float x=12.6;
Equivalently, we can write the following C++ statements.
int first, second;
float x;
first = 13;
second = 10;
x = 12.6;
2. Using C++’s assignment statement
The assignment statement takes the form variable = expression;
The expression is evaluated and its value is assigned to the variable.
In C++, = is called the assignment operator.
3. Use input (read) statements.
int x = 10; Or int x; Or x=10;
int x;
x=3+7;
Example # 2
int I, J;
I = 4;
J = 8;
I = 7;
J = 5;
cout<<I<<‘ ‘<<J;
4 345
I J
4 8
7 8
7 5
7 5
543 345
I J
Default values
Example # 3
float amountDue;
int counter;
int x, y;
In C++, all identifiers must be declared before they can be used. If we refer to an identifier without declaring it, the compiler will generate an error message indicating that the identifier is not declared.
x=8+3;
int x;
int x;
x=8+3;
Displayin variables
•
Using the cout statement• Example:
int a, b, c, d;
a = 65 ; b = 78 ;
cout<<"a"<<endl;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<'\n';
cout<<d<<endl;
Output a
65 78
6749684 4203005
4. Types of statements
• The body of the main is enclosed between { } and has two types of statements.
• Declaration statements.
• Executable statements.
Declaration Statements
int a, b, c;
float x, y;
Variables (or identifies) can be declared anywhere in the program, but they must be declared before they can be used.
Executable Statements
Example
a = 4; //assignment statement
b = a+1; //equation statement
cout<<a<<endl<<b<<endl; //output statement
5. Form and Style
Consider the following ways of declaring variables.
int feet, inch;
float x, y;
or
int a,b; float x,y; int a,b,float x,y;
or
int feet;
int inch;
float x;
float y;
Blank spaces
int a,b,c;
int a, b, c;
The blanks between the identifiers in the second statement are meaningless.
In the statement,
inta,b,c; Error
no blank between the t and a changes the reserved word int and the identifier a into a new identifier inta.
Example # 4:
• Find the 4 syntax errors .
int x; //Line 1 int a;b; //Line 2
int y //Line 3
Float z; //Line 4 y = w + x; //Line 5
Constants and Variables
• Named Constant: A memory location whose content is not allowed to change during program execution.
• Variable: A memory location whose content may change during program execution.
Named Constant: The syntax to declare a named constant is const dataType identifier = value;
• In C++, const is a reserved word.
Example
const float conversion = 2.54;
const int noOfStudents = 20;
const float payRate = 15.75;
noOfStudents = 15;
Naming Identifiers
float a = 2.54; //conversion constant
float x; //variable to hold centimeters float y; //variable to hold inches
x = y * a;
Consider the following
float conversion = 2.54;
float centimeters;
float inches;
centimeters = inches * conversion;
Note: Use meaningful identifiers