Thursday 5 March 2015

If Statement in C

The if Statement

A simple condit ion is expressed in the form:

if (condition)
statement;

It starts with the keyword if, followed by a condition (a logical expression) enclosed within parenthesis, followed by the result statement. The resulting statement is executed if the condition is evaluated as TRUE. Note that there is no semicolon (;) after the condition expression.

Consider the following example:

if (marks >50)
printf("You have passed the exam!");

If the value of the variable “marks” is greater than 50, the message “You have passed the exam!” is displayed on the screen; otherwise the statement is skipped and no message is displayed. Program 4.1 illustrates the use of this in a C program.

/* Program-4.1 */

#include <stdio.h>

int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks); //get marks
if (marks >50) // if marks >50 display message
printf("You have passed the exam!");
return 0;
}

Executing Program-4.1 with different inputs will behave as follows:

Case 1: Enter marks: 73
You have passed the exam!
Case 2:Enter marks: 34
Case 3:Enter marks: 50

In the second and third cases, the message “You have passed the exam!” will not be displayed. More than one statement can be executed as a result of the condition by embedding set of statements in a block (between two braces {}).

Example 4.1 – Write a program which accepts a number (an amount of money to be paid by a customer in rupees) entered from the keyboard. If the amount is greater than or equal to 1000 rupees, a 5% discount is given to the customer. Then display the final amount that the customer has to pay. First the program needs to check whether the given amount is greater than or equal to 1000; if it is the case a 5% discount should be given. Then the final amount needs to be displayed. All these are done only if the condition is TRUE. So instructions which compute discount and final amount should be executed as a block. Program 4.2 is an implementation of this.

/* Program-4.2 */
#include <stdio.h>
int main()
{
float amount,final_amount, discount;
printf("Enter amount: ");
scanf("%f", &amount); //get amount
if (amount >= 1000) // if amount >= 1000 give discount
{
discount = amount* 0.05;
final_amount = amount - discount;
printf ("Discount: %.2f", discount);
printf ("\nTotal: %.2f", final_amount);
}
return 0;
}

Executing Program-4.2 with 1250.25 as the keyboard input display the following:

Enter amount: 1250.25
Discount: 62.51
Total: 1187.74

In Program-4.1 if the condition is TRUE, the set of statements inside the block are executed. If the condition is FALSE (if the amount is less than 1000) those statements will not be executed. Example 4.2 – Modify Program-4.2 so that it displays the message “No discount…” if the amount is less than 1000. Another if clause is required to check whether the amount is less than 1000. The second if clause can be used before or after the first (existing) if clause. Program-4.3 below has been modified from

Program-4.2 to address this.

/* Program-4.3 */
#include <stdio.h>
int main()
{
float amount,final_amount, discount;
printf("Enter amount: ");
scanf("%f", &amount); //get amount
if (amount >= 1000) // if amount >= 1000 give discount
{
discount = amount* 0.05;
final_amount = amount - discount;
printf ("Discount: %.2f", discount);
printf ("\nTotal: %.2f", final_amount);
}
if (amount < 1000) // if amount < 1000 no discount
printf ("No discount...");
return 0;
}

Exercise 4.1 – Modify Program-4.1 so that it displays the message “You are failed!”, if marks are less than or equal to 50. In many programs it is required to perform some action when a condition is TRUE and another action when it is FALSE. In such cases, the if clause can be used to check the TRUE condition and act upon it. However it does not act upon the FALSE condition. Therefore the expression resulting the FALSE condition needs to be reorganised. In example 4.2 when we wanted to identify the case where the
amount is not greater than 1000 (the FALSE case) we were checking whether it is less than 1000 (<1000).

The C language allows us to use the if-else structure in such scenarios. You can include both the
cases (TRUE and FALSE) using the if-else structure.



No comments:

Post a Comment