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

rinkuba.gohil commented: how it possible? +0

Recommended Answers

All 9 Replies

#include<stdio.h>
#include<conio.h>
main()
{
      if (! printf("HELLO"))
      printf("HELLO");
      
      else
      printf(" WORLD");
getch();
}
commented: That's horrible.. -2

I need the explanation for this

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");
}
}
commented: Thread is 2 years old... -1

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.

but what's the problem in above post trick ? can u please xplain me ?

#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.

ya this wat ve need it its a good solution for the problm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.