473,387 Members | 1,942 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,387 software developers and data experts.

what is the Condition to be given to enter into both if and else loop ?

karthik baskar
Expand|Select|Wrap|Line Numbers
  1. if(condition)
  2. {
  3.       printf("Hello");
  4. }
  5. else
  6. {
  7.       printf(" World");
  8. }
  9.  
What condition should I give to get the output as Hello World ??
Nov 25 '10 #1
7 2339
Banfa
9,065 Expert Mod 8TB
The real answer to the question is that you do not want to associate yourself with someone asking such a stupid question.

On a *nix platform you could get both if and else branches to run by calling fork() as the condition although it would be a little random as to which branch ran first.
Nov 25 '10 #2
Actually this was a question asked in an interview. But, my friends said that this question has an answer other than what you have mentioned and it works in all versions of C/C++.
Nov 25 '10 #3
I think !printf("hello ") will be worked.
Expand|Select|Wrap|Line Numbers
  1. if(!printf("hello"))
  2. {
  3.       printf("Hello");
  4. }
  5. else
  6. {
  7.       printf(" World");
  8. }
And fork() is also available.
Nov 25 '10 #4
Banfa
9,065 Expert Mod 8TB
Using !printf("hello ") does not actually cause the if cause to run it just makes it look like it might have. For example

Expand|Select|Wrap|Line Numbers
  1. if(!printf("hello"))
  2. {
  3.       printf("Goodbye");
  4. }
  5. else
  6. {
  7.       printf(" World");
  8. }
  9.  
Still prints "HelloWorld" not "GoodbyeWorld" because the if clause does not actually run.


BTW I know its and interview question but that doesn't make it a good question just a silly one that anyone with any sense would know better than to ask.

If you have an option don't work for the people asking this question.
Nov 25 '10 #5
if(getche() !== hello)
{
printf("Hello");
}
else
{
printf(" World");
}
Nov 25 '10 #6
Sorry, I think you are not quite catch me.
if you want to print Goodbye, Then you should use the following code:
Expand|Select|Wrap|Line Numbers
  1. if(!printf("Goodbye"))
  2.       printf("Goodbye"); 
  3. else 
  4.       printf(" World"); 
  5.  
printf("GoodBye") would return an int.
Is that right?
Thank you!
Nov 26 '10 #7
Banfa
9,065 Expert Mod 8TB
Sorry, I think you are not quite catch me.
No you are not catching me. The question is "what Condition can be use to make the flow of execution enter both the if and else code block".

This

Expand|Select|Wrap|Line Numbers
  1. if(!printf("Goodbye"))
  2.       printf("Goodbye"); 
  3. else 
  4.       printf(" World"); 
  5.  
Does not do it, the if code block is not entered the condition just produces output that makes it look like it might have done.

I demonstrated this by changing the output of the if code block to show that it does not run.

There is no condition that will make both code blocks run.
Nov 26 '10 #8

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

Similar topics

2
by: Jo Vermeulen | last post by:
Hello, I was wondering if it's possible to add a condition to the enhanced for loop. This is an example of an enhanced for loop: LinkedList<MyClass> children = new LinkedList<MyClass>();...
1
by: Lee Harr | last post by:
I am just starting to use properties, and wonder what you might think of this ... class RNumber(object): def __init__(self, n, nmin, nmax): self.nmin = nmin self.nmax = nmax self.n = n
13
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own...
3
by: faizankhan666 | last post by:
For example if some variable lets say..a is greater than 10 then program should close.... i.e "press any key to continue" message appears.
2
by: saturn99 | last post by:
I have a requirement to write the SQL code as follows(peseudo code): IF THEN INSERT INTO .... ELSE UPDATE .... END IF The codes must be executed as...
1
by: kenneth6 | last post by:
#include <iostream> using namespace std; int main() { char firstname; char finalFirstname; cout << "Enter your first name: "; int num = 0;
2
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2;...
2
by: expoC | last post by:
Why is the below program is not coming out of the for loop and the variables outputting dummy values /* oddeven.c: Illustrates looping construts */ #include <stdio.h> int main() { for (;...
3
by: rayda | last post by:
i was doing a an ASP.net web page with vb. i m putting the login and sign up form in the same web page. i tried the sign up form. after i filled up all the required fields, when i press the Enter...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.