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

05 Decision Making

The document provides an overview of various decision making and control statements in C programming such as if, if-else, nested if-else, switch case, conditional operator, and goto statements. It explains the syntax and usage of each statement with examples. Key topics covered include using if statements to execute code conditionally, if-else statements to choose between multiple code blocks, nested if statements for multiple levels of conditions, switch case for selecting between multiple blocks based on a variable value, and conditional operators for shorthand conditional assignments.

Uploaded by

Vipul Desai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

05 Decision Making

The document provides an overview of various decision making and control statements in C programming such as if, if-else, nested if-else, switch case, conditional operator, and goto statements. It explains the syntax and usage of each statement with examples. Key topics covered include using if statements to execute code conditionally, if-else statements to choose between multiple code blocks, nested if statements for multiple levels of conditions, switch case for selecting between multiple blocks based on a variable value, and conditional operators for shorthand conditional assignments.

Uploaded by

Vipul Desai
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

C Programming, Decision Making

C Programming
Decision Making
Topics Covered: 1. Introduction to Decision Making OR Control Statement 2. if statement . if...else statement !. "ested if...else statement #. if...else if statement $. s%itc& statement '. Conditional o(erator OR )* o(erator +. goto statement

Introduction to Decision Making OR Control Statement: ,e kno% t&at t&e e-ecution of (rogram is done instruction ./ instruction. 0fter e-ecuting one instruction /our com(uter %ill e-ecute anot&er instruction. 1&is flo% of t&e instruction e-ecution is kno%n as se2uential e-ecution. 3ut some time /ou need to c&ange t&e flo% of t&e instruction .ased on t&e certain condition. 1o c&ange t&e flo% .ased on t&e condition %e are using t&e Decision making statement or control statement. C su((orts follo%ing control statement or decision4making statement* 1. i statement 2. s!itc" statement . Conditional Operator statement !. goto statement

i Statement: It is most (o%erful and %idel/ used statement used for decision making. 1&e s/nta- of t&e if statement is* if(test condition) { Block Statement-1 } Statement-2 ...................................................... 5irst /our com(uter %ill c&eck t&e test condition. If t&e condition is true t&en com(uter (ut non46ero 7alue and e-ecute t&e 3lock Statement41, after e-ecuting .lock statement it %ill e-ecute Statement42. If condition is false t&en com(uter (ut 6ero 7alue and %ill not e-ecute 3lock Statement41 .ut directl/ e-ecute Statement42.

test conditio n 5alse Statement42

1rue

3lock Statement41

1&e e-am(le of t&e if statement is* if(marks < 40) { printf(Yo are fail!n")# } printf($otal %arks & 'd!n"(marks)# 3/ %%%.s&are.ca.com S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

C Programming, Decision Making 8ere first t&e if statement is e-ecuted. 9our com(uter %ill c&eck %&et&er marks is less t&an !: or not and if it is t&at t&en it %ill (rint message ;9ou are fail< and t&en (rint t&e total marks ot&er%ise it directl/ (rint t&e total marks.

i ###else Statement: It is e-tension of t&e if statement. Its format is* if(test condition) { Block Statement-1 } else { Block Statement-2 } Statement-) ...................................................... 5irst /our com(uter %ill c&eck t&e test condition. If t&e condition is true t&en com(uter e-ecute t&e 3lock Statement41. If condition is false t&en com(uter %ill e-ecute 3lock Statement42. 0fter e-ecuting eit&er 3lock Statement41 or 3lock Statement42 it %ill e-ecute t&e Statement4 .

3lock Statement41

1rue

test conditio n

5alse

3lock Statement42

Statement4 1&e e-am(le of t&e if...else statement is* if(marks < 40) { printf(Yo are fail!n")# } else { printf(Yo are pass!n")# } printf($otal %arks & 'd!n"(marks)# 8ere first t&e if statement is e-ecuted. 9our com(uter %ill c&eck %&et&er marks is less t&an !: or not and if it is t&at t&en it %ill (rint message ;9ou are fail< ot&er%ise it %ill (rint t&e message ;9ou are (ass<. 0fter (rint (ass or fail it %ill (rint total marks.

$ested i ###else Statement: ,&en %e are using if...else statement %it& in one if...else statement t&en it %ill make t&e nested if...else statement. 1&e format of nested if...else statement is* if(test condition-1) { if(test condition-2) { Block Statement-1 } else { Block Statement-2 } } else { Block Statement-) } Statement-4 ...................................................... S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

3/ %%%.s&are.ca.com

C Programming, Decision Making 8ere first /our com(uter %ill c&eck t&e test condition41. If t&e condition is true t&en com(uter c&eck anot&er test condition42. If test condition42 is true t&en it %ill e-ecute 3lock Statement41 ot&er%ise e-ecute t&e 3lock Statement42. If test4condition1 is false t&en it %ill e-ecute 3lock Statement4 . 0fter com(leting t&e nested if...else statement e-ecution com(uter %ill e-ecute Statemetn4 !.

1rue

test condition 41

5alse

3lock Statement4

test condition 42

5alse

3lock Statement42

1rue 3lock Statement41

Statement4!

1&e e-am(le of t&e nested if...else is* if(no1*&no2) { if(no1*&no)) { printf(+ m,er1 is %a-im } else { printf(+ m,er) is %a-im } } else { if(no2*&no)) { printf(+ m,er2 is %a-im } else { printf(+ m,er) is %a-im } }

m!n")# m!n")#

m!n")# m!n")#

8ere first t&e if statement is to c&eck %&et&er no1=>no2. If condition is true t&en it %ill c&eck %&et&er no1=>no . If second condition is true t&en it %ill (rint message ;"um.er1 is Ma-imum< ot&er%ise (rint t&e message ;"um.er is Ma-imum<. If condition no1=>no2 is false t&en com(uter %ill c&eck t&e condition no2=>no and it true t&en it %ill (rint message ;"um.er2 is Ma-imum< ot&er%ise (rint t&e message ;"um.er is Ma-imum.<

i ###else i Statement: It is used to make multi(at& decisions. Multi(at& decision in7ol7es c&ain of if %it& else if. 1&e format of t&is statement is* if(test condition-1) { Block Statement-1 } 3/ %%%.s&are.ca.com S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

C Programming, Decision Making else if(test condition-2) { Block Statement-2 } else if(test condition-)) { Block Statement-) } else { Block Statement-4 } Statement-. ...................................................... 8ere first /our com(uter %ill c&eck t&e test condition41. If t&e condition is true t&en com(uter %ill e-ecute 3lock Statement41 ot&er%ise it %ill c&eck t&e test condition42. If t&e condition is true t&en com(uter %ill e-ecute 3lock Statement42 ot&er%ise it %ill c&eck t&e test condition4 . If condition is true t&en it %ill e-ecute 3lock Statement4 ot&er%ise it %ill e-ecute 3lock Statement4!.

1rue

test condition 41

5alse

1rue
3lock Statement41

test condition 42

5alse

5alse 1rue
3lock Statement42 test condition 4

3lock Statement4

3lock Statement4!

Statement4#

1&e e-am(le of t&e t&is statement is* if(percenta/e *&00) { printf(1istinction!n")# } else if(percenta/e *& 20) { printf(3irst 4lass!n")# } else if(percenta/e *& .0) { printf(Second 4lass!n")# } else if(percenta/e *&40) { printf(5ass 4lass!n")# } else { printf(3ail!n")# } 3/ %%%.s&are.ca.com S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

C Programming, Decision Making In a.o7e e-am(le first com(uter c&eck %&et&er (ercentage=>': or not. If it is true t&en it %ill (rint message ;Distinction< ot&er%ise c&eck anot&er condition (ercentage=>$:. If it is true t&en (rint message ;5irst Class< and in t&e same at last it %ill (rint t&e message ;5ail< if a.o7e all condition is fail.

s!itc" Statement: ,&en t&ere is multi(le condition in t&e if statement it is .ecome difficult to read and understand (rogram. In t&is condition %e can use s%itc& statement, %&ic& allo%s c&ecking t&e 7alue of 7aria.le against t&e multi(le case 7alue. 1&e format of t&e t&is statement is* s6itc7(8aria,lename) { case 81 9 case 82 case 8) defa lt } Statement-. 8ere if t&e 7alue of 7aria.le> > 71 t&en 3lock Statement41 %ill .e e-ecuted. If 7aria.le> > 72 t&en 3lock Statement42 %ill .e e-ecuted. In similar %a/ if at last no matc& found for t&e 7aria.le t&en default statement is e-ecuted. 8ere %e &a7e to (ut t&e .reak statement to com(lete t&e e-ecution of s%itc& statement after matc&ing one case. 9 9 9 Block Statement-1 ,reak# Block Statement-2 ,reak# Block Statement-) ,reak# Block Statement-4 ,reak#

7aria.le for matc&

> >?1
3lock Statement41

> >?2
3lock Statement42

> >?
3lock Statement4

default
3lock Statement4!

Statement4#

Consider t&e follo%ing e-am(le of t&e s%itc& statement to acce(t t&e mont& in num.er and (rint into %ord. s6itc7(mont7) { case 1 case 2 3/ %%%.s&are.ca.com 9 9 printf(:an ar;")# ,reak# printf(3e,r ar;")# S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

C Programming, Decision Making case . case 10 defa lt } In a.o7e (rogram if /ou enter mont& e2ual to 1 t&en (rint @anuar/, mont& e2ual to 2 t&en 5e.ruar/, mont& e2ual to # t&en Ma/, mont& e2ual to 1: t&en Octo.er. If no an/ matc& is found for mont& t&en (rint, ;9ou &a7e entered %rong 7alue<. Similar to nested if...else /ou can use nested s%itc& statement. 9ou can use onl/ %& level o nested s!itc". 0lso /ou can take onl/ '&( cases into s!itc" statement. 9 9 9 ,reak# printf(%a;")# ,reak# printf(<cto,er")# ,reak# printf(Yo 7a8e entered 6ron/ 8al e")# ,reak#

Conditional Operator OR ): Operator: C (ro7ides t&e facilit/ of t&e o(erator to c&eck t&e condition ./ using t&e conditional o(erator &a7ing t&ree o(erands. 1&e format of t&e condition* (4ondition)= Statement1 9 Statement2 If condition is true t&en statement1 %ill .e e-ecuted ot&er%ise statement2 %ill .e e-ecuted. 8ere /ou can also re(resent t&e condition for assignment o(eration* 8aria,le & (4ondition)= >al e1 9 >al e2 8ere if condition is true t&en 7aria.le &as assigned t&e 7alue1 ot&er%ise it &as assigned t&e 7alue2. Consider t&e follo%ing e-am(le to understand t&e conditional o(erator* commission & (sales * .0000) = 2000 9 0# In a.o7e e-am(le if /our sales is greater t&an #:::: t&en /our commission 7alue is 2::: ot&er%ise it is 6ero. 0.o7e condition /ou can re(resent using if statement as under* if (sales * .0000) { commission&2000# } else { commission&0# } 1&e ad7antage of t&e conditional o(erator is t&at /ou can re(resent t&e condition in s&orter form. 3ut some times it is difficult to understand %&en /ou are using multi(le conditional o(erators.

goto Statement: goto is one kind of Aum( statement in t&e C language. It transfers /our control from one statement to anot&er statement randoml/. In goto statement t&ere is one la.el is used to indicate at %&ic& (osition I &a7e to make t&e Aum(. Ba.el is end %it& t&e colon C*D. ,&ene7er /ou &a7e to make t&e Aum( /ou &a7e to %rite statement goto Label. 1&ere are t%o kind of Aum( (erformed ./ goto* 1. 5or%ard Aum(* 8ere t&e Aum( is made to t&e ne-t follo%ing statements. If la.el is after t&e goto statement t&en t&is kind of situation is arise. goto ?@BA?# BBBBBB BBBBBB

3/ %%%.s&are.ca.com

S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

C Programming, Decision Making ?@BA? 9 2. 3ack%ard Aum(* 8ere t&e Aum( is made to t&e (re7ious statements. ,&en la.el is .efore t&e goto statement t&en t&is kind of situation is arise. 1&e s/nta- of .ack%ard Aum( is, ?@BA? 9 BBBBBB BBBBBB BBBBBB goto ?@BA?# Consider t&e follo%ing e-am(le to understand goto statement, main( ) { int a( ,( c# 4@? 9 printf(Anter a C , 9 )# scanf('d'd"(Ca(C,)# c&aD,# printf(4 & 'd!n"(c)# if(c<&0) { /oto 4@?# } } 8ere t&e (rogram is used to calculate t&e c 7alue u( to t&e 7alue is not going to .e larger t&en 6ero. Eenerall/ goto statement is used to read t&e data again in /our (rogram. It is also used in t&e loo( to .reak t&e loo(, 67ile(condition) { BBBBBB.. /oto <E$# BBBBBB. } <E$ 9 BBBBBBB ,e &a7e to a7oid t&e use of goto statement .ecause of 1. Some com(iler generates less efficient Cai[C) XmtiviL&D code if /ou are using goto statement. 2. 9our (rogram .ecomes unreada.le. . Program logic .ecomes com(licated and (rogram .ecomes less under4sta.le.

Pro*lems
1. 2. . !. #. $. '. Program to acce(t /our %eig&t and if /our %eig&t is greater t&an +# t&en com(uter %ill (rint t&e message ;9ou are 5at. Please go to &ealt& clu..< Program to acce(t /our gender from ke/.oard as M or 5 and (rint t&e message ;9ou are Male< or ;9ou are 5emale<. Program to determine t&e commission gi7en to sales man .ased on t&e condition* If t&e sales is greater t&an 2:::: Rs. t&en, if t&e .asic salar/ of t&e salesman is greater t&an #:::Rs. t&en 2:F commission is gi7en else 1:F commission is gi7en. Program to acce(t t&e t&ree edges of t&e triangle and determine %&et&er t&is triangle is rig&t angle or not. Program to im(lement t&e calculator using t&e s%itc& statement. Im(lement /our an/ (rogram %&ic& is e-ecuted again and again until /ou are gi7ing no %&ene7er it ask a.out /our c&oice ,ould /ou like to continue G9H"I)<. ,rite a (rogram to (rint num.er from 1 to n using onl/ if condition and goto statement. S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

3/ %%%.s&are.ca.com

C Programming, Decision Making +. K. 1:. 11. 12. 1 . ,rite a (rogram to read /our age and if /our age is greater t&an $# t&en /ou are getting #: F discount in .us fare. 0lso if /ou are female t&an again /ou are getting #: F discount. 0t t&e end of (rogram (rint discount and net .us fare CJse nested ifD ,rite a (rogram to c&eck odd and e7en num.er. ,rite a (rogram to c&eck %&et&er t&e num.er is di7isi.le ./ K or not. ,rite a (rogram to determine to c&eck %&et&er t&e num.er is di7isi.le ./ 1 and 11. ,rite a (rogram to determine to c&eck %&et&er t&e num.er is di7isi.le ./ 1' or 1K. Program to determine t&e income ta- .ased on t&e follo%ing condition. Income Income4ta=> 1,#:,::: !: F => $:,::: 2: F => #:,::: 1: F ,&at %ill .e t&e 7alue of a for eac& gi7en case. a > . = c ) c = d ) 12 * d = e ) 1 * 1! * 1# Cases are* I. . > #, c > 1#, d > e > +L II. . > 1#, c > 1:, d>e>+L III. . > 1#, c > 1:, d > e> 2: I?. . > c > K, d > 2:, e > 1K

1!.

3/ %%%.s&are.ca.com

S&are 3C0.Com 0 (lace to learn and s&are all a.out 3C0

You might also like