please tell me what is the condition to use here....

if(CONDITION)
printf("HELLO");
else
printf("WORLD");

ok frnds now i want the output as HELLO WORLD

Edited 4 Years Ago by WaltP: Old thread with worthless resurrection

Comments
how it possible?
#include<stdio.h>
#include<conio.h>
main()
{
      if (! printf("HELLO"))
      printf("HELLO");
      
      else
      printf(" WORLD");
getch();
}

Edited 6 Years Ago by Nick Evan: Added code-tags

Comments
That's horrible..

When you are in the if loop the printf statement prints the Hello and returns true. So !true is false so the printf under if is not printed and so it goes to the else part and prints the world part. Hence youget HelloWorld as output

void main()
{
int check =1;
if(check==1)
{
 printf("Hello");
 goto condition1;
}
else
{
 condition1: 
 printf("World");
}
}
Comments
Thread is 2 years old...

hi dude the if else is used to tell yes or no not to tell yes & no therefore the output will be hello or world its depend on the condition k ,hw can any one tell that the output is hello world without the condtion ,plz mention the the ur promblem with clear meaning

is it possible to print both statements of else

If you are using the if-else clause as intended, either the code in the "if" part will execute or the code in the "else" part will execute, but not both.

Now if the question is "Can I print 'Hello World'" where the 'Hello' part is in the 'if' brackets and the 'World' part is in the 'else' brackets", yes you can.

int i;
for(i = 0; i < 2; i++)
{
    if(i == 0)
    {
        printf("Hello ");
    }
    else
    {
        printf("World");
    }
}

Two iterations through the for-loop. On the first, "Hello " is displayed. On the second, "World" is displayed. Or use goto statements, as mentioned. The bigger question is why are you trying to do this?

Just noticed the post dates on this thread.

Edited 4 Years Ago by VernonDozier

#include<stdio.h>
#include<conio.h>
main()
{
      if (! printf("HELLO"))
      printf("THIS DOES NOT PRINT");

      else
      printf(" WORLD");
getch();
}
  • conio.h and getch are not standard. http://www.daniweb.com/software-development/cpp/threads/11811/replacement-of-getch
  • main needs to return an integer
  • The solution "cheats" because the "if" block does not print "Hello" as the OP clearly wanted. "HELLO" prints in the conditional test, not the block of code that is executed when the if statement is true. What really is happening is below.

    if(printf("HELLO"))
    {
    printf(" World");
    }

There is no "else" statement in there. The OP wanted code where the "if" block prints "Hello" and the "else" block prints "World". The earlier snippet doesn't do that.

Edited 4 Years Ago by VernonDozier

This article has been dead for over six months. Start a new discussion instead.