Introduction to Logical Operators in C

Discussion in 'C' started by lionaneesh, Jun 17, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    In this tutorial we’ll be learning about LOGICAL operators in C , What are they and How they can be used. These are also a fundamental part of C programming Language, and You’ll find them in almost every useful code using conditions.

    Note : C Generally denotes 1 for True and 0 for False. (While dealing with conditions )

    So what’s the waiting then let’s get started

    What are Logical Operators in C?



    While writing our C programs, we might feel a dire need for combining several conditions into one block and save some lines of code, make it more readable and more reproduce able. C has a special set of operator know as LOGICAL Operators to get the job done.

    Note: The operands(conditions) of logical-AND and logical-OR expressions are evaluated from left to right. If the value of the first operand(condition) is sufficient to determine the result of the operation, the second operand is not evaluated. This is a very important point to remember in our minds.

    There are 2 types of logical operators in C
    1. Logical ‘AND’
    2. Logical ‘OR’
    Logical ‘AND’ is denoted by ‘&&’, What is does is basically combine some conditions and Return True if all of them are true and return false if even one of them is false.

    Note : If the first operand of a logical-AND operation is equal to 0 (FALSE), the second operand is not evaluated.

    This can be simplified using the following example:-

    Let’s take an English statement and try to convert them in C conditions.

    If it’s raining and the wind is blowing I will not go to school

    Translation:-
    Code:
        if(it is raining and wind is blowing)
        {
                i will not go to school;
        }
     
    So for this code to execute ‘I will not go to school’ 2 conditions need to be true :-
    1. It is raining
    2. Wind is blowing
    If even one of them is false the program will not execute ‘I will not go to school’

    I hope this makes it clear now.

    Logical ‘OR’

    Logical ‘OR’ is denoted by ‘||’, What is does is basically combine some conditions and Return True if even one of them is true and return false if all of them are false.

    Note : If the first operand of a logical-OR operation is equal to 1 (TRUE), the second operand is not evaluated.

    This can be simplified using the same type of example:-

    Let’s take an English statement and try to convert them in C conditions.

    If I am ill or it’s a weekend I’ll not go to the office

    Translation:-
    Code:
        if(I am ill or it’s a weekend)
        {
                I’ll not go to the office;
        }
     
    So for this code to execute ‘I’ll not go to the office’ one of the two
    conditions need to be true :-
    1. I am ill
    2. It’s a weekend

    Coding



    Now that we know some Logical Operators in C, Let’s use them to build some conditional statements and How they can be used in a C program.
    Code:
     
    #include<stdio.h>
    
    int main()
    {
        int i = 22;
        if(i < 100 && i != 19)
        {
            printf("i is less than 100 and not equal to 19\n");
        }
        else if(i == 100 || i == 99)
        {
            printf("i equals to 100 or 99\n");
        }
        else if(i > 200 && i < 200)
        {
            printf("i is grater than 200 and less than 200 , This can never be true\n");
        }
        else
        {
            printf("We are in else block\n");
        }
        return(0);
    } 
    The above code is a example of how we can use conditions in a C program. Lets compile and Run it.

    Output :-

    Code:
    i is less than 100 and not equal to 19 
    
    Exercise :-

    It is left as an exercise to users to Check this program for different values of ‘i’ and post their output in Comments.
     
  2. Delois

    Delois New Member

    Joined:
    Aug 6, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    nicely explained example for logical operators. when I was new at c language I used to surf net too much for this types of programs but I never did a search in forums I think that time I should be the member of this types of forums to communicate directly with professional like you to get knowledge with real concepts.
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks that means a lot! :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice