Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views33 pages

PPS Notes Unit 2

This document contains notes for the Programming for Problem Solving (BCS-101) course for first-year IT, ME, and CE students at Babu Banarasi Das Northern India Institute of Technology. It includes various programming concepts and exercises related to loops, conditional statements, and functions. The notes are intended to aid students in understanding and applying programming principles effectively.

Uploaded by

deveshdk008
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)
18 views33 pages

PPS Notes Unit 2

This document contains notes for the Programming for Problem Solving (BCS-101) course for first-year IT, ME, and CE students at Babu Banarasi Das Northern India Institute of Technology. It includes various programming concepts and exercises related to loops, conditional statements, and functions. The notes are intended to aid students in understanding and applying programming principles effectively.

Uploaded by

deveshdk008
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/ 33

Date : Oct 28,2023/2023-24(Odd Semester)

Branch : IT,ME,CE
Year/Section : First Year - N1&N2
Name of Subject: Programming for Problem
Solving (BCS-101)

UNIT-II
Notes

Name of Faculty: Reetesh Gupta


Name of Department: Information Technology
[[[

BABU BANARASI DAS


NORTHERN INDIA INSTITUTE OF TECHNOLOGY
LUCKNOW
I
1
)-..,1\ \H oL ST ATt :0M t- ~1 T s : - A p~ ~ ~ h a._ ~ •

i ,U-o.t,_ ~ ~~ OJ\L . w,u__~ .k'm ,;t,l .""'- .~ UL . ',4c .h

~
~~ i. L~ e_ 0,~ uJL_J__ . ~ ~ ~ _
M
(3 ~

. vwt ~ J.w,6 . p~ -~ h.e_, ~~ IMD Y e_


~j : ~ AP
t ~~
% ~
+
wkc.,k CL'¼_
p~ t.v-€.- ~ 0M h,el J1,--e_ IM-

".""'J k ~ foy ~ c,,,M ., ~ i


r~ .
J~ a._
~ ~
,Q_XL ~ .

' fl.--,_ ~ XL~ 1 "-


UM~~ .
J hAJ, ~
. ~
.
-
.,l. w,\;J._
f 0
GM_~t;_~ OY t

➔ ¾ """~ ~ u,,,,\w,e,, ~ tN- b c z ~


4 i
~~ ~ e,w__ r~ c ~~~) re- ~ ~ ,{\-z,.k~ 'i ~

r~ - ~ ~ ~~
~
°'-
JI-,_ ~~ ~ 1 Uv l~

/4..t1_~ (JY ~+ _il h~ .


Jh U)V I~ A), J, ,~ d k Mrt11- CL& {$ BWS ·, -
i- ~d Lt . ~ Ifl, ~ ½ I ti.,.U:,~rr,,_- ~ 1 ,!, \,J ,., ~ .

(0,.) i_ f - ~~~
(b) i, 1- ~ "
( C.) AwiJct. - ~ "

:)_ - Loop /2 .tJ L~


(a.) foy ~ ,!~~

( b) w \.-..t t... '' "


(_C) d_~ - w~ ~ " "
. i.,.J e... r
,,(_ x. Y-,c½ ex
LOV\~/ -'..lK.pl'~
X =~ ~ )'l .-C ~ ~u&_ te, }
X. != } ')L ~; vuJ. ~ .:.1.il..
1
hJ d
-y'- z. ~ X.. _:.u, LcA t\AcL-v, J.
?L > '}- --)t_ ~') <l~ ,, ,,
'X- < =- ~ ~ c...r. tu~ ~Yi c,Y u.a.l +v
~ \1 d
?L > -==- } . )(_ t I ~'l..1:,,._.L.tt, I\
1
' I \ 1, ~ .

;+ C{~LLirvv )
,w- 1 ~~t
r 3
~\...(. , ~

M t~ ~
3
D C n Co bol Smuduu

-salaet

AP o md the guals amaona


h thre w.brs

/ mg stlemat find e larnqust nosx


#inudo shdie h
#inlnela io h>
Uid maim C)

int a, b, C, big
pi ("ELa a thre uwweans\n)
Aam ("dAd1.d, La,Lh,sc)
b big)
- b
3
CC bq)
big = C

("Graals o
bi)
The l slalemel

LAP to d th u a u t o hue
ww.burs ww -osesale ant
/x Usinq Q-Mud stalamt find t lngust
ho nwn bons k
inclda sldio h)
iude Ceeh)
yeLd ma

nt ab

p t"ENTER Two NUMBERS ")


ACa ("2dZd",ka,&b);
a>b)
rinlt ( J piatat e hon nuabr is hd", a)

Qws WAP t daln n e whafhon gut uwmas

AS N L ordd.
To Delermuml Even 0x 0dd number a
inude {sldish
include kaian)
Ucud mam ().

Yum
bi (" En yun Numben i \ )
ACam ("d &nn
m num 2
m==0)

il " Jhe ansben aa_oddl n"


qut(

Qus AP edutwnime whahua fhegautm


yLanu As ap ytan ax ol

Qws2 WAP to Callati te abkelt valu


Calcultn of aselnte Valu s
#indudo ieh
include Cheh
UGLd mam

wm

ACa (Zd" ntn)_


w wm < 0)
m=

p The abselud, valu is d", mm


l ~+c ~ Y ~ i )
{~H Lier.,. ~ 2- )

~e, ~ 1- ~
Lk,
~~2- ~
)
J.u-
{
{ ( ~polh¼~ 3)
/2-h,..t, ~ ~ ' /

iX~---
1-, k - t - ~ 1- ~
~
5 : .J ~ £U -tL fhr.e..e_
L oq1C AL oPE R.A To R

f F F
T F
F
F F
1
T T T
~ 4 ~ ~ M h.
Jk Y- xP Y ~ ()v-., A,,~

b J-
f F f
i= 1 T
T f T
T T T
~
O ~ m _ _ -£)_~ ~~ N D T : ~
(0 ~~ N~ L9-N'- k ~
~~ f,~u- fo
orJt99L- °'- ~ ccJ___ L xp Y. £VY1- ~

mAL e, r ~ ~ tB ~ - P-v.u-tt
EJ< pY ~~
~
) (~ ')
) ~ -
) (~
1~~,J__
-j/ J~ cu v_
(')pe__-rcJM-i,
c ~ J, o ~
o , ~~ 0~

6~ Pl\OL,,,,. J. kcD.-W\JL th ,
:

~ f1' 1'U - J '~ ~· Ac~ ~ for= " k l a~ ;J-


/➔ ct 1 - tkvi - ~ . 1/ A ~ M r
-« -

1~y~-~ i 7 f,1,1JY,_;fvl_~ ;-:-0 Y~O½ 3J


1 t- ~ { ~ Y Ll-0 ; (;-A 1 M ~ ( o y ~ v ~ ~ Vl ~ - y ce)

~ 1k v ~ y ~ J_ ~ k ~~ 2-_ 1 , ~~
e,_,-Jt be.- ~ ·-r ,(h¼ fJY'\ 3 _·
~ v~ Y- ~w vv cl,,

~ :x., )r ;
~~
;
;.,x 7t' } ;

h~ ( ,, i, d. , I ~'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½I- ~"Y - .t ._J_, ro µ,,..t,) 1.- ~ ~


30• 0 00 0 ( w-t,_.: cl, ·,._, 0..

\ "' ~ V ,,_,,;, o.l.4_ b .~ ~ · ~ :I,.,;"";} "'- ~ , <°AM""~ ho QL


~~ ;{_'f-~t °' ~ v ~ .
~ l>J-3. : - w-~ .w~toL k k ,, L5t.LJ.t
-<..)(:p"l" (.).¼ ~
~ ~ Uowi 1,- a Y\ ~ ~ c,
i f- \z M (1/'A,
+
I -vst -"~ V ~~~ o..""cl l\. ,{)- q_

, ~i°'1-- V<>v~~
A-n \-h 'M e..~ C G.,.. P"' ~ O'-,'\., Re,~~ A--il rh 'M e.l"\c, ( ;,< f"f~ e--A
R = 2/ 9 o C\. = 2/~
R - 2,0/9 o o.. = 2• 0/ ~
2./9,0 0 C\ = 2/q ,o 0' 2.21-
R --
D · 21.'L
R = 2•o/q, 0 0

4- c~ - S--/1 2- 4 •o
R -- 9j-;._
R -- q, 0/2- 4- °' - fj,- 0/2- -4· 5
R -= 9 /2, D 4- C\ = 4.5

R -- q,o/2.1 0 4- ~ .5
''1~CMh~
-B-
:- 1t ~ ~ ~ e-w V\... o..-6.
L r_
f' "'"uu;l, (.&v\ V ~~ ,

1~~- <:M \i ~ o.. ~ aru/'h.~~ ~><= pY <-,(,¼ ~ t ~ ~


~\~ 1'}' 'Yi-r~ ~ va._k 1- ~ -0<pY~~ I~ ~
Uh.~ lAld' . Jhvv.r ·«!.. ~ j,,.. C,..1Ji J' /4.'. ~ pro~ """3\.1 s

~ ~ V10t \J-.,y\_~ t4__ ~pi~'S u-v\~

'1~ G:u~ir ~ ~ ~ ~ ~ c_;d' ~


clu.\-) ~~ &_~ lj ~ ·, V\ ~ P~ ~ fu Q.l(h..)J bcf ~
VOJw\.~ ~ ~ ~ ta- k UM vuJ--~ .
i A. l\. = Soo , b =- :,-0 ;
~\- 'feb I
/

0 t-o := ( t ~t) CA/ b ;


.J-t..,,., c,_ we. ~ ~ ~ ~ <'.__Mh1iJ-- 1 ~ ~ f-a-
~ 0.. \ ) ~ Q . . . t ~ ~ ~p.L +e o..c± t~ °'- ~~
1~~~ ~ ~µ .
- -

_Cl,)__1,,,,1..L .7 FL cl!rY Cf'~j _Y\b ~u ~J];:___cL~· ~ -fJJ-\1; _ j_y) __fu_ __ ---- ~


