| Author |
alternate code for if/else needed
|
Pradeep allada
Greenhorn
Joined: Jul 28, 2011
Posts: 15
|
|
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
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
|
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
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
Bartender
Joined: Jan 28, 2008
Posts: 4503
|
|
Pradeep mca wrote: write code to get the output of if..else with out using if else and any other controle statements?
your opinion?
|
Not everything that counts can be counted, and not everything that can be counted counts-Albert Einstein
|
 |
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: 26716
|
|
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 ⊓ ¬g ⟹ skip.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26716
|
|
|
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: 19
|
|
|
What about switch and ? : are they not allowed as well ?
|
 |
Claudiu Chelemen
Ranch Hand
Joined: Mar 25, 2011
Posts: 62
|
|
|
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: 8428
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26716
|
|
|
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: 19
|
|
|
Although this is bad design but maybe you can use assert ?
|
 |
Pradeep allada
Greenhorn
Joined: Jul 28, 2011
Posts: 15
|
|
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
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
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: 26716
|
|
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: 26716
|
|
|
|
 |
Pradeep allada
Greenhorn
Joined: Jul 28, 2011
Posts: 15
|
|
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
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18365
|
|
|
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: 62
|
|
Here's another solution:
Cheers!
Claudiu
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26716
|
|
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: 13842
|
|
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: 62
|
|
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: 2127
|
|
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: 3901
|
|
i would think about it for a while. then i would just get up and leave. but that's me.
|
I never took notes in college. That's how I got a 4.0 the first 2 years, and a 3.5 the second two years.
|
 |
Myke Enriq
Greenhorn
Joined: Feb 13, 2012
Posts: 28
|
|
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
Greenhorn
Joined: May 22, 2012
Posts: 14
|
|
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.
|
 |
 |
|
|
subject: alternate code for if/else needed
|
|
|