C++ 1
C++ 1
C++ 1
Int roduction
Cttisa qeneral purposcprogamminglanquaqe and
1S Lwidely used nau-adauys ox Competive pr.cqiammir
i It runs lots of platforms like indows, linux, mace
iiIcan be used to develop opeiatmq susiemS brousers.
qamesand So 0m.
Ctt20 (Upcominq)
return 03
Date
Headers
tinclude iostream
uSing name space stdi
tinclude
I leis us work with inputEoutput obyects, Such as
namespace
It deines uhich inpul /_output fonm is to be used..
o u Laill understond this better as we proqress)
NOTE
Semicolon ( ) is used todemunate
a tt stalememt
main unctin
int maun ()
return 0
of function
int This is 1eturn type
Variables
iNariables are the containers to store data types
Data tupes
1 intinteqer value
2unsiqned int posttiveinteqervalue
3char characters tSpecialcharacterS
4.unsigned char characters
5.boolBoolean Tue 0 alse)
ssting: Sel characders
1 o n q : larqeinteger value
8 Shot& Smallinteaer value
9.loctr 1 decimal diqts o precision. Use en tor
10 double: more decimal
(15)
diqtsofprecisien.pauerd 10
NOTE
Use auto Keuwod, it uncentam
data tupe.
tor exanmple
auto a 13
auto b: Ti
Comments :
Mut- line_comments6
lheu start with U) and end oirth CD
Eor example
This is a
multi line comment "
Operators P
Operctors QTe special sumbol specitfie opercthons on one
Or mOre operandsuponwhich reBums a esult
There are ditferent dupes ot operators Cvaulable n
Cttthese aue
a. Arithematic operators
bUnaru Operators
C.Relationaloperators
d Loqical operartors
e Brtuise operators
Assiqnment Operators
M i s c aperatorS
Conclitioals
a Statement:
The i keyuwordis used to exe.cute aSTOement or
is fulfilled.
block, t and onlu if a conditiom
Suntax
i Ccondition) i
stotemenm do execude t condition ig drue
E.qint1e20:
if i > 10)
cout True
Output
b. -else staBement:
The i statement Hells_uS that a condition 1S
rue it WIll execute a block of statemets, but i
wont conditian is false
So usinq the else statement wHh the if statemet
we execute a
code block when the condition is false
Suntax:
if Ccondition) 1
Ilexecutes thisblock if Comdition is
rue
else1
lexecutes his block HCondttiom is talse
Eg.itnumber
Cin numberS
else
COuH & he number isodd endls
CNested statement
Nested statements mean an iB siademeni insideancihe
istatement.
Suntax :
ifCconditiond i
llexecutex conditi.oni is Atue
i Ccondithion2) 1
llexecutee iconditium2is true
t Int age
153
iCage=14) L
it Cage= 18)1
COut k Adult K endl s
else
cOut& eermaqer endl s
else
i (age0)
cout& Child endl
else
Couta Invalid cge < end
d.i-else - taBememt
IheCond1ttomal expressions are executed ntop- dau
1S toOUnd thot
dnher, hre as soon a s a conditiom
evaluatea o r u e , the statements aet executed
Syntax
it CCondition 1) 1
Il executes COndition is drue
else if Condituon 2)
Il executee if condHion2 s rue
else i
ll execute s il all condtions fai
e. Switch statement :
Suntax
Suitch expression)L
Case comstant1:
ILexecutes expressiom- comstant
break
case constant 2
I executes it expressibm comstant 2
break
delault
ILexecutes texpressiom dtes E Constants.
Loops
a or loop hai allotos us
1S a repetetion conttol structure
e x e c u t e d a specHic
num
ber of
LOte a loop h a t is
meS- steps
heloop enables us to perfomn nunmberof
Hoqether in one line.
Suntax
orintializathom expression COndition expre ssion
update expression) 1
J| body o loop
E . I t main O1
tor Cinli:1iE53++)
cout & "Hellol endl
b. lhile loap
Ina uhileloop the comdition is evaluated first, and
ifit retuxns true, thenthe statemeds inside the
while loop execute.
This Iepecledli happes u t l the condition etums
false
lhen the condHion retuInS iale, the condrol comes
Ourt of the loop and umps to he proqroms next
stateme after the while bop
Suntay
initialization
uhile (condition)
Il stakemends
updadeexpre ssioni
E.ginm main O1
int i1
while Cie5).1
cout The Nalue of i is i endl;
C Do wh1le loop
The do while loop is Similar to hile loop ith_
on=
af
cifference1e the conditiom is tesBed at the end
i executed al
the loop- body thus the loop body
Mhich make
leasT once XTespective ot heconditiom
an ext comtrolled loop.
Suntax
InttHalizattion
do
llstatements
updatt expre sSIm
ahile Ccondition) ;
Pagw a
EqInt : 2
do
uhile C41)i
Jlowchatts
aForloop
Start
Iniialisatiom
False
Condition pdale
True Cotmrle
FoT loop bodu
Stop
Start Starxt
False
Condition do... Lwhile
oop pod
Truc
condikian T s
Stop while loop body
Stop