---- - fD2Lw~o ---~ -- :·---.- - -_
-------- ------- -----
- _I£__~ _d.:ti_ v~ ·-~ ---- --
- _l ~ ~ m M ~ '"cD , , ·V'(\* ?r----- ~~
--~ - -3
- I , J
Q_,' -V_

f
, ,__.._ ._.,~""'- -h--~

. •
::;:-----------

, \ L ,. .- _o -
- ~-
~ "IL~ - - 14-h--~ -~ --:,--- J~
- ~ ~
- , -t ~L , -
__l-__ c;JL_ ~ ~ - tN d:ovtJ-"'v a V)ot~-~ ~
_it=--~ ~;.J, ,,L_ ~ -~ ~ ~ ~ 6-,_
___kv_~ _O)lL_h _Lnf-_\AU__ he: . ~~--L;l~ \f. _~-t t9; :
- ~ • _ W~ lb-L· cl:o_v~---,~ ~ 1 \0.bW\.LJl .
1

- - - - - - ~\ ~ ~ ~-•-~h_ ) __ _
_ _ _ _ _ _ _c.Va_
_ :l -[i _~----+-
( _..,.
) - - - -- - -
p:P:.Jf ( h \'.0 })xi Vt,'.:! M virt iY\,!;Wv..L_ '~) ____,,
1
.____ _

~ )I---+-';----
- - - --

r
I

..
I

.,___ ____,_-'--'---+'--~ ~ ~ ~_b±x+


~
Y),\ l.-' &t k w~ ~:Y\- s t; c6
.b

