1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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
    Conditional Statements (also called Conditions) are features of a programming language , Which allows it to perform actions depending upon some conditions provided by the programmer , This provides a “ DECIDING” functionality to a computer program and allows for flexible and easy code development.

    Conditions are one of the most fundamental features of a C Program , You’ll see them In almost every useful C code.

    In C the conditional statements generally follows the if - else if - else constructs

    Code:
    if(condition is true)
    {
                do something;
    }
    else if(condition2 is true)
    {
              do something;
    }
    else
    {
                do something;
    }
    

    The ‘if’ statement



    The if statement in C is quite similar to the ‘if’ statement in English , Consider the following examples :-

    “If I am not tired I will surely go to work”
    “if tomorrow is a holiday I will go to Mc Donald’s”

    In the above statements we have a condition and on the basis of that condition we have a task associated with it, if the person is not tired he will surely go to work. Now let’s break this statement.

    Condition : ‘if the person is not tired’
    Task: He’ll go to work


    An if statement in C is generally in the following form:-
    Code:
    if(condition is true)
        {
                do something;
        }
    
    Between ‘(‘ and ‘)’ can be any valid C condition , and based on which the appropriate decision is taken.

    The ‘Else if’ statement



    The ‘else if’ statement in C is generally used when we need to compare more than one condition

    The else if statement is generally used in following form :-
    Code:
    if(condition is true)
        {
                do something;
        }
        [COLOR="Red"]else if(condition2 is true)
        {
            
        }
        [/COLOR]else
        {
                do something;
        }
    
    The else if block is executed if :-
    1. All the preceding conditions failed and
    2. The condition provided is true

    The Else Statement


    The else statement in C is generally in the following form
    Code:
    else
    {
    	Do something;
    }
    
    The else block is executed if the all the preceding conditions returned false.

    That’s all for this tutorial stay tuned for more.
     

Share This Page