22
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

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

Embed Size (px)

Citation preview

Page 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

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 statementb)Switch statement

Page 2: 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

If statement It can be used in following ways:

Simple if statement

If-else statement

If-else if statement

Nested if statement

Page 3: 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

SIMPLE IF SIMPLE IF STATEMENTSTATEMENT

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.

Page 4: 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

GENERAL CONDITIONS USED IN FOR LOOP

x==y x!=y x<y x>y 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

Page 5: 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

EXAMPLE:#include <stdio.h>void main(){int i;i=5;if(i==5){printf(“Inside if \n”);printf(“This proves i=5”);}}

Output:Inside ifThis proves i=5

Page 6: 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

IF-ELSE STATEMENTIF-ELSE STATEMENTSyntax:if(condition){

no. of statements}else{

no. of statements}

start

Condi-tion

Statem-ent

Execute statement

NO

YES

Page 7: 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

EXAMPLE:#include<stdio.h>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 trueThis is outside if and else

Page 8: 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

IF-ELSE IF STATEMENT IF-ELSE IF STATEMENT (LADDER IF)(LADDER IF)

syntax:

if(condition){

no. of statements;}else if(condition){

no. of statements;}else{

no. of statements;}

Page 9: 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

EXAMPLE:

#include<stdio.h>void main(){int a;a=1;if(a==4)printf(“I’m in 4th yr”);else if(a==3)

printf(“I’m in 3rd yr”);else if(a==2)

printf(“I’m in 2nd yr”);else

printf(“I’m in 1st yr”);

}

OUTPUT:I’m in 1st yr

Page 10: 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

NESTED IF STATEMENTNESTED IF STATEMENTA 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;

}

Page 11: 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

EXAMPLE:

#include<stdio.h>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 she’ll get a concession of 5%”);

}

Page 12: 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

else{

printf(“Costumer is male\n”);if(a>50)printf(“His age is more than 50, so he’ll get a concession of 5%”);

}}

OUTPUT:Costumer is female

Page 13: 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

NOTE:If in above example,

a=10 ;

Then,

If in above example,

a=60;

c=‘m’;

OUTPUT:Costumer is femaleHer age is less than 16, so she’ll get a concession of 5%

OUTPUT:Costumer is maleHis age is more than 50, so he’ll get a concession of 5%

Page 14: 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

IF……….IF..ELSE………..ELSE:

Syntax: if(condition){

if(condition)execute the body

elseexecute the body

}

Else{

execute the body}

Page 15: 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

EXAMPLE:

#include<stdio.h>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”);

Page 16: 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

elseprintf(“a is less than b”);

}elseprintf(“a is equal to b”);}

OUTPUT:a is not equal to ba is less than b

Page 17: 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

SWITCH STATEMENTSWITCH STATEMENTIt 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;

}

Page 18: 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

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<stdio.h>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

Page 19: 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

EXAMPLE2:

#include<stdio.h>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 2t is equal to 3t is not found

Page 20: 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

EXAMPLE3:

#include<stdio.h>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 foundt is equal to 2

Page 21: 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

?: OPERATOR This is a operator which is capable ofcontrolling the flow of control. syntax:Conditional expression ? Expression

1: expression 2

Conditional expression

Expression 1Expression 2

: ?

Page 22: 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

Example:1.…..int a=5;a>5?printf(“a=5”):printf(“a!=5”);….Output:a=52.…..int a=6;a>5?printf(“a=5”):printf(“a!=5”);….Output:a!=5