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

COMP 002 - Reviewer - Using Conditional and Looping Constructs

Reviewer

Uploaded by

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

COMP 002 - Reviewer - Using Conditional and Looping Constructs

Reviewer

Uploaded by

jisoo0186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

‭COMP 002 Reviewer: Using Conditional and Looping Constructs‬ ‭COMP 002‬

‭IBITS Year 17 | A.Y. 2023-2024 First Semester Midterms‬

‭CONTROL STRUCTURES‬
‭●‬ i‭f-else statement‬
‭●‬ ‭ ontrol ‬ ‭s tructures ‬ ‭gui de‬ ‭the‬ ‭flow‬ ‭o f‬‭p rogra ms ,‬‭e nha nci ng‬
C ‭○‬ ‭The‬ ‭"i f-el s e‬ ‭s ta tement"‬ ‭e xtends ‬ ‭the‬ ‭ca pa bi l i ties ‬ ‭o f‬
‭cl a ri ty‬ ‭a nd‬ ‭u nders ta ndi ng.‬ ‭Util i zi ng‬ ‭s el f-conta i ned‬ ‭the‬ ‭"i f‬ ‭s ta tement"‬ ‭b y‬ ‭p rovi di ng‬ ‭a n‬ ‭a l terna tive‬ ‭b l ock‬
‭m odul es ‬ ‭k nown‬ ‭a s ‬ ‭l ogi c‬ ‭o r‬ ‭control ‬ ‭s tructures ,‬ ‭thes e‬ ‭o f‬ ‭code‬ ‭to‬ ‭e xecute‬ ‭w hen‬ ‭the‬ ‭condi tion‬ ‭i s ‬ ‭fa l s e.‬‭Thi s ‬
‭e l ements ‬ ‭a na l yze‬ ‭condi tions ‬ ‭to‬‭d etermi ne‬‭the‬‭p rogra m's ‬ ‭cons truct‬ ‭e nha nces ‬ ‭code‬ ‭flexi bi l i ty,‬ ‭a l l owi ng‬
‭d i rection.‬ ‭The‬ ‭three‬ ‭p ri ma ry‬ ‭types ‬ ‭o f‬ ‭l ogi c‬ ‭a re‬ ‭S equence‬ ‭d evel opers ‬ ‭to‬ ‭h a ndl e‬ ‭m ul tipl e‬ ‭s cena ri os ‬ ‭w i th‬
‭( s equentia l ‬ ‭flow),‬ ‭S el ection‬ ‭( condi tiona l ‬ ‭flow),‬ ‭a nd‬ ‭conci s e‬ ‭a nd‬ ‭rea da bl e‬ ‭s ynta x.‬ ‭The‬ ‭b a l a nce‬ ‭b etween‬
‭I tera tion (repetitive flow).‬ ‭s i mpl i ci ty‬ ‭a nd‬ ‭vers a til i ty‬ ‭m a kes ‬ ‭the‬ ‭"i f-el s e‬
‭s ta tement"‬ ‭a ‬ ‭corners tone‬ ‭i n‬ ‭m a ny‬ ‭p rogra mmi ng‬
‭●‬ F‭ l ow of control ‬ ‭l a ngua ges .‬
‭○‬ ‭Typi ca l l y,‬ ‭s ta tements ‬ ‭i n‬ ‭a ‬ ‭p rogra m‬ ‭e xecute‬
‭s equentia l l y,‬ ‭fol l owi ng‬ ‭o ne‬ ‭a nother.‬‭Thi s ‬‭s equentia l ‬ ‭Example:‬
‭flow‬ ‭o f‬ ‭control ‬ ‭ca n‬ ‭b e‬ ‭m odi fied‬ ‭to‬ ‭i ntroduce‬ ‭choi ces ‬
i‭ f (expres s i on)‬ i‭ f (a > b)‬
‭o r‬ ‭repetitions .‬ ‭Us i ng‬ ‭i f,‬ ‭i f-el s e,‬ ‭a nd‬ ‭s wi tch‬ ‭{‬
‭{‬
‭s ta tements ‬ ‭a l l ows ‬ ‭for‬ ‭s el ecting‬ ‭a l terna tive‬ ‭a ctions ,‬ ‭p ri ntf(“a i s bi gger tha n‬
‭/ / Do thi s ‬
‭w hi l e‬ ‭w hi l e,‬ ‭for,‬ ‭a nd‬ ‭d o‬ ‭s ta tements ‬ ‭fa ci l i ta te‬ ‭b ”);‬
‭}‬
‭i ntera ctive a ctions .‬ ‭}‬
‭e l s e‬ ‭e l s e‬
‭{‬ ‭{‬
‭SINGLE SELECTION CONSTRUCT‬ ‭/ / Do thi s i ns tea d‬ ‭p ri ntf(“b i s bi gger tha n‬
‭}‬ ‭a ”);‬
‭●‬ ‭A‬ ‭s i ngl e‬ ‭s el ection‬ ‭cons truct‬ ‭a l l ows ‬‭a ‬‭p rogra m‬‭to‬‭m a ke‬‭a ‬ ‭}‬
‭ eci s i on‬ ‭b a s ed‬ ‭o n‬ ‭a ‬ ‭s i ngl e‬ ‭condi tion.‬ ‭I f‬ ‭the‬ ‭condi tion‬‭i s ‬
d
‭true,‬ ‭a ‬ ‭s peci fic‬ ‭b l ock‬ ‭o f‬ ‭code‬ ‭i s ‬‭e xecuted;‬‭o therwi s e,‬‭i t‬‭i s ‬
‭s ki pped.‬ ‭Thi s ‬ ‭cons truct‬ ‭i s ‬ ‭repres ented‬ ‭b y‬ ‭the‬ ‭“i f‬ ‭MULTI-WAY STATEMENT‬
‭s ta tement” i n ma ny progra mmi ng l a ngua ges .‬
‭●‬ ‭ ‬ ‭m ul ti-wa y‬ ‭s ta tement,‬ ‭o ften‬ ‭i mpl emented‬ ‭u s i ng‬ ‭the‬
A
‭●‬ i‭f statement‬ ‭“s wi tch‬ ‭s ta tement”‬ ‭i n‬ ‭s ome‬ ‭p rogra mmi ng‬ ‭l a ngua ges ,‬
‭○‬ ‭The‬ ‭"i f‬ ‭s ta tement"‬ ‭i s ‬ ‭the‬ ‭funda menta l ‬ ‭b ui l di ng‬ ‭p rovi des ‬ ‭a ‬ ‭s tructured‬ ‭w a y‬ ‭to‬ ‭h a ndl e‬ ‭m ul tipl e‬ ‭condi tions ‬
‭b l ock‬ ‭o f‬ ‭condi tiona l ‬ ‭l ogi c.‬ ‭I t‬ ‭e na bl es ‬ ‭the‬ ‭e xecution‬ ‭b a s ed‬ ‭o n‬ ‭d i s crete‬ ‭va l ues .‬ ‭Ea ch‬ ‭va l ue‬ ‭i s ‬ ‭a s s oci a ted‬ ‭w i th‬
‭o f‬ ‭a ‬ ‭b l ock‬ ‭o f‬ ‭code‬ ‭b a s ed‬ ‭o n‬ ‭the‬ ‭e va l ua tion‬ ‭o f‬ ‭a ‬ ‭a s peci fic bl ock of code.‬
‭s peci fied‬ ‭condi tion.‬ ‭The‬ ‭cl a ri ty‬ ‭a nd‬ ‭s i mpl i ci ty‬ ‭o f‬ ‭the‬
‭"i f‬ ‭s ta tement"‬ ‭m a ke‬ ‭i t‬ ‭i ndi s pens a bl e‬ ‭for‬ ‭h a ndl i ng‬ ‭●‬ s‭ witch statement‬
‭b a s i c deci s i on-ma ki ng s cena ri os .‬ ‭○‬ ‭The‬ ‭"s wi tch‬ ‭s ta tement"‬ ‭i s ‬ ‭a ‬ ‭p owerful ‬ ‭tool ‬ ‭for‬
‭h a ndl i ng‬ ‭m ul tipl e‬ ‭condi tions ‬ ‭e ffici entl y.‬ ‭Unl i ke‬ ‭a ‬
‭Example:‬ ‭s eri es ‬ ‭o f‬ ‭"i f-el s e"‬ ‭s ta tements ,‬ ‭the‬ ‭"s wi tch"‬
‭o rga ni zes ‬ ‭code‬ ‭i n‬ ‭a ‬ ‭m ore‬ ‭s tructured‬ ‭m a nner,‬
i‭ f (expres s i on)‬ i‭ f (s core >= 90)‬ ‭i mprovi ng‬ ‭rea da bi l i ty.‬ ‭H owever,‬ ‭i ts ‬ ‭u s e‬ ‭ca s es ‬ ‭a re‬
‭{‬ ‭{‬ ‭l i mi ted‬ ‭to‬ ‭s cena ri os ‬ ‭w here‬ ‭the‬ ‭condi tion‬ ‭i s ‬ ‭b a s ed‬
‭/ / Do thi s ‬ ‭p ri ntf(“Congra tul a tions ”);‬ ‭o n‬ ‭d i s crete‬ ‭va l ues .‬ ‭The‬ ‭"s wi tch‬ ‭s ta tement"‬ ‭s hi nes ‬
‭}‬ ‭}‬ ‭w hen‬ ‭m a na gi ng‬ ‭m ul tipl e‬ ‭b ra nches ,‬ ‭p rovi di ng‬ ‭a n‬
‭e l ega nt s ol ution for certa i n types of deci s i on trees .‬
‭MULTIPLE SELECTION CONSTRUCT‬
‭Example:‬

