Strange if condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerico
    New Member
    • Sep 2006
    • 39

    Strange if condition

    Hi all.I have a problem.

    if (some condition)
    printf("hello") ;
    else
    printf("world") ;

    The "some condition" should be such that both "hello" and "world" are printed.Thanks for any help.

    Regards,
    Jerico
  • sreema
    New Member
    • Sep 2006
    • 3

    #2
    i think "any condition" cannot be true and false at same time.



    Originally posted by jerico
    Hi all.I have a problem.

    if (some condition)
    printf("hello") ;
    else
    printf("world") ;

    The "some condition" should be such that both "hello" and "world" are printed.Thanks for any help.

    Regards,
    Jerico

    Comment

    • pukur123
      New Member
      • Sep 2006
      • 61

      #3
      It is not possible in C. Then what is the need of if statement. Directly execute all the statements irrespective of the conditions. So C has switch case to handle this type of situations. Go through the switch case.

      Comment

      • sreema
        New Member
        • Sep 2006
        • 3

        #4
        wht i meant was in C both if and else cannot execute sequentially. Definetly can be done using switch.



        QUOTE=pukur123]It is not possible in C. Then what is the need of if statement. Directly execute all the statements irrespective of the conditions. So C has switch case to handle this type of situations. Go through the switch case.[/QUOTE]

        Comment

        • jerico
          New Member
          • Sep 2006
          • 39

          #5
          I also thought so.But then I remembered something called fork().If I use

          if(!fork())
          printf("hello") ;
          else
          printf("world") ;

          then both "hello" and "world" are printed. Can there be any other solution?

          Regards,
          Jerico

          Comment

          • nikhilraj
            New Member
            • Sep 2006
            • 11

            #6
            Originally posted by jerico
            I also thought so.But then I remembered something called fork().If I use

            if(!fork())
            printf("hello") ;
            else
            printf("world") ;

            then both "hello" and "world" are printed. Can there be any other solution?

            Regards,
            Jerico
            hey function which u used fork() is used in unix to create a child process.So here two threads of controls are generated one by parent and another by child.When fork is executed a child process is created.The parent process returns the processid of the child and the child process returns 0 .so one printf is executed by parent and another one by child .

            Comment

            • KartikVlsi
              New Member
              • Jan 2007
              • 1

              #7
              There is always a solution to a problem.. It depends on how u think.. Instead of focussing on the problem, focus on the solution.. the question was not to try to figure out whether c supports the control entering if and else.. it just said the output needs to be hello world.. well answer is simple..


              if(!printf("Hel lo"))
              printf("Hello") ;
              else
              printf("World") ;

              The conditions printf("Hello") will print hello and value retured is obviously true which by using ! can be made false so that it can then print world...

              "Think, It may be a new experience"!!!

              Comment

              • willakawill
                Top Contributor
                • Oct 2006
                • 1646

                #8
                Originally posted by KartikVlsi
                There is always a solution to a problem.. It depends on how u think.. Instead of focussing on the problem, focus on the solution.. the question was not to try to figure out whether c supports the control entering if and else.. it just said the output needs to be hello world.. well answer is simple..


                if(!printf("Hel lo"))
                printf("Hello") ;
                else
                printf("World") ;

                The conditions printf("Hello") will print hello and value retured is obviously true which by using ! can be made false so that it can then print world...

                "Think, It may be a new experience"!!!
                Nice answer :)

                Comment

                • vsagarmca
                  New Member
                  • Jul 2007
                  • 1

                  #9
                  Very good question and answer. The more interesting way is presentation. If you have this kind of nice question please email me at vsagarmca@gmail .com
                  Any help would be appreciated.

                  Comment

                  • ssleepwalker
                    New Member
                    • Mar 2008
                    • 2

                    #10
                    Perhaps recursion might interest you..

                    Compliments of sleep+walker programming:1 Yahoo! Chat.

                    /* begin ifelse.c */

                    #include <stdio.h>

                    int main(void)
                    {
                    static int i;

                    if(!i++)
                    {
                    printf("Hello ");
                    main();
                    }
                    else
                    printf("World!" );

                    return 0;
                    }

                    /* end ifelse.c */

                    C:\MinGW>gcc -Wall -pedantic -std=c99 -O2 -o ifelse.exe ifelse.c
                    C:\MinGW>ifelse
                    Hello World!
                    C:\MinGW>

                    Comment

                    • ssleepwalker
                      New Member
                      • Mar 2008
                      • 2

                      #11
                      I've considered KartikVlsi's idea and I'm quite convinced that unless by an act
                      of God the if statement will NEVER execute. My proposal eliminates writing code that will never execute, god willing.

                      I propose:

                      /* begin file.c */

                      #include <stdio.h>

                      int main(void)
                      {

                      if(!printf("Hel lo "))
                      {
                      }
                      else
                      printf("World!" );

                      return 0;
                      }

                      /* end file.c */


                      C:\MinGW>gcc -Wall -pedantic -std=c99 -O2 -o file.exe file.c

                      C:\MinGW>file
                      Hello World!
                      C:\MinGW>

                      Comment

                      Working...