This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programming Diversions and the fly likes alternate code for if/else needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "alternate code for if/else needed" Watch "alternate code for if/else needed" New topic
Author

alternate code for if/else needed

Pradeep allada
Greenhorn

Joined: Jul 28, 2011
Posts: 17
Recently my friend have attended an interview, in that the interviewer asked

write code to get the output of if..else with out using if else and any other controle statements?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Not sure why this is in the Jobs section. Moved to Beginning Java.

Welcome to the JavaRanch.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
Pradeep mca wrote: Recently my friend have attended an interview, in that the interviewer asked

write code to get the output of if..else with out using if else and any other controle statements?


The ternary operator [ <condition>?<operation if true>:<operation if false> ]!? eg: x<10?true:false;
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Which a) is technically a control statement (and therefore not allowed), and b) cannot handle anything that does not return a value.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Federico Cardelle
Greenhorn

Joined: Jul 26, 2011
Posts: 26
Pradeep mca wrote: Recently my friend have attended an interview, in that the interviewer asked

write code to get the output of if..else with out using if else and any other controle statements?

My try...

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Pradeep mca wrote: write code to get the output of if..else with out using if else and any other controle statements?

your opinion?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Federico Cardelle wrote:

Isn't valid Java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
It would probably work in C, however.

Interviewers seem to like giving their victims impossible tasks and seeing how they cope. With the exception of the (a==b), which will not work in Java™, all the suggestions use some variant of this Generalised Substitution Language (GSL) idiom:
g ⟹ S ⊓ ¬g ⟹ T
   which is pronounced "g guards S demonic choice not g guards T".
All forms of choice are actually implementations of that construct, which in Java™ would appear as if (g) S() else T(); but it is impossible to create a selection control which is not some version of
g ⟹ S ⊓ ¬g ⟹ T.
Note the straight if without an else is a specialised form of that GSL construct: g ⟹ S ⊓ ¬gskip.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
It might not work in C, even. Is the result of a==b defined in C? Does it always return 1 for true? Can it return -1 for true, as happens in many languages.
Jai Mani
Greenhorn

Joined: Sep 11, 2010
Posts: 23
What about switch and ? : are they not allowed as well ?
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

I would say no, since they're still control statements..
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

Jai Mani wrote:What about switch and ? : are they not allowed as well ?


No, i think they are too considered control-statements and therefore not allowed.

I dont know the answer.
I would probably ask the interviewer to describe a situation where you would have to do
such a thing, and there would be no way to use a control-flow.


Knowledge Reigns Supreme
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

My first question back to the interviewer would be "Since your specs are too vague, can you give me a list of what you consider control structures and therefore not allowed, since otherwise no matter what I give you, you can come back with 'no, that's a control structure'".

There is no reason to even begin solving the problem until you have better specs.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
As I said, it is more about how you deal with an impossible request than about your answer.
Jai Mani
Greenhorn

Joined: Sep 11, 2010
Posts: 23
Although this is bad design but maybe you can use assert ?
Pradeep allada
Greenhorn

Joined: Jul 28, 2011
Posts: 17
Seetharaman Venkatasamy wrote:
Pradeep mca wrote: write code to get the output of if..else with out using if else and any other controle statements?

your opinion?




I don't know exactly
maybe we can write using bitwise operators
Federico Cardelle
Greenhorn

Joined: Jul 26, 2011
Posts: 26
ok, here is the workaround for my previous try, now working in java...

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Jai Mani wrote:Although this is bad design but maybe you can use assert ?

That could work, until you turn off assertions.

For those that wonder how that could work:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Both those last two solutions are so brilliantly far-fetched, I am tempted to move this thread to "Programming Diversions"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Pradeep allada
Greenhorn

Joined: Jul 28, 2011
Posts: 17



ThanQ man It's working perfectly....
Federico Cardelle
Greenhorn

Joined: Jul 26, 2011
Posts: 26
From several sites...


Java Control statements control the order of execution in a java program, based on data values and conditional logic. There are three main categories of control flow statements;

· Selection statements: if, if-else and switch.

· Loop statements: while, do-while and for.

· Transfer statements: break, continue, return, try-catch-finally and assert.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Touche. I didn't even think about try-catch being control statements as well.
Federico Cardelle
Greenhorn

