Forums Register Login

if-else

+Pie Number of slices to send: Send
Hi i am new to code Ranch. i have a code snippet below :



What should be condition so that output comes out to be : helloworld
+Pie Number of slices to send: Send
That is not possible. if {} else {} statements give you an "either one thing or the other thing" decision flow. Not both things.

It would technically be possible if you surrounded that statement with a loop that caused it to be run twice with the condition changed for each. True for the first, false for the second.

But I have no idea why you would want to do that. I'm not suggesting you should do it. Ever.
+Pie Number of slices to send: Send
you are correct. even i wonder why would someone need this, however i need answer since it was an interview question.
so possibly some silly trick may print both. (i mean we need not actually go to both blocks, what we need is to print helloworld on console)
if it was C solution might had been :

if(!printf("hello"))
.......

since printf has return type boolean. However in Java i have searched same does not work as all print statements have return type void. if i missed any please point out.
+Pie Number of slices to send: Send
 

Bhawana Gupta wrote:you are correct. even i wonder why would someone need this, however i need answer since it was an interview question.
so possibly some silly trick may print both. (i mean we need not actually go to both blocks, what we need is to print helloworld on console)
if it was C solution might had been :

since has return type. However in Java i have searched same does not work as all print statements have return type void. if i missed any please point out.



The java PrintStream class also has printf() method -- and that method does have a return type.

Henry
+Pie Number of slices to send: Send
That's probably the trick that was being looked for. Here's another silly approach that I think would work:

+Pie Number of slices to send: Send
Thanks Henry Wong. However i need some print statement that returns boolean (without need of creating new method as suggested by Matthew Brown ) is it possible that way ?
or some other trick that does not demand new methods
+Pie Number of slices to send: Send
 

Bhawana Gupta wrote:Thanks Henry Wong. However i need some print statement that returns boolean (without need of creating new method as suggested by Matthew Brown ) is it possible that way ?
or some other trick that does not demand new methods


You could test if the returned value is equal (or not equal) to some other value (hint: what value can be assigned to any reference variable). That would give you a boolean statement.
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1076 times.
Similar Threads
Logic problem or invoke method problem
return in thread.
alternative available for If Else condiotion
Clarification about Ternary operator
S2.equals(7) is false, but (S2 == 7) is true. Short S2 = new Short((short)7); Why?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:15:36.