Pages

Saturday, October 10, 2009

Using the logical operators with If

There will be situations where we want to use more than one condition at a time. For example if a company gives promotions to its employees based on two conditions a) They should have an experience of more than 3 years and b) They should score more than 7 points in personal interview. Now an employee has two conditions to fulfill – his/her experience should be more than 3 years and at the same time he/she must score more than 7 points. In such cases where there are multiple conditions we can use either and which is symbolized as ‘&&’ and or which is represented as ‘||’. The && (and) operators combines two or more given conditions and if all of them evaluate to true returns the result as true. On the other hand || (or) will return true if even one of the given conditions is true. Let’s start with && which uses the following logical table


True + True = True

True+ False = False

False+ True = False

False+ False = False

As evident from the above table, and operator when used will yield TRUE only if all the given conditions are true, otherwise it will return false.

Example 1

void main()

{

int experience,points;

printf(“\nEnter Your Experience (In Years) : “);

scanf(“%d”,&experience);

printf(“\nEnter points scored in personal interview : “);

scanf(“%d”,&points);

if(experience>3 && points>7)

printf(“\nYou will be promoted”);

else

printf(“\nYou do not meet the specified criteria”);

}

Notes :

1. Read && as “And”.

2. The message “You will be promoted” will be printed only if both experience is more than 3 years and points are higher than 7. If any of these values is less the message given in the else part will be printed.

Here is the output if both conditions are true

Enter Your Experience (In Years) : 5

Enter points scored in personal interview : 9

You will be promoted

If one condition fails, here is what you will see

Enter Your Experience (In Years) : 2

Enter points scored in personal interview : 8

You do not meet the specified criteria

Example 2

void main()

{

float height,speed;

char grade;

printf(“\nWhat is the height of the candidate “);

scanf(“%f”,&height);

printf(“\nWhat was the speed in seconds to compelte 100 meters “);

scanf(“%f”,&speed);

fflush(stdin);

printf(“\nWhat was the grade in the written examination “);

scanf(“%c”,&grade);

if(height>6.0 && speed<=13 && grade==’A’)

printf(“\nYou have cleared the first phase of selection”);

else

printf(“\nSorry ! You have failed in at least one parameter”);

}

Note :

1. There can be as many conditions joined by &&.

2. If you enter a small ‘a’ as grade, the condition will become false.

The OR(||) operator

The Or operator is a bit different from && as it returns true even if any of the given conditions is true. It uses the following logical table

True + True = True

True+ False = True

False+ True = True

False+ False = False

As given in the above table OR will return false, if all the conditions are false.

Example 1

void main()

{

int sub1,sub2,sub3;

printf(“\nEnter marks in subject 1 “);

scanf(“%d”,&sub1);

printf(“\nEnter marks in subject 2 “);

scanf(“%d”,&sub2);

printf(“\nEnter marks in subject 3 “);

scanf(“%d”,&sub3);

if(sub1>=60 || sub2>=60 || sub3>=60)

printf(“\nPASS”);

else

printf(“\nFAIL”);

}

0 comments:

Post a Comment