0% found this document useful (0 votes)
2 views46 pages

Conditional Programming

The document discusses conditional execution in programming, focusing on concepts such as sequential execution, conditional control, if-else statements, nested statements, and switch statements. It explains how conditions determine the flow of execution and provides examples of how to implement these constructs in code. Additionally, it covers the use of loops and the goto statement for controlling program flow.

Uploaded by

ar9912069
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views46 pages

Conditional Programming

The document discusses conditional execution in programming, focusing on concepts such as sequential execution, conditional control, if-else statements, nested statements, and switch statements. It explains how conditions determine the flow of execution and provides examples of how to implement these constructs in code. Additionally, it covers the use of loops and the goto statement for controlling program flow.

Uploaded by

ar9912069
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Unit 3 Conditional Fro sam Gxecution. sae Ataterunts una peer axe executed un the oder tn which % ca Ln pe yarn. This up 7 execution. 1s Called Sepuantiol execution ZF Some times ft L necessary to * Select q ge ALaturtents ger Aevocal alkucnatives . * Skip cextatn Statements debencling on some Gondthions Ln tho. Cx. Cutior gon some othux. petit : *% Re on Qa oe det ments fora Anown. number 7 mes ot. Ente a 4 eaped Condition uk i ‘led. Condition Contzol prams and Continue the, Statonents * Some time it fs NECA 40 Check, the condition, zy OUR deel gion. ¢ ee - : stvitues pesfooming a Aoytar tet th sw ® Petes on the condition 4 joe btodimmant whith Vgratement wilt exceutt next 45 olelxminad. es Hot, the control ds peansfecd 40 that Statement & stands execution ye 1 that potnt: * This enti procs us Anown as tondétonel execution "which tidudes beth deciuon mak’ & ce a Statement x “q Sf stoferment the Logical condstion L& teste uobich sedulld in eithud tees. ie K Of the susudt os cue (nonkex) then Atatements that | jolew ye executed. * US the nell ts Jobe (Kee) then contyol trans to next executable dtatement that L . 2D cl fotos sit gees Fite d oe ) ae C Condition) © secant eens eee ure: Condition > logical expssion that susult sn a Anue 6% folie Atetomant -2 simple or compound Atoatunent Note: er Simple toy Atehernont) no besaces oe. Suepubeed. ig main ( ) tne awn; ee C" Erte a numbex \n'); oy e tock", berm) « YY Crum == 0) fog, @ You entoved Keno! )5 J ¥ main ¢) ” float x, 4, Hatio ; pert] C" Gwe X LY \n)5 eer ae “Up 4 ( 1attio > 0) . punt, (" Radin as pruater than Zva\n J Jd C" You axe sig i landition\n ie ("Radin ts teas tan ov gual to den’); 4 Sf- Else Statement x If dtoterent us ube to Creculn only one action, Of tow aw two dtafements to be. executed. anand than sie dhote ment Ld uubed - * The. — Alohment 3 a two be ieee © = — ‘ d Ctondsteon) Atatementl; if else Statement 2 5 5 % The Condition us tested and ri the Hesutt 2 this loxical Xut us Dru, thin dtatemunt 1 6 Cxe cuted ‘athxwise. Akalement? 4s erecutid.. * A Z ‘ os Cmscuting One of these tuo Ataterart, the, lontvel thansfers to th. dtatemert conme ee CS ae hie Sturctune . __ tau fe ae es ie main) unt m,n; fa ("Griue te vation. menn'd; Acar} C4 hd’ em, kn); Yy (m2=n) t pee C" Beth numbers ase gquatin.)5 3 iy ele. hip ost axe not push In); 4 euatat Cosel. Cased. he M&e NV Gitex, Value of MeN Db < 26 4y Both numbers ae ual Both numbers are not eyed main G pour OE a Chomactw\n ); oes Che, & CA); Cch=='4' I] Cha='y') ee C' Yes"); ply ci No") 4 Nestecl SY statement ah thew az mew than two altyenalives Lo sebe, than the nested -4f Atotiments axe used. * ee 4 within ae us Collid a nested d Alatiment | ee : d Cuonct'ton1) = 7d Ctondttion 2) ant if che Atotimert a> 5 4 cut C tondt’on 8) i AFatiment 3, 4 else. ne 7 Peoatary to -fcnd est number cut of gtven 3Nlog 4 Sa 2 t unt a, bcs punt] C" Entuo the value of A,B ew); Acay Chd Ld thd, tate); df (a>b) Ys Ca>e) ime ig) ee ee paiay Ce. sages), sat bse) ; a CR us — Dy else oe are 8 luk a Program to accept two und Coovclinale q ‘ca & ae ls Traced endude < dtdio- A> Jnclude <[Link] Votd mainc) unt %, pet eC ete, the value wy ay fora fate Scant td Xd", bx, by); Cx>0 ay >o0) at C" Pott ties in oust fusdxant\n. i Cudse sia ae po ( : feird b's En fowdh ouadvant\n'): ee v& gro ey C"fotrt Lies tr Aecond gquadkart” in); # ( 0 kL Yeo) fs C" Point Dies ty Thicd quaosxant \n'); (1320 &R yS=o) eo Cfoid Les kt onspin J; ; 4 Dowling Else. febl Else. hoblen foblem ‘ Sn se - elke Atatement, thou. exists a pre ken When show 1s no matching else Jov ev' This problem Js Rnown as Dastey elke poblm ; “4 : ( tonattion1) 127 /Conattiond®) / | Motment L - 7 ele] — btatemunta dolushon. (Gy) Cexpussion!) O s ey ae exprustion Z) rs as. ger i a) , Z 1 ele! Aker 7 bealy of ete Test clic ladder, * To make, mulhuny decision based on Several. tondition “f- leew (adldex. fs Uldtel . * AS secon as one o) He over ¢ ov ae Gs a tue pudult, HE following Atatement 6 “block, ts exetubd , & np futur Compaxigions ane i i xy none ef the Condition ib tune , fe ee else, 45 exrcuted % Fihal else ts optional , 4 poe else a not pases no action took “place even 4 all the conclitien aste aad Sates 4 (condition) Abatement ; elie. q C Gonaiton) Atatament ; else 4 Ctsnotitron ) Atoterment 5 elke AbMIEnt, + 4 (sesultS=-F0) ey C" Gost. AnD ; he ( sesutt = 60) pty (" Grade B \n) else ¥ C sult > = Yo) fig C" bsoole c\n"); ele. : pony Fail”); de tli a cea, mam 11 C to test Whether the poe Oya Js hea yore ot not. Ae snulude < Molin AD Rad includ < Conty-A> void main) 2 (baerO); unk yeas pes C" Ente the qe tn be checbadlly). ea € ‘td’ & Ka); G? %4==0) L potty Yen sb tenp); else. ia ("Yea bs neta leh yet) gtr Oy; _ Ulsite a program Jo check whthue a VMs e ae oe even”, a numbe us Perogpsarn He Undude < &tc’o. AD 4 unclude < conto. AS Vatd main ) Ckeoec). ‘ 2 = ns Ping Crt am rn) a Beer : : oe tod , &n)s CN%R==0) : nee C"Number even"); Cana [=~ 0) ‘ ay C" Mambee ah Odd") uh f quence >; i de (ab culakion d Arua 9; Cache OL ig as de pencliin up n chortle 7 Lbs G a ent “a Ces RS Progsa—- de undude < dtoloe-AX # uncluoe. < Conor Votd math ¢ ) : Chaw Cth; oak ol, a8 5 Hes 5 guna C° Ende © dor eee circle OR R for en (otto Acanp C" “Ze, & CA) S ap Geka Chee © pata CG ‘ eden) 5 C" Enter Raduus 7 circle nd 5 on CH wal)’, qgua= 34x areal 5 a C" The axon 7 ctiscle. 8 a si G 4 Cchse R i ee vy re * Ene tg wotelth 2") 5 dcanp (°% M's &aL, baa); 4 ad OL OR 5 tae pun i pee? see ee Ms of oxen) 4 a 4 Swttch Statement : * Switch Ataterunt uw a ie cec'wion that tests whathow an eaprussion matcArss of the @ numbex ¢f Constant gee values ane,” branches Accordingly. OY % This Ja anothoe joem g mutt coay ole cesren. ( G Salen y ae C expeusaton ) Case. Valued + Alatiments buen ; Case Value ds Atatemmeniz Case. Vabite,3 2 Afotime bsuak; 2 aieuee 5 x The werd dusttch cy antes 4 Awttch, Vauable. 0% eapresaion in sposurdhedis 'o Lowe ey a constant, Afatamant % The Coase Kyword gue by colon. x Whan Awiteh Statement 12 = ee ex poussion Us evaluatid and uy value 14 Acne compased cwtth, Case constants. ® Whan a match ts found, the statement Sequence, Uwsouated that “case ts executed untill the. ~hswak Atetement 0%, end of switch ss suachool * Default statement us executed. v nv match, ws oe Prog. a He thotude < sto. AN vetd main ( ) i unt ni, n2, of; poy C Ente two numdext') « & a Chd yd" bens, & m2); intf Cin. Meru’) 5 int{ C'n L- Addition’); C\Ind- substraetion'): po C"ln 3. mutliplication') 5 pu C"In 4. héveston) : punt Cn Select Yous. chocce entering d a Number!) + A d sf Cha, ia Awiteh Cop) 4 (asc i s, Samia peu In Adclition 13 Yd, rain, bucak: “ Caged? peg CIn i fae i td, Mao); buak 5 Case 3é § : eee ts td, nixne) bsulak + Case 4: pe ChQuotient us hd, nt ]nz Dy Py C'\n Kamasnclex Us td‘, nt &n2); Z uy x Be. ae libute a Fer. thet calculate con of Cacchk, Ke tanglk, Dufangle , Aguant oy fam, 3 He unduds < Aton h> 4 undude < Con’o. A> votd marin) 2 buat); wnt Coda 5 loat Ade, ‘ d baat Jee Auth, erg th, bacth prrdg C Menuln d; pat Cin L- coach "D5 i ("\n & Squane J; a ("Ine Teelangle"), ef C\In4. in a pair (“n6rtese you cholo. . ending number) ; Acard C'%dl." ke tole) Awttch Code) Coase ¢ ; : pun Cin Ente Radius curcke 5 Aca ee & saoltus): ama = “eltk Hadius HOCUS “paint C'\n the hed cuecke 43 uy axeg, break» Cobe 2° pay Cn Ent Acole 2 Apuasee”) 5 = Cc p & Avole) ; sea = Atle ye dette 5 ey ("In the mee Apuasee 45% oxen), beak 5 Case 3% aoe Ey ee oe Cad: & bake, de fay Pt); OXQQ = OS base x Kee’ ALB “pac C'ln Ascasy Sight a E area), bron 2 Case Ys perry ("In Ente length & breath of Rectangle!) say CU h ; beng th, & bear) aua= Lengtn x buadth; peg Cn Aca af Rectangle As Uf, 00a)» buak + defautt : pee C"\n Error un Cod") 5 0; J z Qo. sur a preg ram tp ee a Lette & i just guade bs fu gen ake lative Reman “$. Super. A Vesey Lrool 8 Pore, 4 Absent E Facl. re og te nelude < Atol’o AS +e unclude < conto. AX Vocol marin ¢_) t Chas, Lette ¥ ‘pun C" Entue the. letty\n") s Acant C40", & better); Aurctehr C Lethe) fa Eose 5: u " ~putey C Supe\n ag Peds Serger Ast pre C" Ves Good 1n') ; cs ¢ ar = C" Faidth); break s Case Case. 'B F Case “Y’ + purty C" Absent \n''); burak Case. 'P’: peg C" Fett |n")s beak; default : ; peer ce i cAecece" i 2 GoTo dtatement peds unconditional conte > Cc Langue e lore, the coribol. Statement goto” to txo pom one point do another Jn Po Syntax gee abel 5 whee. oto > a Reyword, Label > a Aymboli. Constant whva. the Contreal will bares ee Net? A (abel can be placect anywhexw un the. o ea either, tie 5. Sa pe dtatement 3 “4 ote END: i), d ce STRAT CS ed oe : ge ene Faye He uncdude < dtelio- AS te unclude < Contd. A> ved main ( ) unt oak 5 r : ae ae oS uo 7 ie , “i C goals <= 5) ; gow Sos 5 else, pent ( Girot Player!) ; 4 Sass ‘ pong} C oF Your Beat) y 4 a hoping => og he. ao ea Lectniguae wough which ‘d Alatements He executed, sufeately ee a «Guage condition ws si see => me 4s also Caled a Hefetitive oe an eseative Corttpl mechanism. => hoop cme toro pasts , one 4 pba ce) i and. & Calle conte Alaternent . ae eum Lorteal teat whose Hest ds elthon Ct folic. g eee a ae Ctra, othsuise Loo PY control. statment J plrad befoue the bo le, Lt is let amy Control Lays vo PRONE eat YO (body) Lt cabted ent Cort olleol Lop > Thur gone nary mechanism ane O Initialisation Te dt snthol — low courttie @ Dewsion’ A ost Concli’ttin to dectle whethrn loop toil execute ot rot Ou dation + Monee or chousrenting countess alia. a | loops © While statement D clo- while statement 3 ee dtatunent (oh hoop Uohile, Loop executes untill the ipndéhon igs 0 whit nee ye oe Cexpesseon ‘3 See oheste whils > Ty a Reywotecl eaneition > logdeat exprestion That seesults rithor Shue ie Makmenk> — dtalemint which cet be while * In while Loop , juty the topteot exprussron 4 tuted anol ‘suobule Ls Lou , the, Statements cr while hudy Wolf executes, tals oviba puccess d i rte Cordis inti the lopical empeaion become, Fe : %* This 14 also called as prtastect loop. * The value 4 Loop Counti, will oe ane each, note [ee op corde doesn't changes tn each poss, the oops wilt entuss into Anférits loop wondi'tion Prorram b port yen name un cesvud a bimes te induce < btn. b> Vord maine ) wnt &5 ent cy ped ie sles a Limes an “ne een ee Chd', & KR; "20% Bee) whik CL£ This 1s Colhed ost Lest Loop pecaust the test ts made at tw ae cack fats dyrton. do Atoturens oF whi Cenpiusston) 5 Nat irae 7 dowhile wit executs otleash one because the opal tut ts costed out at the end Vee (ip The bo publ ida ceils mocily te vonlable tubd in the topical expression onitin as & ae to oe oo all odd teehee ees te tndudle < stdio-h > veld maine ) Nes unt odel_rum =} bum =0 5 a éun+ = Pao Tus Odd-num += 2s while (odd_num <=50) 5 pag Cum = Hn, um) 5 cs Be funk dye ae Resnbow ad eae whe, FE Uncluds Vatd man ) Chev a int Colos- ode, Choc 5 ”s C Entw Colow Coole \n') ; CK & alex. Cooled; dwitch C Color cook.) a j Cae pert (color is voilet \n D5 dear 5 Case ay Ks ae tian trai Vlg Case 3s oe ttt. ts Blue\n'') 5 beak | J Oe 4s aoe " plot, 1s Gxuan\n') j ae ping C" Colov Js Yelbowo\n') , use 6: : page Coley us heeled back j Ger ae C tlow ti Red \n"); beak ; ce get C" Parnbow bod es z ane): “poy Pe pois staan? Chd’, bowie), While ( Choces =a4); a te qn sum eo Boys gy ene Avues HH Ancudle < stolid. AS dindude < tonto. h > Void main €) unt n, Abn, sonst ; fet 4um=0, AUN, num, oen+ POG C Ente the rum ae Chea ba), Atgn= L; ae inseaxi), nhum=L5 dan = 13 Const = 13 alo x ee = (num/den) x dv'yn 5 A um+ toxm ; dant +; dign = El) 5 >» Const++ 2 while (const <=n) 5 poof cary Aves = nef \n"', dum) a Fee hoop > for Lop us wbed when the proprmmee Rnouss ow ss tim a ae Atatererds are to be exeeutid.. mtorr Sfp 5 epetain a ; expression) g Wwhute : expression L > unttloléses Hu. loop treba. yes beans exputsion? —> Conditional exprsion Lat tests whiter the Loop will continu ov Stops expuuston 3 > moaihes Loop trdex aft each. iden ee . Atoterme 3 ad Loop which execctetfor Qh. ee to a = Jo numb exs aay Ph FE sindluds < stded-t> FE tndude < cont AS Votol man ¢) £ unt 1, 5 Gem. ae O3 L<= Jo; L++) pati) * pli C4, 2), a J He wnducle < tii -AS Veto maine) Also ser tnt to, Minit =5 ; fee C5 £< Limit ; ) EES g PAO bet, 0; fo laut o ram to Cobculah the sum of ica Acstes ape loo tesems. Fe ndlude < Steblo. AS #= Snelude < Gnio- AS vetd mating) t unt £ Se dehie , {b= 1, 423, Count=o; point Chat fans doe PROD PS» Owens : Cd Pia? C20 5 E<=lo0; £+t) 1 fies ft feba; pet Cou f95 eb L = ADR. 2 = sae 4? F Nestecl fox Statement => 9n nated tr’ tou vw tuo Ge Sabrrents, The tout one Jb Called, euuter loop & the Aecond ens. =P The Atoferantl & séatument, ase » ies Aha numbo f times Apes peek 4 maximum Aimit d te Seta pe loop , j* Cexpit expia 5 expi3) i Jt COPS; expan; oppasy t Statement t Sat mort 2 i / AF unclude < Atelio-h> Void main CO) unt 2, num 25 5 Pers C' Number, fattzen \n) fee (4omum 5 i515 &-) t : a ie Gets ou igi) Cea oe peng ("\n'); oe af an te, fee jlbiotyy ion: se tndlude < stehio- A> votd main ©) ant 443 Chau Ch= eS i (to; £25 5 +4) i a Yes fe tr t oy ey per C'\n") 5 Pe | 5 uh oe dee Beak, statment: > Buck steterent allows to tuuninali ty exoculion, a tp b jump a Loa, tht ey back th Conditional test. > when beak dtatmaent th enwountined «nade a loa, the ws immediatly Lvuninabd & forties exccution Us te tansfdoud to next Staturent ey 7 the. leop- aT i* C20; L< leo; ser) ping Cad, ); : (f== /o) % buok, (ne htaeruent ae 16 Check the sete | ect ' rum bens eg Bie die ae gre [than Ao print He Aum att Sndude & dtd Ad te tncluca Ltonto.A> Vata main c) unt 4um= 0, ny ae oe pu C' Enlie the. Lint Wn); beard C"spel", ber); om Cue05 Loe Crt) AUN al 5 Y ( sum >&0) 3 d bruak + poy C the duo is nd; hum); a ee Maio: > Continue Adtoterent 4 usec for the nent Lbecation the Aon tp Jake place, a ma Code &n’ between , > It ts used to tyemindh the cwount tation & Continue with Pe next Hecate of Be to > Continue stobument causes the updation, & then the conditional pat portion ¥ loop t exceuts. 2s eae dG t= a; t+) ye ue ci 254) ie. cet oe Chb Gobi p25 He undude € S$oud A> vod mance ) t We Rout; Lum.= 0, Aum2= 0, Limit, pam; punt 0" Entee Lémit\n') » Cd” timit) i Ckeut-=0 5 pout< umit ; router) c Hum = ( Rew 22d; ge S=Ip) suml += Rout 5 4 continue ; uma = out; Poe C" dun4 = hd \n', Aums ); palit C" Suma = ‘d\n, Suma); 7

You might also like