Download - Lecture- Variable & Data Types
-
7/24/2019 Lecture- Variable & Data Types
1/24
Data Types&
Variables
By: Madam Suhada Ishak
-
7/24/2019 Lecture- Variable & Data Types
2/24
Data Types All C++ compiler support 3 basic data tpye
!amely i!te"er #oati!" poi!t a!d character$
There are ma!y other types o% data e": shortlo!" si"!ed a!d u!si"!ed$
A commo! mistake amo!" !e' C++ pro"rammeris the (ariable is used B)*,) a data types is
assi"!ed to the (ariable$
REMEMBER!!! -ou must al'ays assi"! a datatype to the (ariable be%ore usi!" it$
-
7/24/2019 Lecture- Variable & Data Types
3/24
A *e' Basic Data Types Table belo' lists data types i! C++$
Data Types Keywords To declarevariable of type
I!te"er i!t I!te"ers e":
33.../01224560
Character char A si!"lecharacter e":7A88988+88m888
Si!"le precisio!#oati!" poi!t
#oat A #oati!" poi!t!umber e": ;$132.$./0$.
Double precisio!#oati!" poi!t
double A #oati!" poi!t'ith doubleprecisio!$
)": ;$55653 3.4
-
7/24/2019 Lecture- Variable & Data Types
4/24
Variables A (ariable is a data !ame that may be used to store a data
(alue$
A (ariable !ame is chose! by the pro"rammer i! amea!i!"%ul 'ay so as to re#ect its %u!ctio! or !aturepro"ram$
Some e@amples o% (ariable !ames are:
Variables !ames may co!sist o% letter di"its a!du!derscore = ? character but must be"i! 'ith letter$)@amples o% i!correct (ariable !ames are:
Sum Total Average Area_circle
/06 Sum " #$d
-
7/24/2019 Lecture- Variable & Data Types
5/24
Declaratio$ of %ariables
I! C++ a (ariable must be declared be%ore they usedi! the pro"ram$
A (ariable declaratio! i!%orms the compiler o% the
!ame a!d type o% the data (ariable 'ill hold$
The sy!ta@ %or declari!" a (ariable is as %ollo's:
v_1,v_2,,v_n are the !ames o% the (ariables$
Multiple (ariables o% the same data types areseparated by commas$
Datatype v_1, v_2, v_3,,vn
-
7/24/2019 Lecture- Variable & Data Types
6/24
&$itiali'atio$ The process o% "i(i!" i!itial (alues to (ariables is
called i!itialiatio!$
C++ permits the i!itialiatio! o% more tha! o!estateme!t usi!" multiple assi"!me!t operators$
)@amples:
@y.m!1.
-
7/24/2019 Lecture- Variable & Data Types
7/24
De($i$g Symbolic )o$sta$t
I! C++ pro"rams o!e may de!e a co!sta!tusi!" the key'ord Ede!e o! top o% the mai!pro"rams or %u!ctio!s$
The (alue stored i! a co!sta!t ca!!ot be cha!"edduri!" pro"ram e@ecutio!$
A co!sta!t is de!ed as %ollo's:
Ede!e FI 3$;/1Ede!e Day%-ear 360Ede!e GoursFerDay 1/
-
7/24/2019 Lecture- Variable & Data Types
8/24
*lobal %ariables Hocal (ariables are declared 'ithi! the body o% a
%u!ctio! a!d ca! o!ly be used 'ithi! that %u!ctio!$
Variable ca! be declared "lobally so it is a(ailable
to all %u!ctio!s$
A "lobal (ariable declaratio! looks !ormal but islocated outside a!y o% the pro"ram8s %u!ctio!s$
This is usually do!e at the be"i!!i!" o% thepro"ram le$ The (ariable is !ot declared a"ai! i!the body o% %u!ctio!s 'hich access it$
-
7/24/2019 Lecture- Variable & Data Types
9/24
Stateme!ts A stateme!t is a complete i!structio! to the
computer$
I! C++ stateme!ts al'ays e!d 'ith a semicolo!=? e@cept %or preprocessor directi(es such asEde!e a!d Ei!clude
Most stateme!t are composed o% e@pressio!s a!d
operators$
)@amples:
-
7/24/2019 Lecture- Variable & Data Types
10/24
Di+ere$t types of ),,Stateme$t
;$ utput Stateme!t1$ I!put Stateme!t
3$ Assi"!me!t Stateme!t/$ Co!ditio!al Stateme!t
0$ Hoop Stateme!t
-
7/24/2019 Lecture- Variable & Data Types
11/24
-utput Stateme$t Are used to display i!%ormatio! o! scree!$
The simplest %orm to used cout stateme!ts:
Ei!clude Kiostream$hL
coutKK(alueKK (alueKK(alue
coutKK(ariableKK(ariableK
coutKK(ariableKK(alue
The %ollo'i!" are escape seue!ces 'hich ca! be used i! a
stri!" literal i! a cout stateme!t$! Ne' li!e r Carria"e
retur!O Doubleuote
t Tab % *orm %eed Backslash
b Backspace 8 Si!"le
uote
-
7/24/2019 Lecture- Variable & Data Types
12/24
&$put Stateme$t These stateme!t are used to read i! i!%ormatio!
%rom a keyboard or other i!put de(ices$
The simples %orm is used to cin stateme!t is:
Ei!clude Kiostream$hL
ci!LL(ariableLL (ariableLL(ariable
The symbol LL is called a! 7e@tractio! operator$
The opera!d a%ter the symbol LL must be a(ariable$
-
7/24/2019 Lecture- Variable & Data Types
13/24
Assig$me$t Stateme$t Values ca! be assi"!ed to (ariables usi!" the
assi"!me!t operator 77 as %ollo's:
Values ca! be assi"!ed to (ariables at the same
time the (ariable is declared$
Ho'er;.
Ppper;..Step4Total50$61
i!t Ho'er;.i!t Ppper;..i!t Step4#oat Total50$61
-
7/24/2019 Lecture- Variable & Data Types
14/24
perators A! operator is a symbol that i!structs C++ to
per%orm some operatio!s o! o!e or moreopera!ds$
Gere are the bi!ary mathematical operators:-peratio$ -perator E.ample
Multiplicatio! 9 @9y
Di(isio! Q @Qy
Additio! + @+y
Substractio! 2 @2y
Modulus R @Ry
-
7/24/2019 Lecture- Variable & Data Types
15/24
-rder of Evaluatio$Arit/matic -perators
-peratio$ -perator -rder of Evaluatio$
Fare!theses =? )(aluated rst$ I% the pare!theses are!ested the e@pressio! i! the i!!ermost
pair is e(aluated rst$ I% there arese(eral pairs o% pare!theses they aree(aluated le%t to ri"ht$
Multiplicatio! di(isio! a!d
modulus
9Q or R )(aluated seco!d$ I% there are se(eralthey are e(aluated %rom le%t to ri"ht$
Additio! a!dsubtractio!
+ a!d 2 )(aluated last$ I% there are se(eral theyare e(aluated le%t to ri"ht$
-
7/24/2019 Lecture- Variable & Data Types
16/24
E0uality a$d Relatio$al-perators
I! C++ relatio!al operators are used to comparee@pressio!$
A! e@pressio! co!tai!i!" a relatio!al operator e(aluates aseither true =;? or %alse =.?$
C++ !otatio! %or relatio!al operators are listed belo':
-peratio$ -perator E.ample
)ual @y
Not eual @yHess tha! K @Ky
Hess tha! or eual K @Ky
reater tha! L @Ly
reater tha! or eual L @Ly
-
7/24/2019 Lecture- Variable & Data Types
17/24
E.amplesE.pressio$s Evaluate as &$terpretatio$
/1 . *alse
3L1 ; True
/5 ; True
=0+0? =190? ; True
1 i l2B l
-
7/24/2019 Lecture- Variable & Data Types
18/24
1ogical2Boolea$-perators
I! C++ these operators allo's the pro"ram totest se(eral relatio!s =compou!d compariso!s? i!a particular e@pressio!$
Belo' are e@amples o% lo"ical operators i! C++:
-peratio$ -perator
A!d &&
r UU
3ot !
-
7/24/2019 Lecture- Variable & Data Types
19/24
)o$ti$ue44
Het T T,P) a!d **AHS)
Truth table %or the && operator
5 6 5 77 6
T T T
T * ** T *
* * *
-
7/24/2019 Lecture- Variable & Data Types
20/24
)o$ti$ue44
Truth table %or the UU operator
5 6 5886
T T T
T * T
* T T
* * *
-
7/24/2019 Lecture- Variable & Data Types
21/24
)o$ti$ue44
Truth table %or the =!ot? operator
5 !5
T *
* T
-
7/24/2019 Lecture- Variable & Data Types
22/24
E.ample
E.pressio$s Evaluate as i$terpretatio$
=//? && =03? ; true
=/L;? UU =0K1? ; true
=31? && =//? . %alse
=03? ; true
-
7/24/2019 Lecture- Variable & Data Types
23/24
E.ercise Assume that the (a,IABH)S o%x ; the (alue o%y 6 the
(alue o%z;0 a7k8 b7y8$ Veri%y the output %rom thee@pressio! belo':
E.pressio$s -utput@L && y3
@0 UU y3
=@L5 && y11 UU K/?
aK7a8 && aL78bL7y8 UU b7!8
-
7/24/2019 Lecture- Variable & Data Types
24/24
E.ercise Assume that the (alue o%x is 1 the (alue o%y is 0 a!d the
(alue o%zis ;0$ i(e the (alues o% (arious e@pressio!belo':
E.pressio$s Evaluate as i$terpretatio$