0% found this document useful (0 votes)
26 views

FPL Assignment 3

Uploaded by

meabc2319
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

FPL Assignment 3

Uploaded by

meabc2319
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

D.>Describe different forms 0p p-else state.ment .

How do they
diffet
Jiere Cre severcl form f the ?-else statement
1). T{ tctementI£ epression i9 true. it enecute the
Statement otheruise. jump to ne1t instoucion
-It checks ohecthe given epyession is b0oleon o)
hot
Syrtan

if(enpression)
Stotement 1
Statement 2,

ex- #include <Stdio


#include <conio h)

Void mCin

iht a=5 b=6c


C=atb;
if (C= =11)

printf (C Eecute me 1 ;

Printf Emecute me 2");

NMIET
Jutput &Eecute me 1.
2 9 else, 9tatementItig used to check an condition
ahd
dependirg on the outcorme of any conditior1 Hhen
NE CCI tollan the qppaopzicte PBh

Gyntuti
if Cenptession)
State.ment 1:
Statement 2;

else

Stateme.nt 3;
State ment 4

Py:-Finclude<Stdio:h
tinclude. <coniço h)
Void mair1O

int maxks = SO;


i£ (marks >40)

printf ( student is pass

else

Printf (" student /s fail");

Qtput: SHude.t iS paGS


NMIET
3 Lfelse 1 Statement & It js sed to execuHe orie code
Taom nulHiple conditioh:
Syntan
1 CCondiHios

Statement 1i
else i£ (Condition 2)

Statenent 2

else if (condition 3)

Staternent 3;

else i? (corditiorn n)

Statemernt b;

else

default statement;

Ea #include<etdio-t
#itclude <conic-h
Void main
NMIET
it humbet =o;
Clrscr 0
Printf("EnBer the umber");
Scanf ("Id, & number):
Lf (humber = =10)

printf ("Number is equal to 40");


else if (number= =50)

Printf (" number is equal t0 so)}

else. i (number = =I00)

Priotf(Dumber ia equal to 400);.

else

printf (Number is not equal to 10, SO,100";

getch():

Dutput Enter the number = 10


Number js not equal to 10,

NMIET
QExplain hat,hat is the purpose Switch sttcrnent 3
&explaih dules associaB cd cotth the use of Switch Stotemerr
Soitch Statement i when there are several octions ard
we have checse only cne optior Trorm the available ornes
coe cah use ScoHch statement:

Syntm
Suitch Cenptession)
Case vCue 1:
staternent 1;
break:
Case Value 2:
Statemeht 2;
break:
Case Value 3:
State ment 3;
break;
default
Statenent;
break;

Rules:
he enpression follooing the keyboand ScoiBch must yeild
ike 1.
integer value Tt must be inteqer constant
The keuLOotds case is tolloced by inteqer r character
COnstant,each constant h each Must be di£fe. rent
4he other.
orm all
the
Airst keyoord
indeger enpre9SI0n tollootng the NMIEd
switch is evcluaBed. The value it gives is seasching
aacirat the constant values thct TolloOS the ase
StcHe.ent:
hen q ncBch
is founid the proatam emecuted the
statemer1t folloing the case Tf Do mHch is found
ioith only Of the case sBatementTollocoing tte default
are eecuted.

Ex &
#include<stdioh)
Void mcin

into:
Printf ("please erter no. betieen 4&5=:
Scanf "% d &a):
Stoitch (a)

COSe 1:
Ptiotf(" Yau cho0se one':
break:
CUse 2:
rintf (" Yoy chO0se. tao;
brecak
CClSe 3:
Print f(" You chocse. three";
break
CUse 4:
Ptintf ( You chocse Four ";
break,
COSe 5: NMIET
Pintf C You choose, Tive");
break;
default:
Printf Ihvalid choose ErBer opo-blw 4&5"):
bredk;

*Output:
Please enter a D0: beteen 1 &53
Youcho0Se. Three.

Explain the Narious loop construct ah enample


Givc
Por each &eaplain the. NOrking of the constant
Loop Stcteneti
Suppose coe have. print tible of 2then
need to orite 40 lines of code by usinq looping stalement
JAe cah do it ory in tao - three lines

1While loop i

Syntan
Varigble inidilization
Jnthile (cordition)

statements:
increment o Decternent

S evalucrted iret &if it is tFUe ther,body o£ loop


enecuted NMIET
LAAer enecuing the bods o¹ the loop the condition is
again evcluated &if it is true the body is enecuted
once agaih he procese 0 tepe cted eMecuted of loop
contin OUS until the conditior inaly become false and
contaol is transfor med out the loop
Ex
#include<stdioh>
tinclude<conio:h)
Void rnaint )

int xi * Output
1,2,3, 4,S 6 , 8 9 , 10
otile (n2=10)

ptint£ ("-d");

getch );

Do-ahile locpi

Syntam
Uiable initilization;
do

Statementi
Decrement
Increrne nt (oR

ohile Ccanditicr): NMIET


-Docohile i9 ah enter controlled loop stite rmerrt. body of
loop is enecuted first & ttrer the condifion i9
evaluc1ted iP it is true then the body of loop js
e1ecuted oce agaiti.
The pr OCeSS OF of eecution of body of loop i9
ontinous until 4he condiHion Tirc| becomne false K
condition ia txansfer to the Stcte.ment immediatl
dfter the loopD

Ex #include..<Stdio h
tinclude <conio-t

Void mgint )

int a,i:
q=5,= 1
do

pri ntf (" /-d",a ) :

ohile (i<=S)
geth ()
Dut put S10 1S 20

condition, Incxemert decrement)


Tor (Ihitilization
hta-
Statements, NMIET
loopiS an enter contsolled looping GtatemerntTn
For
Hhis loop more than one te Mriable carn be, initilized
coill be. taken at a time
the action
Variable initilization , conditiorn checking & incre ment
6Rdecrement
is more FleMible thdh other loop:
aft
incdude<stdio:h)
include<corio-b)
Void mcin)

for (1=1i1210;x+t:)

pintf C"");

getch;

00tputi 1 2 3 4 S 6 8 g 10

short note on sequsnce corntaol Statement and


selection contzol stotement'
sequence contool stctement
It ensure that the statement in a pgaa Qre
CIRCUted in the, oder they appear Dm top to
bottom:
hey cire Used to mcitain the. ogical floo oF
eneution NMIET
#include<stdio-h)
Void tmain )

b= 20:
int a= 10,
CaQtb
Priotf C: /dc):

Selection contol statenecti-


and enerue
a prognam to make desicion
blocks of code based on condition.
erific
specific
They are used for branching, egbili ng diferent
*
eValtior.
outcomes based cn

ea Gtateent

b;
pint f C"a is qreater than
telse
Print f (" b is greater";
IE -else statement

B');
itf ("a ja qxeater than

itStatement)

NMIET
Give the 5yto for 'for
suitablC cnample stoHernert and cnplain
Syntan: For (initializctlion . cord: incrernerit ||
decrerment
Statements;
eo # include <stdio-hy
int mainc)
:Output
Eoter a nurmber:2
int i=1 ,num
Pritf (" Eoer a humber:"); 4
SCan f d", &num): 6
for Ci= 1, iLEI0 i ) 8

Priot f(d\n numi):

7etun 0;

20

ivinitiglizaion i i= 1 (loop stquts odh i set to 1)


Cond; i o(Uoop as_/ i9 less thcrn OX
equal to s)
lorig-
) nczernert : i +t (Af ter each terction, i js inCAemete
by 1)

NMIET
y6Conmpase b/w for while' and do cotile' Ooops
Teatue Tor loop wbile oop do while loop
Tnitilization pet formed in done geperatlyIt i9 done before
the jntilization before the loop. the loop separaty
part oE the
loop syrta
2Condition It is checkedTH 0S checked It is checked after
Check before. enking before entering the enecion of locp
Hhe loop body loop body bdy at least once

3Iterationnckement/ incxementldecxement incrementJdecre


decrernerrt isis done, inside the ment js dohe
pst of loap loop bods inside the lo op
Sytan hody
Syntoam for (initlizctian cuhile (condn) do
condiion -?
incxemtt/ while cond;
Ldecrement.)
F-----}

NMIET
Explain baeak and cordinue th cnrrçk
Cotinue i- T is used to Skíp the urrent itertr
cf arny lcop & bring the cortl to ihe tesrira
of
of iteraticn

-The statements follocing the cortinut ctotrmtrte


Ore skipped &iterHion Gtat aguir.
#inclIde<stdio-h>
Void main )

for (int i=1iL=D;it+)

fcontinuei

Printf ("d \n" i)


3

Drtputi
2

10
NMIET
he, most u9ed jurmp statemert
Break:--It j9 one Þf
() nCbredk is uscd to prermuturlly enst
foom a loop o Soitch stutc rnent, before
the block 1s been fully eneauted

As soom d9 break Gtaterent is encountered


loop loop ter mincites irnrmidicty ard ortzr
inside. a the loe
toansfesed to nent Staterment utsrde
ie
Eai- #iclude <Stdiohy
int mainu

i4+)
for Cint i - 1 ; it=to

break

Printf ( /d n'

Setun 0;
3

Cutput: 2

NMIET

You might also like