Computer Concept & Programming In C - Unit III - 1




Q.1 What is the use of if (condition) in C programming.               (AKTU. 2008 - 09)
Ans. Conditional Program Execution: -
Applying if and switch statement: -
A simple condition is expressed in the form: 
if (condition)
Statement;
It starts with the keyword if. Followed by a condition (a relational or a logical expression) enclosed in parenthesis followed by the result statement. The statement is executed if the condition is evaluated as TRUE, for example
if (a < 100)
printf (“\n The variable a is less than 100”);
If the value of a is less than 100 then the message is displayed on the screen; otherwise the statement is skipped. Example in the following program a credit card limit is tested for a certain purchase. The value of the variable “amount” is received from the keyboard, then tested using the if statement. If the condition is TRUE (i.e. the amount is less than or equal to 1000) the massage your charge is accepted is displayed. If the condition is FALSE, the program ends without response.
# include <stdio.h>
# include (conio.h>
void main ( )
{
float amount;
clrscr ( );
print f(“\n Enter the amount”);
scan f(“%f ”, & amount);
if (amount < = 1000)
printf (“\ n your charge is accepted”);
getch ( );
}
In the following example another simple condition is added to deal with the other case (i.e. the “amount” is greater than 1000). The message “The amount exceeds your credit limit” appears on the screen in this case.
# include <stdio.h>
# include <conio.h>
void main ( )
{
float amount;
clrscr ( );
printf (“\ n Enter the amount”);
scanf (“% f ”, & amount);
if (amount < = 1000)
print f(“\n your charge is accepted);
if (amount > 1000)
printf (“\ n The amount exceeds your credit limit”);
getch ( );
}
you can use more than one statement as a result of one condition by embedding the statements in a block (between braces {}). Look at this example below.
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
void main ( )
{
float amount;
clrscr ( );
printf (“\ n Enter the amount”);
scanf (“% f ”, & amount);
if (amount < = 1000)
{
printf (“\ n Your charge is accepted”);
printf (“\ n your price plus taxes is $%.2f ”, (amount*1.05));
printf (“\ n Thank you for using C credit card.”);
exit (0);
}
printf (“\ n The amount exceeds your credit limit”);
getch ( );
}
In this example, if the condition is TRUE, the statement inside the block are executed upto the statement exit (0). The function exit, which is found in the header file <stdlib.h>, is used to terminate the execution of the program at this point. In this case, you do not need the second condition, because if the condition is FALSE the control will be transferred to the last statement, skipping the if block. If you enter an amount like 150, the output will be:
Enter the amount 150
Your charge is accepted.
Your price plus taxes is $ 157.50
Thank you for using C credit card.
If you enter an amount like 4000, then the output will be 
Enter the amount: 4000
The amount exceeds your credit limit.
Notice that there is no restriction on the position of the braces. You may put them anywhere around the block, but arranging the braces in this specific manner as shown is a convention for the convenience of the reader. It will help you read your programs, especially when they get large and contain many nested blocks.
Important: You may write an if statement, by mistake like this 
if (a > b);
printf (“\ n The charge is accepted”);
This extra semicolon after the condition will end the statement at this point, and the next statement will always be executed. So, watch your semicolons.