Nested - IF


Nested - IF

 
Nested - IF
 Example:7
 
    /*Quick Demo of NESTED if-else*/

 main()
 {
  int i;
  printf("Enter either 1 or 2");
  scanf("%d",&I);
  if(i==1)
  printf("You would go to Heaven");
  else
  {
   if(i==2)
    printf("Hell was created with
                 you in mind");
   else
    printf("How about mother earth");
  }



 In the  above program an IF-ELSE occurs within the ELSE block of the first IF statement .Similarly , in some other programs an IF-ELSE may occur in the IF block as well. There is no limit on how deeply the IF'S and ELSE'S can be nested.

 Example:8
/* calculation of Gross Salary*/

 main()
 {
  float bs,gs,da,hra;
  Printf("Enter basic salary");
  scanf("%f",&bs);
  if(bs<1500)
  {
   hra=bs*10/100;
   da=bs*90/100;
  }
  else 
  {
   hra=500;
   da=bs*98/100;
  }
  gs=bs+hra+da;
  printf("gross salary=Rs %f",gs);
 }

Example:9

 main()             
 {
  int a=500,b,c;
  if(a>=400)
  b=300;
  c=200;
  printf("\n%d%d",b,c);
 }

OUTPUT: 300 200

Example:10

 main()
 {
  int x=10,y=20;
  if(x==y);
  printf(\n %d%d",x,y);
 }

OUTPUT:  10 20

Explanation:
The semi-colon (;) at the end of IF statement terminates , IF functionally,so x and y values are get printed.

Example:11

  main()
  {
   int x=3,y=5;
   if(x==3)
   printf("%d",x);
   else;
   printf("%d",y);
  }

OUTPUT:       3

Example:12

 main()
 {
  char str1[]="Hello";
  char str2 []="Hello";
  if(str1==str2)
  printf("Equal");
  else
  printf("Unequal");
 }

OUTPUT:         Unequal

Explanation:
         By using "==" operator we cann't campare two strings,we must use strcmp() to compare  strings.So the else is executed.

Example:13

 main() 
 {
  int i=10,j=40;
  if((j-I)%10)
  printf("Man sees your actions. . . ");
  else
  printf("God sees your motives. . . ");
 }

OUTPUT:      God sees your motives. . .

Explanation: 
       This is quite straight-forward.(j-I)%10,on substituting the values of j and I, become (40-10)%10.that is 30%10, which gives the remainder as 0. thus the condition would now become,Since 0 is treated as falsely in c,the control reaches the second printf() which prints output.

Example:14

 main()
 {
  float a=0.7;
  if(a<0.7)
  printf("Stoned");
  else
  printf("Averaged");
 }

OUTPUT:
          Stoned.

Explanation:
The output is very surprising! 0.7 is never less than 0.7, so the condition  should evaluate to FALSE.But that doesn't  happen, Reason is ,when 0.7 is stored in a,due to precision considerations, it is  stored as something less than 0.7.naturally,when value stored in a is compared with 0.7,
the condition evaluates to TRUE and Stoned gets printed.

Two get rid of the problem there are 2 solutions.

(a) declare A as a long Double as
     shown below:

      long double a;
(b) Typecast 0.7 to a Float while
    comparing as shown below:

      if(a<(float)0.7)

Typecasting means converting to the specified data type.

Example:15

           main()
           {
            int i=400*400/400;
            if(i==400)
             printf("Hai");
            else
             printf("Hello");
           }

OUTPUT:    Hello.


Explanation:               
        The answer is Yes. Thus, the statement int i=400*400/400 is quite alright.But on evaluating the expression it doesn't turnout to be 400. Reason is,when 400*400  is done we don't get 160000 because 160000 falls outside the integer range (-32768 to +32767).Whenever a number exceeds 32767,it goes to the negative side and picks up the appropriate number.
  So if results FALSE, that's why the result.

 Example:16

  main()
  {
   int x=3;
   float y=3.0;
   if(x==y)
   printf("\n x and y are equal");
   else
   printf("\n x and y are not equal");
  }

OUTPUT:           x and y are not equal

Explanation:
       Because, here data types of two
variables are different, so,else part
get executed.

Example:17 

 main()
 {
  int i=65;
  char j='A';
  if(i==j)
  printf("C is Wow");
  else
  printf("C is a headache");
 }

OUTPUT:         C is Wow

Explanation:
      Here , the ASCI value of character 'A'is 65. While comparing it takes A as
65, that's why if part is executed.

Example:18

 main()
 {
  float a=12.25, b=12.52;
  if(a=b)
  printf(\n a and b are equal");
 }

OUTPUT:     a and b are equal

Explanation:
     Here in IF we are not comparing the values, but it is an assignment, So, IF value is non-zero,so the output results.

Example:19

 main()
 {
  int x=30,y=40;
  if(x==y)
  printf("\n x is equal to y");
  else if(x>y)
  printf("\n x is greater than y");
  else if(x< y)
  printf("\n x is less than y");
 }

OUTPUT:         x is less than y

Explanation:
         It  compares in first IF whether x and y are equal or not,the comparison results FALSE,so the control comes to next IF it also fails.Finally the condition satisfied, thus the output.

Example:20

 main()
 {
  int x=10;
  if(x>=2) then
  printf("\n %d",x);
 }

OUTPUT:     10

Explanation:
        It checks for the condition, it results TRUE then printf() get executed.

No comments:

Post a Comment