Selection Feature of C: Decision making statements: It allow us to take decisions as to which code...

Preview:

Citation preview

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

If statement It can be used in following ways:

Simple if statement

If-else statement

If-else if statement

Nested if statement

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.

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

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

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

no. of statements}else{

no. of statements}

start

Condi-tion

Statem-ent

Execute statement

NO

YES

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

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;}

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

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;

}

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%”);

}

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

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%

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

Syntax: if(condition){

if(condition)execute the body

elseexecute the body

}

Else{

execute the body}

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”);

elseprintf(“a is less than b”);

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

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

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;

}

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

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

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

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

: ?

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

Recommended