Joined: Jul 26, 2011
Posts: 26
Ok, next try, even more far-fetched.
The problem with not using control statements is that all the lines of your code will be executed in the given order.
The only workaround I can think of is commenting out the lines that you don't want to execute.
I have written a method that prints \\ or \* depending on two arguments being equal or not, without using control statements (well, it uses return, but you can avoid it if you use instance variables and make the method return void ).
Then, you can write a method that takes a .java file, parses it and copy to another .java file, but when it reaches an if...else blocks, it call four times to a
modified version of the commentOut method and it comments out the block that wouldn't be executed with the if...else (I mean, one block in the resulting .java file will be between "//" lines and will execute, the other block will be between "/* ... */" and won't execute.

Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Here's another solution:




Cheers!
Claudiu
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I think this discussion has now reached the standard of creativity meriting transfer to the diversions forum
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Campbell Ritchie wrote:I think this discussion has now reached the standard of creativity meriting transfer to the diversions forum


A very creative diversion, I must say!
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

I wouldn't say that try-catch and assert are control statements.
And neither would they:... http://download.oracle.com/javase/tutorial/java/nutsandbolts/flow.html

(but obviously, you can control the flow with 'em )

Claudiu
Federico Cardelle
Greenhorn

Joined: Jul 26, 2011
Posts: 26
I think that i got it
My previous try was a useless fail, because it only worked when the test was always true or always false
But now I used a little bit of reflection here it is...
I created two classes that override toString() (yes, I had to consult the javadoc ) and the whatIsMyClass method, that declares an Object and initializates it to be of the class TRUE of FALSE depending of the two parameters being equal or not. When I invoke o.toString() it uses the version corresponding to its class. Of course I could make the overriden toString method to do whatever the If block made or, even better, I could use an interface with a method specific for this test.
Just change "myPackage" for a valid package and you will see that it works

Tariik el berrak
Greenhorn

Joined: Jan 23, 2009
Posts: 9
The majority of the tests last 1 hour with 20 to 30 questions, I think what they are looking for is to see if you can write it differently:



could be writen like :

Daniel Doboseru
Ranch Hand

Joined: Sep 26, 2011
Posts: 57
^disagree. This kind of question is more psychological than technical. He's not interested in seeing if you can write if/else as a "? :" but to see your reaction under stress when facing an apparently impossible task. With hard-work you can drastically improve your coding skills in a very short time, but your smartness not! This is why some companies often hire students with zero knowledge in a certain programming language, because even if you don't know the syntax, after a interview they figure out that you might be a good asset.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
Hmmm, well as long as we're resurrecting such an old thread, it seems to me it was answered pretty well early on, then incorrectly dismissed:
Rob Spoor wrote:
Ashutosh Limaye wrote:The ternary operator [ <condition>?<operation if true>:<operation if false> ]!? eg: x<10?true:false;

Which a) is technically a control statement (and therefore not allowed), and b) cannot handle anything that does not return a value.

a) Technically, a ternary operator is not a statement at all, according to the JLS.
b) True. However the original question asked vaguely to get the "output". If there's something to output, it can be converted into something to return. If there's no output, then there;s nothing to do anyway.

Similarly, it may also be possible to do this with a Map of some sort, as long as the condition in the if statement is simple enough. But that doesn't really handle an all-inclusive "else" without using an actual control statement.

We don't really know the intent of the interviewer, but I wouldn't be too quick to dismiss the easy solution.
Daniel Doboseru
Ranch Hand

Joined: Sep 26, 2011
Posts: 57
Usually, when such absurd constraints are raised (let's be serious, in which case you are not allowed to use an if/else?) is not necessarily about the solution, but your reaction. He wants to see how would you do in a complicated situation: would you ask for more details, would you try to solve it right away, would you stay and think a bit about it, would you slap him etc.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i would think about it for a while. then i would just get up and leave. but that's me.


SCJP
Visit my download page
Myke Enriq
Ranch Hand

Joined: Feb 13, 2012
Posts: 111
if ( a == b ) then c = value_ONE
else c = value_TWO


my take on it:


try{
float aux = 1/ (a - b)
c = value_ONE
}catch(Exception e){
c = value_TWO
}


sure:
a) an exception could appear in the try section that is not caused by division by zero
b) there could not be an operation called a - b (a and b are not numbers for example)

But I guess it is a start.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Campbell Ritchie wrote:Is the result of a==b defined in C? Does it always return 1 for true? Can it return -1 for true, as happens in many languages.


1. Yes, it is
2. Yes, it always does.
3. No, it can not.

In C, if an integral or pointer expression is checked as the argument of an if, it is considered false if it is zero or null, and true otherwise.

However if a logical expression is considered as a number, it is taken for 0 if false and 1 if true.
 
GeeCON Prague 2014
 
subject: alternate code for if/else needed