‭●‬ ‭ ‬ ‭m ul tipl e‬ ‭s el ection‬ ‭cons truct‬ ‭e na bl es ‬ ‭a ‬ ‭p rogra m‬ ‭to‬


A s‭ wi tch (expres s i on)‬ i‭ nt s core = 5‬
‭choos e‬ ‭b etween‬ ‭two‬ ‭a l terna tive‬ ‭p a ths ‬ ‭b a s ed‬ ‭o n‬ ‭a ‬ ‭{‬ ‭s wi tch (s core)‬
‭condi tion.‬ ‭I f‬ ‭the‬ ‭condi tion‬ ‭i s ‬ ‭true,‬ ‭o ne‬ ‭b l ock‬ ‭o f‬ ‭code‬ ‭i s ‬ ‭ca s e cons ta nt expres s i on:‬ ‭{‬
‭ca s e 5:‬
‭e xecuted;‬ ‭i f‬ ‭fa l s e,‬ ‭a nother‬ ‭b l ock‬ ‭i s ‬ ‭e xecuted.‬ ‭Thi s ‬ ‭s ta tement;‬
‭p ri ntf(“A”);‬
‭cons truct i s repres ented by the “i f-el s e s ta tement”.‬ ‭b rea k;‬ ‭b rea k;‬

‭C OMP 002: Computer Programming‬ ‭Using Conditional and Looping Constructs‬ ‭1 ‬


‭COMP 002 Reviewer: Using Conditional and Looping Constructs‬ ‭COMP 002‬

‭IBITS Year 17 | A.Y. 2023-2024 First Semester Midterms‬

‭ca s e cons ta nt expres s i on:‬ ‭ca s e 4:‬ ‭e l s e {‬ ‭i f (a ttenda nce >= 80) {‬
‭s ta tement;‬ ‭p ri ntf(“B”);‬ ‭/ /‬ ‭D o‬ ‭thi s ‬ ‭p ri ntf(“Pa s s wi th G Attenda nce”);‬
‭b rea k;‬ ‭b rea k;‬ ‭i ns tea d‬ ‭}‬
‭ca s e 3:‬
‭ca s e cons ta nt expres s i on:‬ ‭}‬ ‭e l s e {‬
‭p ri ntf(“C”);‬
‭s ta tement;‬ ‭b rea k;‬ ‭}‬ ‭p ri ntf(“Pa s s ‬ ‭w i th‬ ‭B ‬
‭b rea k;‬ ‭ca s e 2:‬ ‭e l s e {‬ ‭Attenda nce”);‬
‭d efa ul t:‬ ‭p ri ntf(“D”);‬ ‭/ / Do thi s i ns tea d‬ ‭}‬
‭s ta tement;‬ ‭b rea k;‬ ‭}‬ ‭}‬
‭}‬ ‭ca s e 1:‬ ‭e l s e {‬
‭p ri ntf(“F”);‬ ‭p ri ntf(“Fa i l ”);‬
‭b rea k;‬
‭}‬
‭d efa ul t:‬
‭p ri ntf(“Out of Ra nge”);‬
‭}‬
‭LOOPS‬

