You are on page 1of 24

Data Types

&
Variables

By: Madam Suhada Ishak

Data Types

All C++ compiler support 3 basic data tpye,


namely integer, floating point and character.

There are many other types of data; eg: short,


long, signed and unsigned.

A common mistake among new C++ programmer


is, the variable is used BEFORE a data types is
assigned to the variable.

REMEMBER!!! You must always assign a data


type to the variable before using it.

A Few Basic Data Types

Table below lists data types in C++.


Data Types

Keywords

To declare
variable of type

Integer

int

Integers, eg:
33000,45,2,-9,98765

Character

char

A single
character,, eg:
A,*,+,m,\

Single precision
floating point

float

A floating point
number, eg: 1.23,0.09,45.0

Double precision
floating point

double

A floating point
with double
precision.
Eg: 1.7976973 X
(10^308)

Variables

A variable is a data name that may be used to store a data


value.

A variable name is chosen by the programmer in a


meaningful way so as to reflect its function or nature
program.

Sumexamples
Total of variable
Average
Area_circle
Some
names
are:

Variables names may consist of letter, digits and


underscore ( _ ) character but must begin with letter.
Examples
of incorrect
names are:
456
(Sum) variable
% 2nd

Declaration of Variables

In C++, a variable must be declared before they used


in the program.

A variable declaration informs the compiler of the


name and type of the data variable will hold.

The syntax for declaring a variable is as follows:


Data_type v_1, v_2, v_3,,
v_n;

v_1,v_2,,v_n are the names of the variables.

Multiple variables of the same data types are


separated by commas.

Initialization

The process of giving initial values to variables is


called initialization.

C++ permits the initialization of more than one


statement using multiple assignment operators.

Examples:
x=y=z=0;
m=n=20;

Defining Symbolic Constant

In C++ programs, one may define a constant


using the keyword #define on top of the main
programs or functions.

The value stored in a constant cannot be changed


during program execution.

A constant is defined as follows:


#define PI 3.142
#define DayOfYear 365
#define HoursPerDay 24

Global Variables

Local variables are declared within the body of a


function, and can only be used within that function.

Variable can be declared globally so it is available


to all functions.

A global variable declaration looks normal, but is


located outside any of the programs functions.

This is usually done at the beginning of the


program file. The variable is not declared again in
the body of functions which access it.

Statements

A statement is a complete instruction to the


computer.

In C++, statements always end with a semicolon


(;), except for preprocessor directives such as
#define and #include

Most statement are composed of expressions and


operators.

Examples:

X=4+j;
Y=10-m;
Z= x+y;

Different types of C++


Statement
Output Statement
2. Input Statement
Assignment Statement
Conditional Statement
5. Loop Statement

1.
3.
4.

Output Statement

Are used to display information on screen.

The simplest form to used cout statements:


#include <iostream.h>
cout<<value<< value<<value;
cout<<variable<<variable<;
cout<<variable<<value;
The following are escape sequences which can be used in a
string literal in a cout statement.
\n = New line

\r = Carriage
return

\ = Double
quote

\t = Tab

\f = Form feed

\\ = Backslash

\b = Backspace

\ = Single
quote

Input Statement

These statement are used to read in information


from a keyboard or other input devices.

The simples form is used to cin statement is:


#include <iostream.h>
cin>>variable>> variable>>variable;

The symbol >> is called an extraction operator.

The operand after the symbol >> must be a


variable.

Assignment Statement

Values can be assigned to variables using the


assignment operator = as follows:
Lower=10;
Upper=100;
Step=8;
Total=75.62

Values can be assigned to variables at the same


time the variable is declared.
int Lower=10;
int Upper=100;
int Step=8;
float Total=75.62

Operators

An operator is a symbol that instructs C++ to


perform some operations on one or more
operands.

Here are the binary mathematical operators:


Operation

Operator

Example

Multiplication

x*y

Division

x/y

Addition

x+y

Substraction

x-y

Modulus

x%y

Order of Evaluation
Arithmatic Operators
Operation

Operator

Order of Evaluation

Parentheses

()

Multiplication
, division and
modulus

*,/ or %

Evaluated second. If there are several,


they are evaluated from left to right.

Addition and
subtraction

+ and -

Evaluated last. If there are several, they


are evaluated left to right.

Evaluated first. If the parentheses are


nested, the expression in the innermost
pair is evaluated first. If there are
several pairs of parentheses, they are
evaluated left to right.

Equality and Relational


Operators

In C++, relational operators are used to compare


expression.

An expression containing a relational operator evaluates as


either true (1) or false (0).

C++ notation for relational operators are listed below:


Operation

Operator

Example

Equal

==

x==y

Not equal

!=

x!=y

Less than

<

x<y

Less than or equal

<=

x<=y

Greater than

>

x>y

Greater than or equal

>=

x>=y

Examples
Expressions

Evaluate as

Interpretation

4==2

False

3>2

True

4!=7

True

(5+5) == (2*5)

True

Logical/Boolean
Operators

In C++, these operators allows the program to


test several relations (compound comparisons) in
a particular expression.

Below are examples of logical operators in C++:


Operation

Operator

And

&&

Or

||

Not

Continue..

Let T = TRUE and F=FALSE

Truth table for the && operator


P

P && Q

Continue..

Truth table for the || operator


P

P||Q

Continue..

Truth table for the !(not) operator


P

!P

Example
Expressions

Evaluate as

interpretation

(4==4) && (5!=3)

true

(4>1) || (5<2)

true

(3==2) && (4==4)

false

!(5==3)

true

Exercise

Assume that the vaRIABLES of x =1, the value of y =6 , the


value of z =15, a=k, b=y. Verify the output from the
expression below:
Expressions
x>9 && y!=3
x==5 || y!=3
!(x>7 && y!=22 || z<4)
a<=a && a>=z
b>=y || b!=n

Output

Exercise

Assume that the value of x is 2, the value of y is 5 and the


value of z is 15. Give the values of various expression
below:
Expressions
X<=2 &&
y==4
X<=2 || y==4
!(x>2)
!x>1
X>=2 &&
y==4

Evaluate as

interpretation

You might also like