English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can you gimme what 'condition' in the if statement will make the program display 'Hello World'.............does such a condition exiat at all??

if('condition')
printf("Hello";
else
printf("World");

2006-10-14 18:25:06 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

Supposing that printf( ) returns 0 when it succeeds, you could try:

if (0 != printf("Hello"))
printf("Hello");
else printf(" World");

This is called "Lateral thinking" or sometimes "how to write C code which will get you sacked from your job".

2006-10-15 08:27:05 · answer #1 · answered by Anonymous · 0 0

The if statement is looking for an integer value. Any non-zero value will result in Hello being printed. So, you could have:

int condition = 0;

for (condition = 0; condition <= 1; condtion ++){
if (condition)
printf("hello ");
else
printf("world\n");
}

This sets the integer variable 'condition' and the loop executes twice, first printing hello, and then world. A space was added after the word hello, and a carriage return after world.

Hope this helps.

--Dee

2006-10-14 18:42:13 · answer #2 · answered by Deirdre H 7 · 0 0

Assuming you typed the code snippet correctly, no you cannot make it display "Hello World" because the value of condition is never changed. Condition is either true in which case it displays hello or it is false and it displays World. Condition is boolean (true or false, 0 or non zero)

2006-10-14 18:34:37 · answer #3 · answered by mdigitale 7 · 0 0

There's no condition that will meet both the if & else portions of the statement. If the condition is true, you get Hello; if false, World. There's no third option.

2006-10-14 18:33:34 · answer #4 · answered by Flyboy 6 · 0 0

There isn't only one condition. In fact, any condition you put there could make the program display any message you want, as long as the evaluation of the sentence gives you a TRUE as a result value.

2006-10-14 18:28:13 · answer #5 · answered by Horumaket 2 · 0 2

If you're asking if there's a condition that will cause it to execute both the if and the else clause, no. The whole point is that it will do one or the other.

The code you give won't do anything though, due to syntax errors.

2006-10-14 18:33:18 · answer #6 · answered by Ken H 4 · 1 0

fedest.com, questions and answers