‭UNCONDITIONAL TRANSFER STATEMENTS‬ ‭●‬ I‭ n C progra mmi ng, l oops a re us ed to repea tedl y execute‬
‭a bl ock of code. There a re three ma i n types of l oops :‬
‭●‬ ‭ rea k Sta tement‬
B ‭w hi l e, do-whi l e, a nd for. Ea ch l oop ha s i ts own s ynta x‬
‭○‬ ‭The‬ ‭"brea k"‬ ‭s ta tement‬ ‭i s ‬ ‭a ‬ ‭control ‬ ‭flow‬ ‭s ta tement‬ ‭a nd us e ca s es .‬
‭tha t‬ ‭i s ‬ ‭u s ed‬ ‭to‬ ‭e xi t‬ ‭p rema turel y‬ ‭from‬ ‭a ‬ ‭l oop‬ ‭o r‬
‭s wi tch‬ ‭s ta tement.‬ ‭When‬ ‭e ncountered,‬ ‭the‬ ‭"brea k"‬ ‭●‬ ‭ hile loop‬
w
‭s ta tement‬ ‭i mmedi a tel y‬ ‭termi na tes ‬ ‭the‬ ‭i nnermos t‬ ‭○‬ ‭The‬ ‭w hi l e‬ ‭l oop‬ ‭checks ‬ ‭the‬‭condi tion‬‭b efore‬‭e nteri ng‬
‭e ncl os i ng‬ ‭l oop‬ ‭o r‬ ‭s wi tch‬ ‭s ta tement,‬ ‭tra ns ferri ng‬ ‭the‬ ‭l oop.‬ ‭I f‬ ‭the‬‭condi tion‬‭i s ‬‭true,‬‭the‬‭code‬‭i ns i de‬‭the‬
‭control ‬ ‭to‬ ‭the‬ ‭s ta tement‬ ‭fol l owi ng‬ ‭the‬ ‭termi na ted‬ ‭l oop‬ ‭i s ‬ ‭e xecuted,‬ ‭a nd‬ ‭the‬ ‭l oop‬ ‭continues ‬ ‭u ntil ‬ ‭the‬
‭l oop or s wi tch.‬ ‭condi tion becomes fa l s e.‬

