There will be many cases where just an if - else won't work, since it can include just one condition. If there are multiple conditions to evaluate we can use the if-else if statement. Consider the following program where we want to find out profit or loss of a particular transaction.
void main()
{
int cp,sp;
printf(“\nEnter cost price : “);
scanf(“%d”,&cp);
printf(“\nEnter selling price : “);
scanf(“%d”,&sp);
if(sp>cp)
printf(“\nProfit”);
else
printf(“\nLoss”);
}
If the program in run and we enter 100 as the cost price and 200 as the selling price, we will see that the program will display the result as profit since the condition has resulted in true. On the other hand had we entered cost price as 200 and selling price as 100 the result would have been “Loss”. But what if we enter 100 as both the selling price and cost price? Enter the same value (100, 200 or whatever) at both the places you will see that the result will displayed as “Loss” !!. Why is this happening is pretty simple, just because when you enter same value, say 100 at both the places the condition becomes false since 100 is not greater than 100 and therefore prints “Loss”. To correct the problem we can introduce the else if statement in our program. Check the correct version yourself.
void main()
{
int cp,sp;
printf(“\nEnter cost price : “);
scanf(“%d”,&cp);
printf(“\nEnter selling price : “);
scanf(“%d”,&sp);
if(sp>cp)
printf(“\nProfit”);
else if (cp>sp)
printf(“\nLoss”);
else
printf(“\nNo profit or loss”);
}
As you can see now the else if statement is taking care of “cp being higher than sp” and if it is it will again print “Loss”. Now if we enter 100 again in both prompts the compiler first checks if sp (which is 100) is greater than cp (which also is 100). Since 100 is not greater than 100 it skips to the else if part, incidentally the same processing happens again and 100 is again compared with 100. Since both the above conditions are false the only option left for the compiler is to execute the statements given in the else part. Therefore the result will be “No profit or loss”.
Remember
1. Place a space between else and if.
2. You can use as many else if in a program but there can be only one else statement and that too at the end.
Example 2 – Menu Based Program
A menu based program simply refers to a program that displays a list of available options to the user and proceeds as per the choice made. Take a look
void main()
{
int option;
printf(“\nMain menu”);
printf(“\n1. Air ticket”);
printf(“\n2. Train ticket”);
printf(“\n3. Bus ticket”);
printf(“\nSelect your mode of transport”);
scanf(“%d”,&option);
if(option==1)
printf(“\nRs. 25000”);
else if(option==2)
printf(“\nRs. 500”);
else if(option==3)
printf(“\nRs. 230”);
else
printf(“\nInvalid choice selected by the user”);
}
The above program displays a list of option to the user to select from. When run, the output will look like this
1. Air Ticket
2. Train Ticket
3. Bus Ticket
Select your mode of transport
Now if the user enters “1” as his choice the program will display the result as “Rs. 25000” but if you enter your choice as “2”, it will display Rs. 500 and Rs. 230 if 3 is entered. If we enter 2 or 3 as our input the compiler doesn’t directly jump to the else if statement. It will start comparing the conditions one by one and will evaluate the result accordingly. If any other choice is made except 1,2 or 3 all the above given condition will evaluate to false and the program will display the “Invalid Choice” message.
Menu Based Program Using char Input
We can also create a menu based program using character input. The only difference is that we will have to use single quotation (‘’) marks. Consider the example given below.
void main()
{
char op;
int no1,no2,result;
printf(“\nEnter first number “);
scanf(“%d”,&no1);
printf(“\nEnter second number “);
scanf(“%d”,&no2);
fflush(stdin);
printf(“\nEnter a symbol like (+,-,*,/) “);
scanf(“%c”,&op);
if(op==’+’)
{
result=no1+no2;
printf(“\n Addition of both the numbers is %d”, result);
}
else if if(op==’-’)
{
result=no1-no2;
printf(“\n Subtraction of both the numbers is %d”,result);
}
else if(op==’*’)
{
result=no1*no2;
printf(“\n Product of both the numbers is %d”, result);
}
else if(op==’/’)
{
result=no1/no2;
printf(“\n Division of both the numbers is %d”, result);
}
else if(op==’%’)
{
result=no1%no2;
printf(“\n Remainder of both numbers is %d”, result);
}
else
printf(“\n Unrecognised Symbol Entered”);
The above program will ask the user to enter two numbers and an operator (mathematical symbol). It will print the result as per the symbol entered. The output will look like
Enter first number 25
Enter second number 5
Enter a symbol like (+,-,*,/) +
Addition of both the numbers is 30
Notice the use of “fflush(stdin)”. It is written here because sometime C does not allow the user to enter integer and character values successively. If you remove the fflush() statement chances are that the program will terminate unexpectedly.
0 comments:
Post a Comment