Using if-else Conditional Statements in C

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

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    This article is a continuation of my previous article on conditions in C. In the previous article we talked about conditions in C and some if – else statements now in this article we’ll be focusing o how to use these if-else statement in a C program.

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

    How to Form Conditions in C



    In C we use some special operators usually called ‘Relational Operators’ to Construct and Form conditions.

    List of the Relational Operators in C :-
    Code:
    [B]Operator		Description
    [/B]
    ==			Equals to
    != 			No equals to
    <			Less than
    >			Greater than
    >=			Greater than equals to
    
    
    That’s much of Theory there now lets start Coding.

    Note: Most of the time programmers use ‘=’ is place of ‘==’ its one of the most nasty bugs in a C program and often difficult to find. This causes a logical error and can mess up the whole logic of the program.

    Coding



    Now that we know some Relational 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 = 100;
        if(i < 100)
        {
            printf("i is less than 100\n");
        }
        if(i == 100)
        {
            printf("i equals to 100\n");
        }
        else if(i == 200)
        {
            printf("i equals to 200\n");
        }
        else if(i > 200)
        {
            printf("i is grater than 200\n");
        }
        else
        {
            printf("We will never get here\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 equals to 100
    
    Exercise :-

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

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