Sunday, September 16, 2012

CONDITIONAL AND SWITCH


            In GCC, conditional and switch programming is as like Windows (TC).

CONDITIONAL PROGRAMMING
            Conditional program has structure in Windows (TC) as

if(condition1)
{
            statements;
}
else if(conditon2)
{
            statements;
}
else
{
            statements;
}

This is same structure for GCC.

SWITCH-CASE PROGRAMMING
            switch-case programming has structure in Windows (TC) as

switch(choice)
{
            case 1: statements;
                        break;
            case 2: statements;
                        break;
            case 3: statements;
                        break;
            .
            .
            .
}

This is also same structure for GCC.

Let’s check some examples of these…
  1)    CONDITONAL PROGRAM

#include<stdio.h>
#include<string.h>
int main()
{
      float age=0.0;
      char concetion[10]="NULL";

      printf("\n\n Concetion Rate Board");
      printf("\n\n Enter your age : ");
      scanf("%f",&age);
      if(age<=18)
                  strcpy(concetion,"FULL");
      else if(age>=60)
                  strcpy(concetion,"HALF");
      else
                  strcpy(concetion,"NO");
      printf("\n You have %s concetion",concetion);
      printf("\n");
return 0;
}

  2)    switch-case program

#include<stdio.h>
#include<stdlib.h>
int main()
{
            int choise=0;
            float amount=0.0;
            printf("\n Transaction Unit");
            printf("\n 1-Deposite        \
                        \n 2-widrowal \
                        \n 3-exit        \
                        \n Enter your choice (1/2) : ");
            scanf("%d",&choise);
            switch(choise)
            {
            case 1: printf("\n\n Enter amount you want to Deposite : ");
                        scanf("%f",&amount);
                        printf(" Your %.2f amount is Deposited !!!",amount);
                        break;

            case 2: printf("\n\n Enter amount you want to Widraw : ");
                        scanf("%f",&amount);
                        printf(" Your %.2f amount is Widrawed !!!",amount);
                        break;

            case 3: printf("\n\n You have entered option to exit.");
                        exit(0);

            default: printf("\n\n You have entered incorrect option !!!");

            }
            printf("\n");
return 0;
}