what is the Condition to be given to enter into both if and else loop ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthik baskar
    New Member
    • Sep 2010
    • 16

    what is the Condition to be given to enter into both if and else loop ?

    Code:
    if(condition)
    {
          printf("Hello");
    }
    else
    {
          printf(" World");
    }
    What condition should I give to get the output as Hello World ??
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The real answer to the question is that you do not want to associate yourself with someone asking such a stupid question.

    On a *nix platform you could get both if and else branches to run by calling fork() as the condition although it would be a little random as to which branch ran first.

    Comment

    • karthik baskar
      New Member
      • Sep 2010
      • 16

      #3
      Actually this was a question asked in an interview. But, my friends said that this question has an answer other than what you have mentioned and it works in all versions of C/C++.

      Comment

      • fishlover
        New Member
        • Nov 2010
        • 5

        #4
        I think !printf("hello ") will be worked.
        Code:
        if(!printf("hello"))
        {
              printf("Hello");
        }
        else
        {
              printf(" World");
        }
        And fork() is also available.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Using !printf("hello ") does not actually cause the if cause to run it just makes it look like it might have. For example

          Code:
          if(!printf("hello"))
          {
                printf("Goodbye");
          }
          else
          {
                printf(" World");
          }
          Still prints "HelloWorld " not "GoodbyeWor ld" because the if clause does not actually run.


          BTW I know its and interview question but that doesn't make it a good question just a silly one that anyone with any sense would know better than to ask.

          If you have an option don't work for the people asking this question.

          Comment

          • AHSANKATHIA
            New Member
            • Nov 2010
            • 7

            #6
            if(getche() !== hello)
            {
            printf("Hello") ;
            }
            else
            {
            printf(" World");
            }

            Comment

            • fishlover
              New Member
              • Nov 2010
              • 5

              #7
              Sorry, I think you are not quite catch me.
              if you want to print Goodbye, Then you should use the following code:
              Code:
              if(!printf("Goodbye"))
              { 
                    printf("Goodbye"); 
              } 
              else 
              { 
                    printf(" World"); 
              }
              printf("GoodBye ") would return an int.
              Is that right?
              Thank you!

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Sorry, I think you are not quite catch me.
                No you are not catching me. The question is "what Condition can be use to make the flow of execution enter both the if and else code block".

                This

                Code:
                if(!printf("Goodbye"))
                { 
                      printf("Goodbye"); 
                } 
                else 
                { 
                      printf(" World"); 
                }
                Does not do it, the if code block is not entered the condition just produces output that makes it look like it might have done.

                I demonstrated this by changing the output of the if code block to show that it does not run.

                There is no condition that will make both code blocks run.

                Comment

                Working...