473,237 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

Strange if condition

39
Hi all.I have a problem.

if (some condition)
printf("hello");
else
printf("world");

The "some condition" should be such that both "hello" and "world" are printed.Thanks for any help.

Regards,
Jerico
Sep 16 '06 #1
10 5580
sreema
3
i think "any condition" cannot be true and false at same time.



Hi all.I have a problem.

if (some condition)
printf("hello");
else
printf("world");

The "some condition" should be such that both "hello" and "world" are printed.Thanks for any help.

Regards,
Jerico
Sep 16 '06 #2
It is not possible in C. Then what is the need of if statement. Directly execute all the statements irrespective of the conditions. So C has switch case to handle this type of situations. Go through the switch case.
Sep 16 '06 #3
sreema
3
wht i meant was in C both if and else cannot execute sequentially. Definetly can be done using switch.



QUOTE=pukur123]It is not possible in C. Then what is the need of if statement. Directly execute all the statements irrespective of the conditions. So C has switch case to handle this type of situations. Go through the switch case.[/quote]
Sep 16 '06 #4
jerico
39
I also thought so.But then I remembered something called fork().If I use

if(!fork())
printf("hello");
else
printf("world");

then both "hello" and "world" are printed. Can there be any other solution?

Regards,
Jerico
Sep 16 '06 #5
I also thought so.But then I remembered something called fork().If I use

if(!fork())
printf("hello");
else
printf("world");

then both "hello" and "world" are printed. Can there be any other solution?

Regards,
Jerico
hey function which u used fork() is used in unix to create a child process.So here two threads of controls are generated one by parent and another by child.When fork is executed a child process is created.The parent process returns the processid of the child and the child process returns 0 .so one printf is executed by parent and another one by child .
Sep 16 '06 #6
There is always a solution to a problem.. It depends on how u think.. Instead of focussing on the problem, focus on the solution.. the question was not to try to figure out whether c supports the control entering if and else.. it just said the output needs to be hello world.. well answer is simple..


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

The conditions printf("Hello") will print hello and value retured is obviously true which by using ! can be made false so that it can then print world...

"Think, It may be a new experience"!!!
Jan 6 '07 #7
willakawill
1,646 1GB
There is always a solution to a problem.. It depends on how u think.. Instead of focussing on the problem, focus on the solution.. the question was not to try to figure out whether c supports the control entering if and else.. it just said the output needs to be hello world.. well answer is simple..


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

The conditions printf("Hello") will print hello and value retured is obviously true which by using ! can be made false so that it can then print world...

"Think, It may be a new experience"!!!
Nice answer :)
Jan 6 '07 #8
Very good question and answer. The more interesting way is presentation. If you have this kind of nice question please email me at vsagarmca@gmail.com
Any help would be appreciated.
Jul 11 '07 #9
Perhaps recursion might interest you..

Compliments of sleep+walker programming:1 Yahoo! Chat.

/* begin ifelse.c */

#include <stdio.h>

int main(void)
{
static int i;

if(!i++)
{
printf("Hello ");
main();
}
else
printf("World!");

return 0;
}

/* end ifelse.c */

C:\MinGW>gcc -Wall -pedantic -std=c99 -O2 -o ifelse.exe ifelse.c
C:\MinGW>ifelse
Hello World!
C:\MinGW>
Mar 23 '08 #10
I've considered KartikVlsi's idea and I'm quite convinced that unless by an act
of God the if statement will NEVER execute. My proposal eliminates writing code that will never execute, god willing.

I propose:

/* begin file.c */

#include <stdio.h>

int main(void)
{

if(!printf("Hello "))
{
}
else
printf("World!");

return 0;
}

/* end file.c */


C:\MinGW>gcc -Wall -pedantic -std=c99 -O2 -o file.exe file.c

C:\MinGW>file
Hello World!
C:\MinGW>
Mar 24 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gary | last post by:
I am trying to do a simple "less than" conditional statement, and hitting a brick wall if I use a database element with it. This DOES work: <% if Request.QueryString("PC") < 10 then...
8
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it...
4
by: Andrei Ivanov | last post by:
Hello, I have 2 tables: CREATE TABLE products ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL ); CREATE TABLE products_daily_compacted_views ( product ...
1
by: Sam Kong | last post by:
Hello! Recently I had a strange problem with Visual C# 2005 beta 1. When I ran(F5 key) a program, <#if DEBUG> statement was not working. It ran as RELEASE mode. So I had to manually define...
4
by: Dovla | last post by:
Hi, I have the following problem. I have a project that uses the Treeview control. The code supports drag-drop functionality for moving nodes between different levels and for changing their...
12
by: John | last post by:
I can't get my head around this! I have the following code: <% .... Code for connection to the database ... .... Code for retrieving recordset ... If Not rs.EOF Then ... Do something...
6
by: db2group88 | last post by:
hi , i am trying to create such sql on db2 v8.2 on windows. create view AP (APAM32, APNO20, APQY05, pocd01) as select APP1.APAM32, APP1.APNO20, APP1.APQY05, COALESCE(POPT.POCD01, ' ') from APP1...
1
by: pitjpz | last post by:
We have moved our Database to another server. The server it was on used SQL 4 and the new one its on now uses SQL5 the only problem we can find is that when you attempt to delete a record from...
5
by: A.Gallus | last post by:
This is my problem: I have two classes A and B: class A { void methodA(); } class B
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.