of 24
8/19/2019 electrical engineer notes
1/55
Operators
Operators are the symbols that are used toperform certain operation on dataC++ provides variety of operator
Arithmetic operator Relational operator Logical operators
Bitwise operators
8/19/2019 electrical engineer notes
2/55
Arithmetic operators
The five arithmetical operations supported bythe C++ language are
Arithmetic operator is a symbol that performmathematical operation on dataE ample
+ for addition e.g. a+b- for subtraction e.g. a-b* for multiplication e.g. a*b
/ for division e.g. a/b%for modulo e.g. a%b
8/19/2019 electrical engineer notes
3/55
%for modulo
!odulo is the operation that gives theremainder of a division of two values" #ore ample$ if we write%a = 11 % 3;The variable a will contain the value &$ since& is the remainder from dividing '' between
("
8/19/2019 electrical engineer notes
4/55
Question
)rite a program that performs allmathematical operation on two variables
8/19/2019 electrical engineer notes
5/55
Assignment (=)
The assignment operator assigns a value to avariable" a = 5;This statement assigns the integer value * to thevariable a"The part at the left of the assignment operator ,- is.nown as the lvalue left value- and the right one asthe rvalue right value-"The lvalue has to be a variable whereas the rvaluecan be either a constant$ a variable$ the result of an
operation or any combination of these"The most important rule when assigning is the right-to-left rule% The assignment operation always ta.esplace from right to left$ and never the other way%
8/19/2019 electrical engineer notes
6/55
Example
// assignment operator 0include 1iostream2int main -3 int a$ b4a , '54
b , 64 A,bb , 74cout 11 8a%84cout 11 a4
cout 11 8 b%84cout 11 b4 return 54 9
8/19/2019 electrical engineer notes
7/55
Compound Assignment
statement An assignment that assign a value to manyvariables is .nown as compound assignmentoperator"E ample
A,b,&5:,y,;,'55
8/19/2019 electrical engineer notes
8/55
Compound assignment
Operator)hen we want to modify the value of avariable by performing an operation on thevalue currently stored in that variable we canuse compound assignment operators%C++ provide Compound assignmentOperator that combine assignment operator
with arithmetic operator
8/19/2019 electrical engineer notes
9/55
Syntax
,$ ?,$ /,$ @,$ 22,$ 11,$ ,$ ,$ ,-
8/19/2019 electrical engineer notes
10/55
Example
int main -3 int a$ b,(4 a , b4 a+,&4 // eDuivalent to a,a+& cout 11 a4return 54 9
8/19/2019 electrical engineer notes
11/55
Example
refiB,(4
A,>>B4// A contains &$ B contains &
ostfiB=3;
=B--;
// contains 3! B contains "
8/19/2019 electrical engineer notes
12/55
Relational and equalityoperators
Fn order to evaluate a comparison betweentwo e pressions we can use the relationaland eDuality operators"The result of a relational operation is aBoolean value that can only be true or false$according to its Boolean result")e may want to compare two e pressions$for e ample$ to .now if they are eDual or ifone is greater than the other is"
8/19/2019 electrical engineer notes
13/55
Example
==#$ual to=&ot e$ual to
'(reater t)an
ess t)an'=(reater t)an or e$ual to
= ess t)an or e$ual to
8/19/2019 electrical engineer notes
14/55
Example
, == 5 // evaluates to false. 5 ' // evaluates to true. 3 = " // evaluates to true. 0 '= 0 // evaluates to true. 5 5 // evaluates to false.
8/19/2019 electrical engineer notes
15/55
Example 2
a ,, *- // evaluates to false since a is noteDual to *"
a?b 2, c- // evaluates to true since &?( 2, G-is true"
b+6 2 a?c- // evaluates to false since (+6 2&?G- is false"
b,&- ,, a- // evaluates to true"
8/19/2019 electrical engineer notes
16/55
Be careful
The operator , one eDual sign- is not the same asthe operator ,, two eDual signs-the first one is an assignment operator assigns thevalue at its right to the variable at its left-
and the other one ,,- is the eDuality operator thatcompares whether both e pressions in the two sidesof it are eDual to each other"Thus$ in the last e pression b,&- ,, a-$ we firstassigned the value & to b and then we compared itto a$ that also stores the value &$ so the result of theoperation is true"
8/19/2019 electrical engineer notes
17/55
Logical operators ( !, &&, || )
The Operator H is the C++ operator to performthe Boolean operation =OT$ it has only oneoperand$ located at its right$ and the only
thing that it does is to inverse the value of it$producing false if its operand is true and trueif its operand is false"Basically$ it returns the opposite Booleanvalue of evaluating its operand"
8/19/2019 electrical engineer notes
18/55
logical operators && and
The logical operators and are usedwhen evaluating two e pressions to obtain asingle relational result"The operator corresponds with Booleanlogical operation A=I"This operation results true if both its two
operands are true$ and false otherwise"
8/19/2019 electrical engineer notes
19/55
Example
The following panel shows the result ofoperator evaluating the e pression a b%
2 #4 24a b a btrue true true
rue false false6alse true false6alse false false
8/19/2019 electrical engineer notes
20/55
Example 2
The operator corresponds with Boolean logicaloperation OR"This operation results true if either one of its twooperands is true$ thus being false only when bothoperands are false themselves" Jere are thepossible results of a b%
2 #4 24 b a 77 b
true true truerue false true6alse true true6alse false false
8/19/2019 electrical engineer notes
21/55
!or example"
5 == 5 3 ' 0 // evaluates to false true false . 5 == 5 77 3 ' 0 // evaluates to true true 77 false .
8/19/2019 electrical engineer notes
22/55
#recedence of operators
)hen writing comple e pressions with severaloperands$ we may have some doubts about whichoperand is evaluated first and which later" #ore ample$ in this e pression%
a = 5 + , % "8e ma9 doubt if it reall9 means:a = 5 + , % " // 8it) a result of 0!
or a = 5 + , % " // 8it) a result of The correct answer is the first of the twoe pressions$ with a result of G"
8/19/2019 electrical engineer notes
23/55
Operator Precedence (contd.)Operator Precedence (contd.)Example:Example: 10 * (24 /10 * (24 / (5 - 2)(5 - 2) ) + 13) + 13
10 *10 * ( 24 / 3 )( 24 / 3 ) + 13+ 13
10 * 810 * 8 + 13+ 13
80 + 1380 + 13
9393
8/19/2019 electrical engineer notes
24/55
Operator Precedence (contd.)Operator Precedence (contd.)
The order o precedence !n " ++ The order o precedence !n " ++lan#$a#e !% a% ollo&%:lan#$a#e !% a% ollo&%:
'n expre%%!on #! en !n parenthe%!% !%'n expre%%!on #! en !n parenthe%!% !%
e al$ated r%t.e al$ated r%t. Then m$lt!pl!cat!on * and d! !%!on / Then m$lt!pl!cat!on * and d! !%!on /operator% are e al$ated.operator% are e al$ated.
Then pl$% + and m!n$% operator% are Then pl$% + and m!n$% operator% aree al$ated.e al$ated.,n ca%e o parenthe%!% &!th!n parenthe%!%-,n ca%e o parenthe%!% &!th!n parenthe%!%-the expre%%!on o the !nner parenthe%!% &!llthe expre%%!on o the !nner parenthe%!% &!ll
e e al$ated r%t.e e al$ated r%t.
8/19/2019 electrical engineer notes
25/55
Operator '%%oc!at! !tOperator '%%oc!at! !t
The order !n &h!ch operator% ha% %ame The order !n &h!ch operator% ha% %ameprecedence are e al$ated !% no&n a%precedence are e al$ated !% no&n a%operator a%%oc!at! !t .operator a%%oc!at! !t ., an expre%%!on conta!n% %ome, an expre%%!on conta!n% %omeoperator% that ha e %ame precedenceoperator% that ha e %ame precedencele el- the expre%%!on !% e al$ated romle el- the expre%%!on !% e al$ated rom
le t to r!#ht or r!#ht to le t.le t to r!#ht or r!#ht to le t.
8/19/2019 electrical engineer notes
26/55
Operator '%%oc!at! !tOperator '%%oc!at! !t(contd.)(contd.)
Operator a%%oc!at! !t !n " ++ lan#$a#eOperator a%%oc!at! !t !n " ++ lan#$a#e!% a% ollo&%:!% a% ollo&%:
Operators Associativity() ++(postfx) --(postfx)
e t to r!#ht
+($nar ) ($nar ) ++(pre x) (pre x)
e t to r!#ht
* / e t to r!#ht+ e t to r!#ht
+ * / !#ht to le t
8/19/2019 electrical engineer notes
27/55
al$e and al$eal$e and al$e
al$eal$e,t !% an operand that can e &r!tten on the,t !% an operand that can e &r!tten on thele t %!de o a%%!#nment operator .le t %!de o a%%!#nment operator .
al$eal$e,t !% an operand that can e &r!tten on the,t !% an operand that can e &r!tten on ther!#ht %!de o a%%!#nment operator .r!#ht %!de o a%%!#nment operator .
8/19/2019 electrical engineer notes
28/55
Example
a = 5 + , % "; might be written either as%a = 5 + , % " ; a = 5 + , % ";
8/19/2019 electrical engineer notes
29/55
Example 2
a ,, *- // evaluates to false since a is noteDual to *"
a?b 2, c- // evaluates to true since &?( 2, G-is true"
b+6 2 a?c- // evaluates to false since (+6 2&?G- is false"
b,&- ,, a- // evaluates to true"
8/19/2019 electrical engineer notes
30/55
E%cape 5e6$ence%E%cape 5e6$ence% The%e are %pec!al character% $%ed !n control %tr!n# to The%e are %pec!al character% $%ed !n control %tr!n# tomod! the ormat o o$tp$t.mod! the ormat o o$tp$t.7!8erent e%cape %e6$ence% are a% ollo&%:7!8erent e%cape %e6$ence% are a% ollo&%:
Escape Se !e"ceEscape Se !e"ce #!rpose#!rpose a a 'larm 'larm ac %pace ac %pace ;orm eed ;orm eed n n "arr!a#e ret$rn "arr!a#e ret$rn t t Ta Ta < < 5!n#le 6$ote 5!n#le 6$ote = = 7o$ le 6$ote 7o$ le 6$ote
8/19/2019 electrical engineer notes
31/55
$ype casting
The process of converting the data type of avalue during e ecution is .nown as typecasting
Two types of type castingFmplicit type castingE plicit type casting
8/19/2019 electrical engineer notes
32/55
%mplicit type casting
Fmplicit type casting is performedautomatically by the c++ compiler"
8/19/2019 electrical engineer notes
33/55
,mpl!c!t T pe "a%t!n#,mpl!c!t T pe "a%t!n#
,t !% per ormed a$tomat!call "++,t !% per ormed a$tomat!call "++comp!ler. e.#. char + >oatcomp!ler. e.#. char + >oat >oat>oat
long double
double
long
float
int
char
Jighest data type
Lowest data type
8/19/2019 electrical engineer notes
34/55
Explicit type casting
E plicit type casting is performed by theprogrammer Ft is performed by using cast operator The cast operator tells the computer toconvert the data type of a valueKynta
type- e pression
8/19/2019 electrical engineer notes
35/55
Example#loat a$bFnt c4
A,'5"(B,*"&c, int-a @ int-b4Cout11c4
ResultKhows 5
8/19/2019 electrical engineer notes
36/55
$he si eof operator
The si;eof operator is used to find the si;e of anydata valueFt gives the number of types occupied by the value
KyntaKi;eof operand-
E amplecout11si;eof int-Result
&
8/19/2019 electrical engineer notes
37/55
comments
Comments are line of program that are note ecutedCompiler ignore comments and does notinclude them in e ecutable programComments can be added anywhere inprogram in two ways
Kingle line comments !ultiline comments /? ?/
8/19/2019 electrical engineer notes
38/55
Basic Input/Output
The standard C++ library includes the headerfile iostream$ where the standard input andoutput stream obMects are declared"
8/19/2019 electrical engineer notes
39/55
Standard %nput
The standard input device is usually the.eyboard" Jandling the standard input in C++is done by applying the overloaded operator
of e traction 22- on the cin stream" Theoperator must be followed by the variable thatwill store the data that is going to bee tracted from the stream" #or e ample%int age4 cin 22 age4
8/19/2019 electrical engineer notes
40/55
Example
include 1iostream2 int main - 3 int i4 cout 11 8 lease enter an integer value% 84
cin 22 i4cout 11 8The value you entered is 8 11 i4cout 11 8 and its double is 8 11 i?& 11 8" n84 return54 9
8/19/2019 electrical engineer notes
41/55
'a( )or*
)rite a program that adds two floating pointnumbers and shows the sum on screen)rite a program to calculate and print thearea of a sDuare
8/19/2019 electrical engineer notes
42/55
C++ ,anipulator
C++ manipulator are used to format theoutput in different styles"The manipulators are the most common wayto control output formatting
EndlKetwKhowpoint
8/19/2019 electrical engineer notes
43/55
Endl manipulator
The endl stands for end lineThe endl manipulator is used to move cursorto the beginning of ne t line
E ampleCout11Nhello 11endl11Ncomsats
8/19/2019 electrical engineer notes
44/55
"on%tant%"on%tant%
,t !% a 6$ant!t that cannot e chan#ed,t !% a 6$ant!t that cannot e chan#edd$r!n# pro#ram exec$t!on.d$r!n# pro#ram exec$t!on.
T&o t pe% o con%tant%. T&o t pe% o con%tant%.!teral con%tant!teral con%tant
5 m ol!c con%tant5 m ol!c con%tant
8/19/2019 electrical engineer notes
45/55
!teral "on%tant!teral "on%tant
,t !% a al$e that !% t ped d!rectl !n a,t !% a al$e that !% t ped d!rectl !n apro#ram.pro#ram.;or example;or example
!nt a#e 19 ?!nt a#e 19 ?
T pe% o !teral "on%tant% T pe% o !teral "on%tant%,nte#er con%tant,nte#er con%tant e.#. @Ae.#. @A
;loat!n# po!nt con%tant;loat!n# po!nt con%tant e.#. 10.22;e.#. 10.22;"haracter con%tant"haracter con%tant e.#. B'
8/19/2019 electrical engineer notes
46/55
5 m ol!c "on%tant%5 m ol!c "on%tant%
,t !% a name #! en to al$e% that cannot,t !% a name #! en to al$e% that cannote chan#ed.e chan#ed.
,t can e declared !n t&o &a %.,t can e declared !n t&o &a %.con%t D$al! ercon%t D$al! er
con%t data t pe !dent! er al$e ?con%t data t pe !dent! er al$e ?e.# con%t !nt F 100 ?e.# con%t !nt F 100 ?
7e ne 7!rect! e7e ne 7!rect! e
G de ne !dent! er al$e ?G de ne !dent! er al$e ?e.# G de ne Pl 3.141H93 ?e.# G de ne Pl 3.141H93 ?
8/19/2019 electrical engineer notes
47/55
Expre%%!onExpre%%!on
,t !% a %tatement that e al$ate% to a,t !% a %tatement that e al$ate% to aal$e.al$e.
,t con%!%t% o operator% and operand%.,t con%!%t% o operator% and operand%.e.#e.# ' + ?' + ?
Operator
Operands
8/19/2019 electrical engineer notes
48/55
#ro$ra%
G!ncl$de I!o%tream.hJ o!d ma!n() K
char ch1- ch2- %$m?ch1 B2< ?ch2 BL< ?%$m ch1 + ch2 ?
co$tIIC5$m CII%$m?M
8/19/2019 electrical engineer notes
49/55
O!tp!t
104eca$%e '5",, al$e% o B2< and
8/19/2019 electrical engineer notes
50/55
#ro$ra%
G!ncl$de I!o%tream.hJ G!ncld$eIcon!o.hJ
o!d ma!n()K
clr%cr()?%hort ar1 32ALA?co$t II ar1 II endl?
ar1 ar1 +1 ?co$t II ar1 II endl?
ar1 ar1 1 ?co$t II ar1 II endl?#etch()?
M
8/19/2019 electrical engineer notes
51/55
O!tp!t
32&'&- 32&'832&'&
eca$%e ran#e o %hort !% 32AL@ to32ALA.
8/19/2019 electrical engineer notes
52/55
#ro$ra%
G!ncl$de I!o%tream.hJ G!ncld$eIcon!o.hJ Gde ne P, 3.141
o!d ma!n()K
>oat r- area?clr%cr()?co$t II CEnter rad!$%:=?c!nJJ r?area 2.0 * P, * r?co$t II C'rea C II area?#etch()?
M
8/19/2019 electrical engineer notes
53/55
O!tp!t
ser i $ive i"p!t t,e" Area ie .isp aye. o" t,e scree"
8/19/2019 electrical engineer notes
54/55
#ro$ra% 0include 1iostream"h2 0incldue1conio"h2
void main -3
clrscr -4int a$b4
a , '54b , *4cout 11 Na+b ,N11 a+b 11 endl4cout 11 Na>b ,N11 a>b 11 endl4cout 11 Na?b ,N11 a?b 11 endl4cout 11 Na/b ,N11 a/b 11 endl4cout 11 Na@b ,N11 a@b 11 endl4getch -4
9
8/19/2019 electrical engineer notes
55/55
O!tp!t
a+ 15a- 5a* 50a/ 2a 0