• Post Reply Bookmark Topic Watch Topic
  • New Topic

if-else  RSS feed

 
Bhawana Gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bhawana Gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably the trick that was being looked for. Here's another silly approach that I think would work:

 
Bhawana Gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!