Tuesday, November 10, 2009

Else if ladder in C programming

Too many if,else block may be very cumbersome.This situation can be avoided using else if ladder.The syntax of else if ladder is :
if ( condition 1)
true block of the above decision
else if ( condition 2)
true block of the above decision
else if (condition 3)
true block of the above decision
............................................
else
default block.

This block will be
executed if all the above decisions
returns false.This else is optional

In an else if ladder the conditions are evaluated from the top of the ladder. As soon as match is found in else if ladder, the true block associated with the condition is executed and the control leaves the else if ladder and executes the statement following the ladder. If no match is found in the ladder, then the last else containing the default statements are executed. This final else is optional.

 C Program on else if ladder

#include
void main ()
{
int a, b, c;
clrscr ();
printf ("Enter three numbers:");
scanf ("%d%d%d", &a, &b, &c);
if ((a>b)&&(a>c))
{
printf ("The maximum is: %d", a);
}
else if ((b>c)&&(b>a))
{
printf ("The maximum is: %d", b);
}
else
{
printf("The maximum number is :-%d”,c);
}
getch ();
}

Program on student result using else if ladder

In our next program the user will enter an integer value (marks of a student) .The following decision has been taken:
i. marks greater than 90 --- excellent
ii. marks within the range of 76 to 90 --- very good
iii. marks within the range of 61 to 75 --- good
iv. marks within the range of 51 to 60 --- average
v. else --- very poor

#include
void main ()
{
int marks;
clrscr ();
printf ("Enter marks:");
scanf ("%d", &marks);
if (marks>90)
printf ("Excellent");
else if (marks>75)
printf ("Very good");
else if (marks>60)
printf ("Good");
else if (marks>50)
printf ("Averaged");
else
printf ("Very poor");
getch ();
}

2 comments:

  1. how to make a program if we have to calcilate income tax allowed on basic salary using ladder if programming?

    ReplyDelete
  2. program on income tax calculation using else if ladder
    Here is the slab of tax
    Basic below 1000: no tax
    Basic>=1000 and Basic<2000: 4% tax on basic
    Basic>=2000 and Basic<3000: 6% tax on basic
    Basic>=3000 and Basic<4000: 8% tax on basic
    on rest Basic: 10% tax on basic

    #include < stdio.h>

    void main()

    {
    float basic,tax;
    clrscr();
    printf("\nEnter the Basic salary:");

    scanf("%f",&basic);

    if(basic< 1000)
    tax=0;
    else if(basic< 2000)
    tax=basic*4.0/100;
    else if(basic< 3000)
    tax=basic*6.0/100;
    else if(basic< 4000)
    tax=basic*8.0/100;
    else
    tax=basic*10.0/100;
    printf("\nPayable Tax=%.2f",tax);
    getch();
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner