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

Dani AI

Generated

Short answer: an if/else pair can execute only one branch. You cannot have the code that is textually inside the if block and the code that is textually inside the else block both run for the same evaluation of that if. As and pointed out, the language semantics choose exactly one path.

That said, several replies on this thread show tricks and workarounds. The trick demonstrated by prints HELLO while evaluating the condition (because printf returns the number of characters written, a non‑zero value), then the else body runs and prints WORLD. This produces the visible string "HELLO WORLD", but note two important things: 1) HELLO was printed during condition evaluation, not from the if block itself; and 2) relying on side effects inside conditions is fragile and hard to read. had the right intuition but it’s clearer to say printf returns a count (positive on success, negative on error), which is what the if tests.

Better approaches are to make your intent explicit. Examples:

#include <stdio.h>

int main(void) {
    printf("HELLO ");
    printf("WORLD");
    return 0;
}

or, if HELLO should be conditional but WORLD must always print:

#include <stdio.h>

int main(void) {
    if (condition) {
        printf("HELLO ");
    }
    printf("WORLD");
    return 0;
}

or, if different conditions control each word:

#include <stdio.h>

int main(void) {
    if (condA) printf("HELLO ");
    if (condB) printf("WORLD");
    return 0;
}

Avoid goto hacks and side effects in if tests; prefer clear structure, int main(void), and standard I/O functions so the behavior is obvious to anyone reading the code.

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.