‭●‬ ‭ ontinue Sta tement‬


C ‭Example:‬
‭○‬ ‭The‬ ‭"continue"‬ ‭s ta tement‬ ‭i s ‬ ‭a nother‬ ‭control ‬ ‭flow‬
‭s ta tement‬ ‭u s ed‬ ‭i n‬ ‭l oops ‬ ‭to‬ ‭s ki p‬‭the‬‭res t‬‭o f‬‭the‬‭code‬ ‭w hi l e (expres s i on) {‬ i‭ nt i = 1‬
‭i ns i de‬ ‭the‬ ‭l oop‬ ‭for‬ ‭the‬‭current‬‭i tera tion‬‭a nd‬‭m ove‬‭to‬ ‭/ / Do s omethi ng‬ ‭w hi l e (i <= 5) {‬
‭the‬ ‭n ext‬ ‭i tera tion.‬ ‭When‬‭the‬‭"continue"‬‭s ta tement‬‭i s ‬ ‭}‬ ‭p ri ntf(“Count %d”, i );‬
‭e ncountered,‬ ‭the‬ ‭rema i ni ng‬ ‭code‬ ‭i n‬ ‭the‬‭l oop‬‭for‬‭the‬ ‭i ++;‬
‭current‬ ‭i tera tion‬ ‭i s ‬ ‭s ki pped,‬ ‭a nd‬ ‭the‬ ‭l oop‬ ‭p roceeds ‬ ‭}‬
‭w i th the next i tera tion.‬
‭●‬ ‭ o-while loop‬
d
‭NESTED IF-STATEMENT‬ ‭○‬ ‭The‬ ‭d o-whi l e‬‭l oop‬‭i s ‬‭s i mi l a r‬‭to‬‭the‬‭w hi l e‬‭l oop,‬‭b ut‬‭i t‬
‭checks ‬ ‭the‬ ‭condi tion‬ ‭a fter‬ ‭e xecuting‬ ‭the‬ ‭code‬ ‭i ns i de‬
‭●‬ T‭ he‬ ‭"nes ted‬ ‭i f-el s e‬ ‭s ta tement"‬ ‭i ntroduces ‬‭a ‬‭h i era rchi ca l ‬ ‭the‬ ‭l oop.‬ ‭Thi s ‬ ‭e ns ures ‬ ‭tha t‬‭the‬‭code‬‭i ns i de‬‭the‬‭l oop‬
‭s tructure‬ ‭to‬ ‭condi tiona l ‬ ‭l ogi c.‬ ‭B y‬ ‭n es ting‬ ‭o ne‬ ‭o r‬ ‭m ore‬ ‭i s executed a t l ea s t once.‬
‭"i f-el s e"‬ ‭cons tructs ‬ ‭w i thi n‬ ‭e a ch‬ ‭o ther,‬ ‭d evel opers ‬ ‭ca n‬
‭a ddres s ‬ ‭compl ex‬ ‭d eci s i on-ma ki ng‬ ‭s cena ri os .‬ ‭Whi l e‬ ‭the‬ ‭Example:‬
‭n es ted‬ ‭a pproa ch‬ ‭e nha nces ‬ ‭flexi bi l i ty,‬ ‭i t‬ ‭requi res ‬ ‭ca reful ‬
‭cons i dera tion‬‭to‬‭m a i nta i n‬‭code‬‭rea da bi l i ty‬‭a nd‬‭a voi d‬‭the‬ ‭d o {‬ i‭ nt i = 1;‬
‭p i tfa l l s of exces s i ve i ndenta tion.‬ ‭/ / Do s omethi ng‬ ‭d o {‬
‭} whi l e (expres s i on)‬ ‭p ri ntf(“Count: %d”, i );‬
‭Example:‬ ‭i ++;‬
‭} whi l e (i <= 5);‬
‭i f (expres s i on) {‬ i‭ nt gra de = 75‬
‭i f (expres s i on) {‬ ‭i nt a ttenda nce = 80;‬
‭/ / Do thi s ‬
‭}‬ ‭i f (gra de >= 80) {‬

‭C OMP 002: Computer Programming‬ ‭Using Conditional and Looping Constructs‬ ‭2 ‬


‭COMP 002 Reviewer: Using Conditional and Looping Constructs‬ ‭COMP 002‬

‭IBITS Year 17 | A.Y. 2023-2024 First Semester Midterms‬

‭●‬ f‭ or loop‬
‭○‬ ‭The‬ ‭for‬ ‭l oop‬ ‭i s ‬ ‭u s ed‬‭w hen‬‭you‬‭k now‬‭i n‬‭a dva nce‬‭h ow‬
‭m a ny‬ ‭times ‬ ‭the‬ ‭l oop‬ ‭n eeds ‬ ‭to‬ ‭b e‬ ‭e xecuted.‬ ‭I t‬
‭cons i s ts ‬ ‭o f‬ ‭three‬ ‭p a rts :‬ ‭i ni tia l i za tion,‬ ‭condi tion,‬ ‭a nd‬
‭u pda te.‬ ‭The‬ ‭i ni tia l i za tion‬ ‭i s ‬ ‭e xecuted‬ ‭o nce‬ ‭a t‬ ‭the‬
‭b egi nni ng,‬ ‭the‬ ‭condi tion‬ ‭i s ‬ ‭checked‬ ‭b efore‬ ‭e a ch‬
‭i tera tion,‬ ‭a nd‬ ‭the‬ ‭u pda te‬ ‭i s ‬ ‭p erformed‬ ‭a fter‬ ‭e a ch‬
‭i tera tion.‬

‭Example:‬

f‭ or (i ni tia l i za tion; condi tion;‬ ‭for (i nt i = 0; i < 5; i ++) {‬


‭u pda te) {‬ ‭p ri ntf(“Count‬ ‭% d”,‬
‭/ / Do s omethi ng‬ ‭i );‬
‭}‬ ‭}‬

‭C OMP 002: Computer Programming‬ ‭Using Conditional and Looping Constructs‬ ‭3 ‬

You might also like