Post on 08-Apr-2018
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 1/37
Variables and Data Types
ICT 112: High Level Language
Programming
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 2/37
What is a Variable?
A variable is a portion of memory to store a determined value.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 3/37
Identifiers
An identifier is a sequence of one or more letters, digits or underscore
characters (_) that is used to distinguish one variable from the others.
Each variable needs an identifier that distinguishes it from the others.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 4/37
Identifier Rules• Neither spaces nor punctuation marks or symbols can be part of an identifier.
• Only letters, digits and single underscore characters are valid.
• variable identifiers always have to begin with a letter.
• They can also begin with an underline character (_ ),
• but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as
identifiers containing two successive underscore characters anywhere.
• In no case should they begin with a digit.
• Identifiers cannot match any keyword of the C++ language nor your compiler's specificones, which are reserved
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 5/37
Some standard reserved keywords
Some of the standard reserved keywords are:
bool, break, case, catch, char, class, const, const_cast, operator,continue, asm, default, delete, do, double, dynamic_cast, else,
enum, explicit, export, extern, false, float, for, friend, goto, if,inline, int, long, auto, mutable, namespace, new, private,protected, public, register, reinterpret_cast, return, short, signed,sizeof, static, static_cast, struct, switch, template, this, throw, true,
try, typedef, typeid, typename, union, unsigned, using, virtual,void,volatile, wchar_t, while, and, and_eq, bitand, bitor, compl,not, not_eq, or, or_eq, xor, xor_eq etc. etc.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 6/37
Remember!
The C++ language is a "case sensitive" language.
That means that an identifier written in capital letters is notequivalent to another one with the same name but written in small
letters.
Thus, for example, the RESULT variable is not the same as
the result variable or the Result variable.
These are three different variable identifiers.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 7/37
Fundamental data types
When programming, variables are stored in the computer'sMemory.
the computer has to know what kind of data we want to store.
since it is not going to allocate the same amount of memory to store a simple number or a single letter or a largenumber.
Different data types are not going to be interpreted the same way.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 8/37
Data Types
Below are the basic fundamental data types in C++:
Char
short int
Int
long int
Bool
float
double
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 9/37
Data Type - Char
The char data type is used to store character or small integer.
When the char variable is declared in a C++ program, 1byte of space
is allocate in the memory for storage.
Range of Values that can be represented:
signed: -128 to 127
unsigned: 0 to 255
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 10/37
Data Type - short int
The short int data type is used to store Short (small) Integers.
When the short int variable is declared in a C++ program, 2 bytes
of space is allocate in the memory for storage.
Range of Values that can be represented:
signed: -32768 to 32767
unsigned: 0 to 65535
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 11/37
Data Type - int
The int data type is used to store Integers.
When the int variable is declared in a C++ program, 4 bytes of
space is allocate in the memory for storage.
Range of Values that can be represented:
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 12/37
Data Type - long int
The long int data type is used to store long (large) Integers.
When the long int variable is declared in a C++ program, 4 bytes
of space is allocate in the memory for storage.
Range of Values that can be represented:
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 13/37
Data Type - bool
The bool data type is used to store Boolean values.
It can take one of two values: true or false.
When the bool variable is declared in a c++ program, 1bytes of space is allocate in the memory for storage.
Range of values that can be represented:
true or false
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 14/37
Data Type - float
The float data type is used to store Floating point number (fractionsor decimals).
When the float variable is declared in a C++ program, 4 bytes of space is allocate in the memory for storage.
Range of values that can be represented:
3.4x10-38 to 3.4x1038
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 15/37
Data Type - double
The double data type is used to store Double precision floating pointnumber (fractions or longer decimals).
When the double variable is declared in a c++ program, 8 bytes of space is allocate in the memory for storage.
Range of values that can be represented:
3.4x10-308 to 3.4x10308
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 16/37
Declaration of variables
In order to use a variable in C++, we must firstdeclare it specifying which data type we want itto be.
The syntax to declare a new variable is to write:the specifier of the desired data type (like int, bool,
float...)
followed by a valid variable identifier.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 17/37
Declaration of variables (cont.)
For example:
int x;
Float TextMe;
The first one declares a
variable of type int with the
identifier x.
The second one declares a
variable of type float with the
identifier TextMe.
Once declared, the
variables x and TextMe can beused within the rest of their
scope in the program.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 18/37
Declaration of variables (cont.)
For example:
int a, b, c;
If you are going to
declare more than one
variable of the same
type, you can declare allof them in a single
statement by separating
their identifiers withcommas.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 19/37
Signed and Unsigned variablesSigned types can represent both positive and negative values,
Unsigned types can only represent positive values (and zero).
This can be specified by using either the specifier signed or thespecifier unsigned before the type name.
By default, if we do not specify either signed or unsigned mostcompiler settings will assume the type to be signed.
unsigned short int MyAge;
signed int GoalsAggregate;
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 20/37
The C++ Example
// operating with variables
#include <iostream>using namespace std;int main(){// declaring variables:int a, b;int result;// process:a = 5;b = 2;a = a + 1;
result = a - b;// print out the result:cout << result;// terminate the program:return 0;}
Results
4
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 21/37
Scope of variables
A variable can be either of:
Global scopeLocal scope.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 22/37
Global and Local Variables// operating with variables#include <iostream>
using namespace std;//global variablesint integer;char acharacter;float afraction;int main()
{// Local variables:int a, b;int result;// process:a = 5;
b = 2;a = a + 1;result = a - b;// print out the result:cout << result;// terminate the program:
return 0;
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 23/37
Global Variables
A global variable is a variable declared in themain body of the source code, outside allfunctions.
Global variables can be referred from anywhere
in the code, even inside functions, whenever it isafter its declaration.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 24/37
Local Variables
Local variable are declared within the body of a function ora block.
The scope of local variables is limited to the block enclosed in braces ({}) where they are declared.
If they are declared at the beginning of the body of a function
their scope is between its declaration point and the end of thatfunction.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 25/37
Initialization of variables
When declaring a regular local variable, its value is by default
undetermined.
But you may want a variable to store a concrete value at the
same moment that it is declared.
In order to do that, you can initialize the variable.
There are two ways to do this in C++:
C-like initialization
Constructor initialization
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 26/37
C-like initialisation
The C-like initialisation, is done by appending an equal sign
followed by the value to which the variable will be initialized:
It has the following format:
type identifier = initial_value;
For example,
int a = 0;
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 27/37
Constructor initialization
The constructor initialization is done by enclosing the initialvalue between parentheses (()):
It has the format:
type identifier (initial_value) ;
For example:
int a (0);
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 28/37
Initialisation of Variables
// initialization of variables
#include <iostream>using namespace std;int main (){
int a=5; // initial value = 5int b(2); // initial value = 2int result; // initial value undetermineda = a + 3;
result = a - b;cout << result;return 0;}
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 29/37
Strings
Strings are Variables that can store non-numerical valuesthat are longer than one single character.
The C++ language library provides support for stringsthrough the standard string class.
This is not a fundamental type, but it behaves in a similarway as fundamental types do in its most basic usage.
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 30/37
Strings – cont.
A first difference with fundamental data types is that inorder to declare and use objects (variables) of this typewe need to include an additional header file in our
source code:
<string>
And have access to the std namespace (which we alreadyhad in all our previous programs thanks to the using
namespace statement).
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 31/37
Strings – cont.
Strings can also perform all the other basic operations thatfundamental data types can, like being declared withoutan initial value and being assigned values duringexecution:
// my first string
#include <iostream>#include <string>using namespace std;int main (){string mystring = "This is a string";cout << mystring;return 0;}
This is a string
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 32/37
Constants
Constants are expressions with a fixed value.
for example:
a = 5;
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 33/37
Defined constants
You can define your own names for constants that you use very oftenwithout having to resort to memory-consuming variables, simply byusing the #define preprocessor directive.
Its format is:
#define identifier value
For example:
#define PI 3.14159265
#define NEWLINE '\n'
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 34/37
Defined constants – cont.
// defined constants: calculatecircumference#include <iostream>using namespace std;#define PI 3.14159
#define NEWLINE '\n'int main(){double r=5.0;double circle;circle = 2 * PI * r;
cout << circle;cout << NEWLINE;return 0;}
31.4159
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 35/37
Defined constants – cont.
This defines two new constants: PI and NEWLINE.
Once they are defined, you can use them in the rest of the code as if they were any other regular constant.
The only thing that the compiler preprocessor does when itencounters #define directives is to literally replace any occurrence
of their identifier by the code to which they have been defined
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 36/37
Any Questions?
8/7/2019 Variables and Data Types[1]
http://slidepdf.com/reader/full/variables-and-data-types1 37/37
Operators
Computer Programming in C++