Thursday, 29 May 2014

if...else Statement in C

The if, if...else and nested if...else statement are used to make one-time decisions in C Programming, that is, to execute some code/s and ignore some code/s depending upon the test expression.

Simple  if statement

We often need to be able to choose which set of instructions are obeyed according to a condition. For example, if you're keeping a total(sum) and you need to display the message 'OK' if the value is greater than zero you would need to write something like:

if (total>O) printf("OK");

This is perfectly reasonable English, if somewhat terse, but it is also perfectly good C. The if statement allows you to evaluate a condition or expression and only carry out the statement, or compound statement, that follows if the condition or expression is true. In other words the printf will only be obeyed if the condition total> O is true.

Syntax:
The general form of a simple if statement is,
if( expression )
{
 statement-inside;
}
statement-outside ;

The if statement checks whether the 'expression' inside parenthesis ( ) is true or not. If the 'expression' is true, statement-inside the body of if statement is executed but if test is false, statement/s inside body of if is ignored and only 'statement-outside'  is executed. .
#include<stdio.h>
int main()
{
        int x=12;
        int y=5;
        if(x<y)
        {
                printf("x is less than y"); 
        }
       return 0; 
 }
Output: 
nothing will be printed becoz if statement gives false and it directly runs return 0.
if...else statement

The if...else statement is used if the programmer wants to execute some statement/s when the test condition or expression is true and execute some other statement/s if the test condition or expression is false.

This is another possibility is that you might want to select one of two possible statements - one to be obeyed when the condition is true and one to be obeyed when the condition is false. You can do this using the

if (condition) statement1;
else statement2;

 In this case statement1 is carried out if the condition is true and statement2 if the condition is false.

Syntax:
if( expression )
{
 statement-block1;
}
else
{
 statement-block2 ;
}

If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped and
'statementblock2' is executed.

#include<stdio.h>
int main()
{
        int x=12;
        int y=5;
        if(x<y)
        {
                 printf("x is less than y"); 
        }
        else
        {
                 printf("x is greater than y"); 
        }
        return 0; 
 } 
Output: 
'x is greater than y' 


Nested if...else statement

The nested if...else statement is used when program requires more than one test expression.
if( expression )
{
        if( expression1 )
        {
            statement-block1;
        }
        else
        {
            statement-block 2;
        }
}
else
{
     statement-block 3;
}

The nested if...else statement has more than one 'expression'. If the first 'expression' is true, it executes the code inside the braces{ } just below it. But if the first 'expression' is false, it  else is executed and the control of program jumps below the nested if...else.
In above , if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression 1' . If the 'expression 1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.
The ANSI standard specifies that 15 levels of nesting may be continued.
#include< stdio.h> 
#include< conio.h> 
void main( ) { 
     int a,b,c; 
     clrscr(); 
     printf("enter 3 number"); 
     scanf("%d%d%d",&a,&b,&c); 
     if(a>b)
     { 
         if( a > c) 
         { 
             printf("a is greatest"); 
         } 
         else  
         { 
             printf("c is greatest"); 
         } 
     } 
     else 
     { 
         if( b> c) 
         { 
             printf("b is greatest"); 
         } 
         else 
         { 
            printf("c is greatest"); 
         } 
     } 
getch(); 
}

if-else ladder

The general form of else-if ladder is:

if(expression 1) 
{ 
     statement-block1; 
} 
else if(expression 2) 
{ 
     statement-block2; 
} 
else if(expression 3 ) 
{ 
     statement-block3; 
} 
else  
default-statement
The expression is tested from the top(of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.

#include< stdio.h> 
#include< conio.h> 
void main( ) 
{ 
    int a; 
    printf("enter a number"); 
    scanf("%d",&a); 
    if( a%5==0 && a%8==0) 
    {  
         printf("divisible by both 5 and 8"); 
    }
    else if( a%8==0 ) 
    {  
        printf("divisible by 8"); 
    } 
    else if(a%5==0) 
    {  
        printf("divisible by 5"); 
    } 
    else  
    {  
        printf("divisible by none"); 
    } 
    getch(); 
}
Points to Remember

1. In if statement, a single statement can be included without enclosing it into curly braces { }
int a = 5; 
if(a > 4)  
printf("success");

No curly braces are required in the above case, but if we have more than one statement inside if condition, then we must enclose them inside curly braces.

2. == must be used for comparison in the expression of if condition, if you use = the expression will always return true, because it performs assignment not comparison.

3. Other than 0(zero), all other values are considered as true.
if(27) 
printf("hello");

In above example, hello will be printed.

0 comments :

Post a Comment