Translate

Labels

Monday 31 December 2012

CONDITIONAL STATEMENTS ( IF )

IF STATEMENT

In C, the conditional statements rely on idea of boolean expressions.The if statements controls conditional branching.The boolean expression or in other words a relation expression will yield true or false value.In C, a value other than 0 is true and 0 is considered as false.The body of an if statement is excuted if the value of the expression is true i.e..,non_zero.There are two forms of if statement.

if (relation expression)
statement;

if(relation expression)
statement 1;
else
statement 2;

 In the first form of, if relational expression is true (non zero), the statement is executed. If the expression is false, the statement is ignored.In the second form ,which uses else, statement 2 is executed if the expression is false.With both forms,control then passes from the statement to the next statement in the program.
Here is a C program demonstrating a simple if statement:

#include<stdio.h>
void main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x>0)
printf("The value is positive\n");
}

This program accepts a number from user.It then tests the number using a conditional expression of the if statement to see if it is greater than 0.If it is,the program prints a message.Otherwise,the program is silent i.e..,there will be no output.The (x>0) portion of the program is the boolean expression.C evaluates this expression to decide whether or not to print the message.If the boolean expression evaluates to true, then C executes the single line immediately following the if statement (or a block of lines within braces immediately following the if statement ) .If the boolean expression is false, then C skips the line or block of lines immediately following the if statement.consider another example:

#include<stdio.h>
void main()
{
int x;
printf("Enter an integer:");
scanf("%d",&x);
if(x<0)
printf("The value is negative\n");
else if(x=0)
printf("The value is zero\n");
else
printf("The value is positive\n");


in this example the if_else_if construct is used which we can call as a nested if_else structure.The nested if_else structure is used to perform some operations based on choices.Consider a simple arithmetic problem in which add, subtract, multiply and divide operations are to be carried out depending on the choice.The options may be displayed.The program can be written in two ways:
using simple if:

#include<stdio.h>
void main()
{
int a,b,c,choice;
scanf("%d%d",&a,&b);       /*b is not zero*/
printf("1.addition\n");           /* option 1 */
printf("2.subtraction\n");      /* option 2 */  
printf("3.multiplication\n");   /* option 3 */
printf("4.division\n");           /* option 4 */
scanf("%d",&choice);
if(choice= =1)
c=a+b;
if(choice= =2)
c=a-b;
if(choice= =3)
c=a*b;
if(choice= =4)
c=a/b;
printf("The result =%d\n",c);
}  

The above program estimates the value based on the choice and prints the result.The number of comparisons made in the program is 4 irrespective of any choice.It performs comparisons unnecessarily.When (choice==1) is true, it is not necessary to evaluate the other conditions and may be skipped. The use of nested if_else construct will solve this problem.
using nested if_else construct:

  #include<stdio.h>
void main()
{
int a,b,c,choice;
scanf("%d%d",&a,&b);       /*b is not zero*/
printf("1.addition\n");           /* option 1 */
printf("2.subtraction\n");      /* option 2 */  
printf("3.multiplication\n");   /* option 3 */
printf("4.division\n");           /* option 4 */
scanf("%d",&choice);
if(choice= =1)
c=a+b;
else
if(choice= =2)
c=a-b;
else
if(choice= =3)
c=a*b;
else
if(choice= =4)                  /* comparison is optional */
c=a/b;
printf("The result =%d\n",c);
}  

 The above program uses nested if_else construct.if the first condition is true, that is ,(choice = = 1) is true the statement c=a+b; is executed and the statement following the else is skipped.Therefore, if the first condition is true, only one comparison is made and all the other comparison are skipped.Only when the first condition fails the program continues to compare the second condition and it goes on similarly.This program works faster than the previous program.but if choice is 4, both programs have to do four comparisons.

No comments: