Sunday, 1 July 2012

how to declare & use else if() ladder in c programming-syntax-example program

Else if() ladder is a conditional control statement in c similar to if else control statement. else if() ladder is used to check for another condition if the previously specified condition becomes false. That means when a condition gets false then we can check for an another condition by using else if ladder. So by using else if() statement, when a condition is specified and if the condition becomes true, then the block of statements under if() block will be executed. if the condition becomes false, then it will check for another condition. This is also called as nested if else statement in c. so by using this process we can create any number of if else statements.

Else if() ladder in c programming:

Else if ladder can be declare by specifying a condition in the else block of a previously defined if else statement. so when ever the specified condition in the if statement becomes false, then the control enters into else block and there we declared another if statement, so it will again check for another condition. This process can be done for each and every else block. Every else block carries a if statement. hence it is referred as else if ladder.

syntax for else if ladder control statement :-

if(<condition>)
{
-----------------------
-----------------------
block of statements
-----------------------
-----------------------
}
else if(condition)
{
-----------------------
-----------------------
block of statements
-----------------------
-----------------------
}
else
{
-----------------------
-----------------------
block of statements
-----------------------
-----------------------
}

Example to use else if ladder control statement in c programming:-

if(a<0)
{
printf("\n a is negative");
}
else if(a%2==0)
{
printf("\n a is positive");
printf("\n a is even number");
}
else
{
printf("\n a is positive");
printf("\n a is odd number");
}
printf("\n completed");
In the above example we are specifying a condition a<0. if the condition(a<0) is true, then the control enters into the if() block and executes the block of statements under if() block. If the condition(a<0) gets false, then the control checks for an another condition(a%2==0). If this condition becomes true, then it executes the block of statements under else if block and if the condition becomes false then the block of statements under else will be executed, and continued to the next statement after that block.
  • In the above example if we give a=-3, then the condition(a<0) becomes true and the control enters into the if block and prints the message that "a is negative". then it goes to the next statement and prints the message "completed".
  • In the same example if we give a=6, then the condition(a<0) becomes false and then it checks for another condition (a&2==0) and this condition becomes true. so the control enters into the else if block and prints the message "a is positive." and "a is even number". then it goes to the next statement and prints the message "completed".
  • In the same example if we give a=5, then the condition(a<0) becomes false and then it checks for another condition (a&2==0) and this condition also becomes false. so the control enters into the else block and prints the message "a is positive." and "a is odd number" then it goes to the next statement and prints the message "completed".
Output for above example in all the three cases is
  • when a=-3
    a is negative
    completed
  • when a=6
    a is positive
    a is even number
    completed
  • when a=5
    a is positive
    a is odd number
    completed.

Important points to remember while using else if ladder :-

  • we should be very careful while working with else if ladder as it involves multiple if else statements.
  • As a condition has only two choices whether it is true or false, so only the block of statements either under if block or else block are executed. the statements under both the blocks are not executed at a single time. only one block of statements either if or else if or else are executed at a single time depending upon the condition specified.

Example c program to use else if ladder :-

#include<stdio.h>
int main(void)
{
int a;
printf("\n enter a number");
scanf("%d",&a);
if(a<0)
{
printf("\n a is negative");
}
else if(a%2==0)
{
printf("\n a is positive");
printf("\n a is even number");
}
else
{
printf("\n a is positive");
printf("\n a is odd number");
}
printf("\n task completed");
return 0;
}
Output :-
1) enter a number -3
   a is negative
   completed

2) enter a number 6
   a is positive
   a is even number
   completed

3) enter a number 5
   a is positive
   a is odd number
   completed.

Rules To be Followed While Using Else if ladder Statement :-

  • The block of statements should be enclosed under '{' and '}'
  • If no '{' and '}' is specified then it treats only the first statement under if() block and applies the condition for only the first statement. So if there are two or more statements under if block then specify them under '{' and '}'. The { and } indicates a block. It will also raise an error that misplaced else if you skip '{' and '}' at if block if it contains more than one statement.
  • The block of statements declared under else if block are also to be enclosed between '{' and '}'
  • while specifying a condition for equality we should use the operator '==' for example if you want to check the condition for a=5, then write the syntax as
    if(a==5)
    {
    ----------------------
    block of statements
    ----------------------
    }
  • we should only use relational operators or a boolean value in a if statement for specifying the condition.

Commonly Used Operators in else if() Statement for specifying condition :-

  • "==" used to check for the condition whether value at left is equal to value at right.
  • ">" used to check for the condition whether value at left is greater than value at right.
  • "<" used to check for the condition whether value at left is less than value at right.
  • ">=" used to check for the condition whether value at left is greater than or equal to value at right.
  • "<=" used to check for the condition whether value at left is less than or equal to value at right.
  • "!=" used to check for the condition whether value at left is not equal to value at right.
The above are the most commonly used operators for checking the condition through else if() statement.

Conclusion:

Else if ladder is a control statement used in c programming language for declaring multiple if statements. it is also called as nested if statement. we can specify one or more condition in an else block to check for another condition if the previously defined condition becomes false. Hope this article helped you. If you have any doubts, then share them in the comments below. Thank you.......

We Will Send Some More Information For Free!

Like This Story? Please Share!

Other Ways To Share:

Like Our Site? Follow Us!


0 comments:

Post a Comment

Your feedback is always appreciated. We will try to reply to your queries as soon as time allows.
Please do not spam. Spam comments will be deleted upon our review.

Regards:
Srinath Reddy.

 

Articles cannot be reproduced without permission from the author.

BEGINNERS-TUTORIALS © 2013. All Rights Reserved | Contact | About