Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.

Similar presentations


Presentation on theme: "Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control."— Presentation transcript:

1 Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control the flow of execution, they are also known as control statements. In C these statements are: a)If statement b)Switch statement

2 If statement It can be used in following ways: Simple if statement If-else statement If-else if statement Nested if statement

3 S IMPLE IF STATEMENT SYNTAX: If (this condition is true) { statement; } Note: If there is only one statement in if block then there is no need of curly braces.

4 G ENERAL CONDITIONS USED IN FOR LOOP x==y x!=y xy x<=y x>=y x is equal to y x is not equal to y x is less than y x is greater than y x is less than or equal to y x is greater than or equal to y This expression Is true if

5 E XAMPLE : #include void main() { int i; i=5; if(i==5) { printf(Inside if \n); printf(This proves i=5); } Output: Inside if This proves i=5

6 IF - ELSE STATEMENT Syntax: if(condition) { no. of statements } else { no. of statements } start Condi- tion Statem- ent Execute statement NO YE S

7 E XAMPLE : #include void main() { int x; x=2; if(x<10) printf(x<10 is true \n); else printf(x<10 is false); printf(this is outside if and else); } OUTPUT: x<10 is true This is outside if and else

8 IF - ELSE IF STATEMENT ( LADDER IF ) syntax: if(condition) { no. of statements; } else if(condition) { no. of statements; } else { no. of statements; }

9 E XAMPLE : #include void main() { int a; a=1; if(a==4) printf(Im in 4 th yr); else if(a==3) printf(Im in 3 rd yr); else if(a==2) printf(Im in 2 nd yr); else printf(Im in 1 st yr); } OUTPUT: Im in 1 st yr

10 N ESTED IF STATEMENT A conditional statement inside another conditional statement is called nested conditional statement. They are used to implement multi way decision. syntax: if(condition) { if(condition) { no. of statements; } else { if(condition) no. of statements; }

11 E XAMPLE : #include void main() { int a; char c; a=20; c=f; if(c==f) { printf(Costumer is female \n); if(a< 16) printf(Her age is less than 16, so shell get a concession of 5%); }

12 else { printf(Costumer is male\n); if(a>50) printf(His age is more than 50, so hell get a concession of 5%); } OUTPUT: Costumer is female OUTPUT: Costumer is female

13 NOTE: If in above example, a=10 ; Then, If in above example, a=60; c=m; OUTPUT: Costumer is female Her age is less than 16, so shell get a concession of 5% OUTPUT: Costumer is male His age is more than 50, so hell get a concession of 5%

14 I F ………. IF.. ELSE ……….. ELSE : Syntax: if(condition) { if(condition) execute the body else execute the body } Else { execute the body }

15 E XAMPLE : #include void main() { int a,b; a=10; b=20; if(a!=b) { printf(a is not equal to b); if(a>b) printf(a is greater than b);

16 else printf(a is less than b); } else printf(a is equal to b); } OUTPUT: a is not equal to b a is less than b OUTPUT: a is not equal to b a is less than b

17 S WITCH S TATEMENT It can be used to select one option among multiple options. switch can replace if-else if statement(ladder if) syntax: switch(variable) { case value1: no. of statements; break; case value2: no. of statements; break; case valueN: no. of statements; break; default: no. of statements; }

18 There is no need to write break statement at last option. If you do not write break statement with any option the next option will be executed. default can be put any where in the switch block. Example 1: #include void main() { int t; t=2; switch(t) { case 1: printf(t is equal to 1\n); break; case 2: printf(t is equal to 2\n); break; case 3: printf(t is equal to 3\n); break; default: printf(t is not found\n); } OUTPUT: t is equal to 2

19 E XAMPLE 2: #include void main() { int t; t=2; switch(t) { case 1: printf(t is equal to 1\n); break; case 2: printf(t is equal to 2\n); case 3: printf(t is equal to 3\n); default: printf(t is not found\n); } OUTPUT: t is equal to 2 t is equal to 3 t is not found

20 E XAMPLE 3: #include void main() { int t; t=5; switch(t) { case 1: printf(t is equal to 1); break; default: printf(t is not found); case 2: printf(t is equal to 2); break; case 3: printf(t is equal to 3); } OUTPUT: t is not found t is equal to 2

21 ?: OPERATOR This is a operator which is capable of controlling the flow of control. syntax: Conditional expression ? Expression 1: expression 2 Conditional expression Expression 1 Expression 2 : ?

22 Example: 1. ….. int a=5; a>5?printf(a=5):printf(a!=5); …. Output: a=5 2. ….. int a=6; a>5?printf(a=5):printf(a!=5); …. Output: a!=5


Download ppt "Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control."

Similar presentations


Ads by Google