COMP 002 - Reviewer - Using Conditional and Looping Constructs
COMP 002 - Reviewer - Using Conditional and Looping Constructs
CONTROL STRUCTURES
● if-else statement
● ontrol s tructures gui de the flow o fp 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 tod etermi nethep 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 mtom a kea }
eci s i on b a s ed o n a s i ngl e condi tion. I f the condi tioni s
d
true, a s peci fic b l ock o f code i s e xecuted;o therwi s e,i ti 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
● if 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:
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 thecondi tionb eforee 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 thecondi tioni s true,thecodei ns i dethe
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.
● f or loop
○ The for l oop i s u s edw henyouk nowi na dva nceh 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: