Sunday, 6 July 2014

Use of Logical Operators in C programs

Logical Operator play very important role in C programming. Here mainly 3 types of logical operators,
&& – logical AND
|| – logical OR
! – logical NOT
Don’t write single & or single | which will make different meaning for this symbols. The first two operators, && and || help us to set two or more than two conditions in a single statement.
For && operator if both side conditions is true then it’s return true and for || operator if single side condition is true then it will return false.
If we create a table to see how it’s work then

For && operator
Condition1 Condition2 Output
True True True
True False False
False True False
False False False

For || operator
Condition1 Condition2 Output
True True True
True False True
False True True
False False False

Now let’s write a program for && operator
#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter Value For a\n");
    scanf("%d",&a);
    printf("Enter Value For b\n");
    scanf("%d",&b);
    printf("Enter Value For c\n");
    scanf("%d",&c);
    //using only "if else" statement
    if(a>b)
    {
        if(a>c)
        {
            printf("a have max value\n");
        }
    }
    else if(b>c)
    {
        printf("b have max value\n");
    }
    else
    {
        printf("c have max value\n");
    }
    //using Logical operator
    if((a>b)&&(a>c))
    {
        printf("a have max value\n");
    }
    if((b>a)&&(b>c))
    {
        printf("b have max value\n");
    }
    if((c>a)&&(c>b))
    {
        printf("c have max value\n");
    }
}
Now let’s see output for this program.
Enter Value For a
45
Enter Value For b
32
Enter Value For c
66
c have max value
c have max value
first result for without using && and second result is with &&.
Now let’s write a program for || operator
Here before we see program let’s set the condition. If a given value is grater than 20 or equal to 10 or less than 5 then this condition is true.
#include<stdio.h>
void main()
{
    int a;
    printf("Enter value for a\n");
    scanf("%d",&a);
    if((a<5)||(a==10)||(a>20))
    {
        printf("Condition is True");
    }
    else
    {
        printf("Condition is Not True");
    }
}
Now let’s see the output for this program
Enter value for a
4
Condition is True
Enter value for a
11
Condition is Not True
So we know about && and || but we don’t know about “!” operator. This NOT(!) operator is not used so much in programming. Let’s clear the idea about it by writing a program.
#include<stdio.h>
void main()
{
    int a;
    printf("Enter value for a");
    scanf("%d",&a);
    if(!(a>7))
    {
        printf("Value of a is smaller than 7");
    }
    else
    {
        printf("Value of a is bigger than 7");
    }
}
Let me explain this for you. Here if “a” is smaller than 7 the condition becomes true, that’s means just opposite of (a>7) condition. I know that’s confusing, but you can avoid it in programming.
Now let’s see the output for this program.
Enter value for a
6
Value of a is smaller than 7

0 comments:

Post a Comment