Monday, December 12, 2011

IF ELSE STATEMENTS

Introduction

C program cannot think ! but we can make it think to some extend with the help of if, else statement. Technically this both are keywords but holds a very important part in programming. With out if else no complicated program can be made easily. If statement gives power to a program to take decision, it instruct the compiler which set of instruction has to be executed. For that we have to know very few important things like 

Relational Operator
  •  = =   Equal to
  • ! =     Not Equal to
  •  <      Less than
  •  >      Greater than
  •  <=    Less than or equal to
  •  >=   Greater than or equal to
We can also use arithmetic expression in the if statement. A non-zero value is always true where as 0 is considered as false in C.
Syntax for if statement is :

if(a= =b)
{
    statement 1;
    statement 2;
}

For more than one line statement {} braces are needed.
if(Variable Name Relational Operator Variable Name)

x = y means value of y goes to x.
x= =y means value of x is equal to value of y.

For example :

#include"stdio.h"
main()
{
    int x=5, y=6;

        if(x == y)

          printf("Both are Equal Numbers\n");

        else

          printf("Both are Not Equal Numbers\n");
}

Output will be : Both are Not Equal Numbers

x!=y means value of x is not equal to value of y.
For example :

#include"stdio.h"
main()
{
    int x=6, y=7;

        if(x != y)

          printf("Not Equal Numbers\n");

        else

          printf("Equal Numbers\n");
}

Output will be : Not Equal Numbers

x<y means value of x is lesser than value of y.
For example :

#include"stdio.h"
main()
{
    int x=10, y=7;

        if(x < y)

          printf("X is Less Than Y\n");

        else

          printf("Y is Less Than X\n");
}

Output will be : Y is Less Than X

x>y means value of x is greater than value of y.
For example :

#include"stdio.h"
main()
{
    int x=10, y=7;

        if(x > y)

          printf("X is Greater Than Y\n");

        else

          printf("Y is Greater Than X\n");
}

Output will be : X is Greater Than Y

x<=y means value of x is lesser than or equal to value of y.
For example :

#include"stdio.h"
main()
{
    int x=10, y=7;

        if(x <= y)

          printf("X is Lesser or Equal to Y\n");

        else

          printf("X is Greater Than Y\n");
}

Output will be : X is Greater Than Y

x>=y means value of x is greater than or equal to value of y.
For example :

#include"stdio.h"
main()
{
    int x=10, y=7;

        if(x >= y)

          printf("X is Greater or Equal to Y\n");

        else

          printf("Y is Greater Than X\n");
}

Output will be : X is Greater or Equal to Y

Arithmetic Expression

Inside if(....... ) Arithmetic Expression can be done.
For example :

#include"stdio.h"
main()
{
           int x=2, y=3, z=10;

        if((x+2-2)*10 > (y*z)/2)
           printf("X is Greater \n");

        else
           printf("Y is Greater \n");
}

Output will be : X is Greater

Which Entered Number is Greater :

#include"stdio.h"
main()
{
    int a, b;
    printf("Enter First Number : ");
    scanf("%d",&a);
    printf("Enter Second Number : ");
    scanf("%d",&b);

        if(a>b)
            printf("First Number is Greater\n");
        else
            printf("Second Number is Greater\n");
}

This program will work right but if we feed two same number then second one will come which is not right. To make it right we can make a few add of statement like

#include"stdio.h"
main()
{
    int a, b;

    printf("Enter First Number : ");
    scanf("%d",&a);
    printf("Enter Second Number : ");
    scanf("%d",&b);

        if(a>b)
            printf("First Number is Greater\n");

        else
        {
            if(a<b)
                printf("Second Number is Greater\n");
            else
                printf("Both Numbers are Same\n");
        }
}

Or it can be written in the following way :

#include"stdio.h"
main()
{
    int a, b;

    printf("Enter First Number : ");
    scanf("%d",&a);
    printf("Enter Second Number : ");
    scanf("%d",&b);

        if(a>b)
            printf("First Number is Greater\n");
        else if(a<b)
            printf("Second Number is Greater\n");
        else
            printf("Both Numbers are Same\n");
}

Is the Entered Number Even or Odd :

#include"stdio.h"
main()
{
    int a;
    printf("Enter a number : ");
    scanf("%d",&a);

        if(a%2==0)
            printf("This Number is Even\n");
        else
            printf("This Number is Odd\n");
}

Nested if else

A set or more if else written under a if or else block is called Nested if else as shown below :

Estimate a Person's Age :

#include"stdio.h"
main()
{
    int a;
    printf("Enter the Age Please : ");
    scanf("%d",&a);

    if(a<=18)
            printf("Child \n");
    else
    {
        if(a<=50)
            printf("Adult \n");
        else
            printf("Old \n");
    }
}

Below 1000 - No Discount
Between 1001 to 2000 - 20% Discount
Above 2001 - 30% Discount


#include"stdio.h"
main()
{
    int amount;
    float discount;

    printf("Please Enter the Total Amount : ");
    scanf("%d",&amount);

    if(amount<=1000)
    {
        printf("Sorry No Discount \n");
    }   //  End of first if
    else
    {
         if(amount<=2000)
         {
               discount =amount*20/100;
               printf("You are getting Discount of $ %f \n",discount);
          }   //  End of second if
         else
         {
                discount =amount*30/100;
                printf("You are getting Discount of $ %f \n",discount);
          }   //  End of second else
     }   //  End of first else
}   // End of main

else if Clause

Syntax for else if :

      if(condition 1)
        {
            statement 1;
            statement 2;
        }
      else if(condition 2)
        {
            statement 1;
            statement 2;
        }
      else if(condition 3)
        {
            statement 1;
            statement 2;
        }
      else if(condition 4)
        {
            statement 1;
            statement 2;
        }
      else
        {
            statement 1;
            statement 2;
        }

Same work in a different style.
For example :

#include"stdio.h"
main()
{
    int num;

    printf("Just Enter a Number From 1 to 5 : ");
    scanf("%d",&num);
   
    if(num==1)
    {
        printf("You Entered : %d \n",num);
        printf("You Are In First Block \n");
    }
  else if(num==2)
    {
        printf("You Entered : %d \n",num);
        printf("You Are In Second Block \n");
    }
  else if(num==3)
    {
        printf("You Entered : %d \n",num);
        printf("You Are In Third Block \n");
    }
  else if(num==4)
    {
        printf("You Entered : %d \n",num);
        printf("You Are In Forth Block \n");
    }
  else
    {
        printf("You Entered : %d \n",num);
        printf("This is My Favorite ! \n");
    }
}

Logical Operator
  •    &&     AND
  •      ||       OR
  •      !       NOT
One more important thing is Logical Operator. With the help of Logical Operators we can put two or more condition inside if like :

AND (All conditions have to be true), the syntax is :

if(num>=1 && num<=50)

Example :

#include"stdio.h"
main()
{
    int num;

    printf("Please Enter a Number : ");
    scanf("%d",&num);

    if(num<1)
        printf("Number You Entered Is Below 1 \n");
    else
    {

    if(num>=1 && num<=50)    // Here both the conditions have to be true.

        printf("Number You Entered Is From 1 to 50 \n");
    else
        printf("Number You Entered Is Above 50 \n");
    }
}

OR (Any one condition has to be true), the syntax is :

if(num==1 || num==10 || num==100)

Example :

#include"stdio.h"
main()
{
    int num;

    printf("Please Enter a Number : ");
    scanf("%d",&num);

    if(num<1)
        printf("Number You Entered Is Below 1 \n");
    else
    {

    if(num==1 || num==10 || num==100)    // Here any one condition has to be true.

        printf("Number You Entered Is Either 1, 10 or 100 \n");
    else
        printf("Number You Entered Is Unknown to Us \n");
    }
}

NOT (Reverse the condition), the syntax is :

if (!(num<=10))

Example :

#include"stdio.h"
main()
{
    int num;

    printf("Please Enter a Number : ");
    scanf("%d",&num);

    if(!(num<=10))    // Here the condition gets reversed.

        printf("Number You Entered Is Below 10 \n");
    else
        printf("Number You Entered Is Above 10 \n");
}

if(!(num<=10)) is same as if(num>10)

Conditional Operator
  • ?
  • :
Syntax is :

Condition ? Result 1 : Result 2

if Condition is true then Result 1 will be executed
if Condition is false then Result 2 will be executed
For example :

#include"stdio.h"
main()
{
    int num;

    printf("How Much Can You Pay : ");
    scanf("%d",&num);

    (num>=1 && num<=100 ? printf("Sorry I am Busy \n") : printf("You are Welcome \n"));
}

num>=1 && num<=100 if this condition is true then
printf("Sorry I am Busy \n") will be executed
otherwise printf("You are Welcome \n") will be executed.

#include"stdio.h"
main()
{
    char ch = 'y';

    printf("Am I Good ? (y/n) Enter y or n : ");
    scanf("%c",&ch);

    (ch=='y' ? printf("You Are My Friend \n") : printf("You Could Be My Friend \n"));
}

Conditional Operators can also be nested and arithmetic expression
can also be done inside.

No comments:

Post a Comment

Followers