- ·c

- :c:;~ M 6 ;f ~ ~h·~ t0, ~ C'l"Q__

- I c;::nJa ,ti, 5 / Y'l!)v::,L t 4


hf Givxl,~h' h\.l. C) Ye? ~t 1

--r-
I'
w ~ I.e. a It>Yl'f' h'Y'- C.,J l✓ Ck Ye VI 'C.cA the
Q Q
1
,U

; ~ -±tz: ~ ti'-- t}.g V d».r;. , _ biv, da v .¼ , ( O!,<lb=.


i C~ , 4 ~ i,Cdl-&,~ /2b:,+ t ~--1,±.d___
I ,v,'\6 ( 2:Yv,1'duiatN s.-- p-n -,J:: ik ?f"u!i.L4_
tN i t ~Q .
il=- ; V)~ <~kL'elb)
-- - -

I I '--

<l tJ... Lt- (_ CC z o • 1: ~ k- t -,,c. . ..=-,_<.


s _ 5__..___
b_oo_)_ _ __ _ _ _ _
~ pn-rli-- C\V\ ~J~ ~ 8 J ·, l•
/

-2&I Lt '('-- bcl > ~ k i-- lb < 5


/

0 '0 C )

1 /J 7 ,-
11
I, pnJL ('• \V\ - C:xn..d~ A-s
\ \ ---
-Uu.. i t ( hd._: > s ~ 1, L (_ .(_ D 1 !~ t & ~ 5 b u c)
O C

,p-nJf- ~\• \ V\ :i ~ ·L:_ _: ~ 5> b ";


1

/ - - _ -- - - - - -

_ ___.__
,1 _ J- - -- ---- -
~ \A.,!, ➔ .J ¼_, b gJii "'tr f½ YQ, ~ M,i ±-o,.b.,.,,. C!,b j10 ~ ..± ,
I (,.:) X? k °'- e,_ vrrr . __,
rn ti·vJ. -- CV N..ll,,.,,___ Cl. 1:-,; ~
T

- - -------H--¥--~~ - ~1( ' L,t.vi. hn11- µ dw, 9f-- l\ hi~ ") ;


- - ~~ f ( , :! d. '/-cl /, cl ,, , ~ Q r 2r- b/ ~ c) ;
' b > c.. ~~ ~- c_ >b &...si. +c_ > a;))- -

- - - - -- - --- - - --- --

- -- - --- - - -

_i_ - - -- -- - - ---- - -- ----- - 11


l)
. YlOt po&'n )J,~ ) :

- --
- - - ------ - --
- - __ --- --- - - - -- -
---- ----
r
- _. - -
-- --- .
r
(9 Wb ➔ I: ~ A i V) C /-& G'"'-"\- wkfu-?k Q. Ve.,.,-,_ ck~Vt,_
11 M °'- v~.J._ 0 b LJE:vL&er~ . Vk · i hpv-C

s~-=;, I:
11
!l
I
d

.I c1w.. ct i
ik a uam Te dalemime whaMer
he Chanadun. Amluud a Cabia
Ltten a mal
Cask ttan a dligut
shaual Apmhel
hanacAs ASC Valus
A 6S 90o
97-122
O
Specio 127
T o chuk yhe ochoaco Lmtd
Rayhoand "
indudastdio a
nclnda lamB h
UoLd man _

char ch i
pnnl "Enlo a haacleu on bylocmd )

bam &ch)_
pat(" n The ckanacln is_in phah lak
Lisz ('th >=94 _hk=122)
ninl"\n Thedhanadn s in hawalas
2lse 'Ch> =48 2 ch&:52)_
int(" n 3he haracer as dit)D
hn "n J thanaclen as Ahualsuowbe)
/ Anohen o nhn4 pXem0usroham *
inudsdho h
inudetne. a>
veu moin)

chan ch,

intf (" Et a dkanactn fem bayboadLn)


Sca %c",ch);_
(Ch>= 48 i ch< =52)
pnnt(" J 2annd chonaln is adigrEl)_

h =î7 2e Chk= [22


b i l Jh laud charatan is in LaanCase )
C(ch>=0 22 chk= 47)|I|(ch>=s8 2 h= 64) I1.
(Ch912 k= 16)1L (Ch>=|23 2 ch=127)).
pnnt "k zmtinad chenaclen a
sheual ybelin
gutch ( ;
LSAP 1n C ta dat wina fh Yoolse
a quadrauc Raualonn
include dis h>
includ lenio h>
#indud math>
Void man)

e t ab, cd T1,Y2,
pid"Eta he valus a,hand c \n)
Atanf ("_Zf Z£ £" á2b a )
d= bxb ax C
if a==0_L b =b==o)
i l " In No Sel
i l i f a == o)
=
- c/b
)
3
else if(d <o
i ( "l n Koots ae magnna )

1 =-b+ t (d))/(2*a
Y2=(=b-sart (A))/(2a)
E ,Y112,
n l "\n Rrot 1=Zfand Reot 2=

trh(
OPERATOR PRECEDENCE TABLE
,Jh.,u__ o ~ o.> t. ~ ~~
8i 1TW1 SE o Pf? RA T oR.S : -
~ ~,;_~c.,-,J-
0~. J~ l\,11,L ~ /JJ

~ ;V\ J..r10\_J_ ~~ wlv u_ of,k


W¾!wL U-~ ~ ~ ~~
, ~ ~ $;~ $V1, ,·vJ .e, ~ ~ ~ ~
114,M-~YuL j ~

w .~ r ~ .r~ ~ .
~
v~~ ~ 1- w .:~ rm-J~ o (YU _ -

1) rv ( hJ --~ ~ o~)
~) M.. ( bi ~- AN D o ~ )

3) ( '""'~vt..-oR o ~ )
b·, r~
4) A ( b;\-·v-.{ ~ ,R,, i<:~ vt - oR Dp,t>L~)
S) <( (
0

b1r~ ~r- y_t D~ )

C) >> ( b',\-L0M., ~( :t Yi d~ o~ J

<D "" at ~ eAeh ~ 1 rw- ~.


~ - 'tut 1t.-= 1q ·Jii ~~ ~~ Oo oi 0011 .

rv-,_ = 11LD jlo o .

~ .~ ~ h \ ~ w- 1 ~ o~ ro f}-.L ~f~ w1
® i , ~ ~1,~~
~ h~ kh
Y ~-
/JJV-
Ah ut1M4.- o ~ . <1
W-- ~ ;A "t .9 1 ,~ Mt -~ o .
b; d . ~~ _,, .± - Oooo ci11L
~ - ti ~ = 1 :,_1

x, ~~ = Oo oo 00 11

9\- ~ 1 ~ 1 :a 1 / 1kL ~~~ ~.cMJt


tw1r ~
® I
};._,\-- ~ ;A. to- i, ~~ J- ~ o .
~- ~\ ~: :: oo oi oj1 1

© I\ ~ ~ W AA () 4 t1-'L ~ W A,\ 1 ~
0

I ~ r&Y-½
~~ :J- ~ o.
o ~ W- AJ ,,u,t,- W 1 I

~- -x.,1'~ = 00 01 01 00

<(< Jk rr 0 ~ ~, tw- vo .k W b.e_ ~f t.d _ ~ f¼_


M_ ~

~ h k_ ~ft .ul _ hw ~~ - ~t
0~ ~~h'V, fk. ~
~ ·~ ~ r¼_ V I ~ { ~ ts k i ~ L . 6V\ Yl i -1M~

~- : - ~ << ?, = 00 11 1 0 oo
®_ >> _}l.L'\;_

o tv~~+.ct,
1,J ~ \h 1-
r(..vt-
v, t:-L"t✓"'-
Y L V-C\.y_
""\. ~ ,v, l},
6} W W½-( ;),-.. t•t-
Y) J·j
/-1t i,,.
),. '.lr,- _h ~~ J~.

1.:ik;;_
~ t--

~- -x.._ >>2- - 0000 o1uo .


,, I N, (J>..L u,,, t ~ - st ~ J-lU'--l_ ' 1

- - - - - -

Jk ~ J I /4 ~ ~ : - J(.J . ~~ .:u.. ~ ~ ~ A,! (\_

Gk. n et- 0--6 rv ch c_o-J.e._ iJ, -te b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 f¼, p~--"- I V 1 C Y ~
1
~ - ~~ t .~ W L ~ ) fN__
J.& . J ~ ~
/.N'> v~ 1- °"f~ OY ~<
w... ~ ~
\.,u ~ t'rl_ IY1 (Y,< JLV .-

/ J ~ k ~ cl:fj-,u.Ji: re .-=il. 12.-- ~ . <M

~~ ~ ~ ~~ -
°'- ~c .,k µ~~
~/l}L, -{~
A.},

* M~ ~~wok~ ~O-NL~~

,ch - Ju _ ~~ .
t A, \~ ~ ~ ~ t,(_~ ~r ~ ' } whtrtP_AA
~ 1
O ~ DY ~~at h j, Yu½<OV4 .
~ °'1 ~
1
 switch case default Statements: The control statement that allows us to make a decision
from the number of choices is called a switch, or more correctly a switch-case-default, since these three
keywords go together to make up the control statement.

Syntax of switch:
switch (expression)
{
case constant1:
statement1;
break;

case constant2:
statement2;
break;
:
:

case constantN:
statementN;
break;

default:
default statement;
}

Where constant1……….constantN are results of expression tested. Only one statement (or set of statements)
out of statement1……….statementN will execute because expression returns only one result at a time. Hence it
requires break after every case statement. If result will not match any case then control will be transferred to
default body and then back in main body.

IMPORTANT NOTE:
1. The limitation of switch statement is that, in switch, we can use only integer, character or any
expression which output either character or integer value.
2. The character case must be given into single quotation mark (i.e. ‘A’).
3. Each constant in each case must be different from all the others.
4. We can execute more than one case into single case as given below:
case ‘A’:
case ‘a’:

Consider the following program:


/*Demonstration of switch-case-default*/
void main( )
{
int i = 2 ;
clrscr();
switch ( i )
{
case 1 :
2
printf ( "I am in case 1 \n" ) ;
break ;
case 2 :
printf ( "I am in case 2 \n" ) ;
break ;
case 3 :
printf ( "I am in case 3 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
getch();
}
The output of this program would be:
I am in case 2
Use of break & default in switch case statements:
 C uses the keyword break to terminate the current execution and send control to outside executing case
body whereas keyword default is used to control if there are unmatched cases of switch expression.
 In switch structure, break & default are optional statements but required to improve the programming
efficiency.
a. If break is not used then control executes all cases until last break statement will meet or
encounters end of switch.
b. If unmatched case is found then control executes default section and then terminate. default can
be used anywhere in control body but only once.

 Tips and Traps (switch statement):

A few useful tips about the usage of switch and a few pitfalls to be avoided:

a) The earlier program that used switch may give you the wrong impression that you can use only
cases arranged in ascending order, 1, 2, 3 and default. You can in fact put the cases in any order
you please. Here is an example of scrambled case order:

void main( )
{
int i = 22 ;
switch ( i )
{
case 121 :
printf ( "I am in case 121 \n" ) ;
break ;
case 7 :
printf ( "I am in case 7 \n" ) ;
break ;
case 22 :
printf ( "I am in case 22 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
3
}
getch();
}
The output of this program would be:
I am in case 22

b) You are also allowed to use char values in case and switch as shown in the following program:
void main( )
{
char c = 'x' ;
clrscr();
switch ( c )
{
case 'v' :
printf ( "I am in case v \n" ) ;
break ;
case 'a' :
printf ( "I am in case a \n" ) ;
break ;
case 'x' :
printf ( "I am in case x \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
getch();
}
The output of this program would be: I am in case x
In fact here when we used ‘v’, ‘a’, ‘x’ they are actually replaced by the ASCII values (118, 97, 120) of these
character constants.

c) At times we may want to execute a common set of statements for multiple cases. How this can be
done is shown in the program example of “Mathematical Calculator”.
d) Even if there are multiple statements to be executed in each case there is no need to enclose them
within a pair of braces (unlike if, and else).
e) Every statement in a switch must belong to some case or the other. If a statement doesn’t belong to
any case the compiler won’t report an error. However, the statement would never get executed.
For example, in the following program the printf( ) never goes to work.
void main( )
{
int i, j ;
clrscr();
printf ( "Enter value of i" ) ;
scanf ( "%d”, &i ) ;
switch ( i )
{
printf ( "Hello" ) ;
case 1 :
j = 10 ;
break ;
case 2 :
4
j = 20 ;
break ;
}
getch();
}
f) If we have no default case, then the program simply falls through the entire switch and continues
with the next instruction (if any,) that follows the closing brace of switch.
g) Is switch a replacement for if? Yes and no. Yes, because it offers a better way of writing programs
as compared to if, and no because in certain situations we are left with no choice but to use if. The
disadvantage of switch is that one cannot have a case in a switch which looks like: case i <= 20 :
All that we can have after the case is an int constant or a char constant or an expression that
evaluates to one of these constants. Even a float is not allowed. The advantage of switch over if is
that it leads to a more structured program and the level of indentation is manageable, more so if
there are multiple statements within each case of a switch.
h) We can check the value of any expression in a switch. Thus the following switch statements are
legal.
switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )

Expressions can also be used in cases provided they are constant expressions (Cases can never
have variable expressions). Thus case 3+7: is correct, however, case a+b: or case a+3: is incorrect.
i) A float expression cannot be tested using a switch.
j) Multiple cases cannot use same expressions. Thus the following switch is illegal:
switch ( a )
{
case 3 :
...
case 1 + 2 :
...
}
Example: WAP in C for mathematical calculator for addition & subtraction operations.
Answer:
/*Program for Mathematical Calculator*/
void main()
{
int x=50,y=20,z;
char ch;
clrscr();
printf ("\n ENTER A FOR ADDITION");
printf ("\n ENTER S FOR SUBTRACTION");
printf ("\n ENTER YOUR CHOICE\t");
scanf ("%c",&ch);
switch (ch) //switch character value given through keyboard
{
case 'A':
case ‘a’:
case ‘+’:
z=x+y;
printf("\nAddition of x and y is = %d",z);
break;
5
case 'S':
case ‘s’:
case ‘-’:
z=x-y;
printf("\nSubtraction of x and y is =%d",z);
break;
default:
printf("\n ENTER CORRECT CHOICE");
break;
}
getch();
}

The output will be as shown below:


ENTER A FOR ADITION
ENTER S FOR SUBTRACTION
ENTER YOUR CHOICE A
Addition of x and y is =70 //Here value of ch is A hence case ‘A’ will executed.

Advantages & Disadvantages of switch over if:


Is switch a replacement for if? Yes and no. Yes, because it offers a better way of writing programs as
compared to if, and no because in certain situations we are left with no choice but to use if.

The disadvantage of switch is that one cannot have a case in a switch which looks like:
case i <= 20 :
All that we can have after the case is an int constant or a char constant or an expression that evaluates
to one of these constants. Even a float is not allowed.
The advantage of switch over if is that it leads to a more structured programming and the level of
indentation is manageable, more so if there are multiple statements within each case of a switch.
, c."' t,~ i-, D.-u__ Y ~ J? p o\p I~ _,._ C-1- -t ~a. ___) --:,
1."' c< ~ : Lo t Y "-°">'- 1-k v~ 1 """- ,.,i,1'_,.,, vMv1.P1Uc 6;j l

DL ( ~ ~ : ~4(.Q.., ., ,, 'I ,. ,,
h..<1 J

-t +?L o9L.)l-t i- ( -x_::::'X- t i OY '!l. t -= 1) .


~ :
~- d' ~ ~ - - ( ~ ~ d- 1 OY d -~1 ) .

~&\ \, 11--t._ ~'<" ""- ( H'l1 .. &'l ?t+ t) ~ ~ ~ cok.,,. ~ ~


Lx{_u .J 4 ~~ . BLAi -~ ~ cl ef ~\
IY\ ~ ~ ~

~ ~ ~ ~ \ V\ ~ ¥ ~ 1fN ~ tN_ Y'r ~ k~ 1~


~~o ~ .
c~~t: 1 '. ~
~
- ')L ==- 1 '.L
+ t '"X.. ~ p~1tt_ ~ ~v~ ?L = I~
~ ?Lt + Oy
1,
,, ?t =i 1 .
~ - - ,L I \ II
?L- - 0'(

CA-Sf-'.:l.: -4 ')L::: : j 2- .

~ d ;:_ bl.++ ;
~ p~ u__ ~ ¥ ,,U,,(..Jk ~ ~ v ~ d' ~ 12- ~ ')(_ ~ 1 '5 L t_ , t i x_
~ J- cz__ ~ ~ v"-k
J- p ~~ ~ v~ ~ Y) ~

CAS f.3: ~ 'X.. ::. 12-


~ 'j. -= +-t )l_ ',

~ r~UL ~ y~ ~ ~ \J~ tr& d Q._ '"}t_ be,,~ ,(,C, Js i. t:


ct ts GV¼ct ~ a_lA(, ~
p ~ V~ 7L ,tJ, i Y) CY ,.--Guv'_J___ I3

+e- ~ .

~~ ➔ wW w~ k ~ ~~ 1-,· ~J_~ d p~~J)."-"' ?


WJ ~ ()

i
1vl 'n_.:: 4 I
ll
(J I
"\ . '
() J

~ = - -')C ~

a=- x. --;
r~v1f- c,\~ 1 J. ·;. cl '/, i,,, n , ~ , d ) 1
/

)
oj p :

You might also like