How to execute statements in both if and else block

Actually if-else is a conditional statement, and its syntax is
   if(condition)
     {
        block of code;    //which executes if condition is true
     }
   else
     {
        block of code;    //which executes if condition is false
      }

But we can also execute codes in both if and else blocks
one way to execute both if and else blocks is

#include<stdio.h>
main()
  {
    int a=1;
     if(a==1)     
                  \\ here a==1 is a condition which is true because a is initialized
                                               with 1
      {
        printf("am if\n");
      \\ this is the block of code to be executed as condition is true
        goto z;
      }
     else
      {
       z:
                                \\ this is else block, goto statement in if block
                                                 transfers control to z in else block
        printf("am else");
      }
    getch();
  }


Output of above program is:

am if             \\ printf  in if block prints this statement
am else         \\ printf in else block prints this statement

     Lets try this frenzzz.............
Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment