Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

C++

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.

iv Las developed bu Biane Slioustrup in Ahe ealu


1980s 1993)
v t w a s inttlallu knouwn as C with classes and
was Ienamec Ctt in 1983.
N Narious Versions.:
C 98 1993)
C 03 2003)
C t t 11 (2011)
C+4 1 2014)
C+4 11 2020) CurYent

Ctt20 (Upcominq)

Basic Structure oftproqram


#include iostream Cin
uSing namespace std Cout &

int main Ui extraction operator


insertion opeator
ILCode here

return 03
Date

Headers
tinclude iostream
uSing name space stdi

tinclude
I leis us work with inputEoutput obyects, Such as

cin cout tespecivelu


1OStrecim
T stands for input- outputsiieam.
This header ile is necessaru to lake input throuah
user or print output to SCieen

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

main () This 1S dhe potion otanu Ctt code inside


which all commands aue execurted.
NOTE
AlL the code Lriten inside the
Curlu braces A) is said to be
in oneblock also Knouon as
A pathculcy unction scope

Variables
iNariables are the containers to store data types

Suntax: data tupe variable name value s

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 :

Single line comments


Stat with tuo forwar d slashesi 1/
Iheu
For excunmple
L This 16 a Sinqle ine Comment

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

number 2== o)1


COut he number 1seven K endls

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 :

Suitch case statements help in destinq the equali


of aariable or_expresslon against a set of values
and provide aaood alHternativeoT lonq i t statemems.

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

do-while loop end


coLH & Inside body of
tti

uhile C41)i

Jlowchatts
aForloop

Start

Iniialisatiom
False
Condition pdale
True Cotmrle
FoT loop bodu
Stop

balhile Loop C do while

Start Starxt
False
Condition do... Lwhile
oop pod
Truc
condikian T s
Stop while loop body

Stop

You might also like