Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

alternate code for if/else needed

 
Pradeep allada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why this is in the Jobs section. Moved to Beginning Java.

Welcome to the JavaRanch.
 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which a) is technically a control statement (and therefore not allowed), and b) cannot handle anything that does not return a value.
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Federico Cardelle wrote:

Isn't valid Java.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about switch and ? : are they not allowed as well ?
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say no, since they're still control statements..
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, it is more about how you deal with an impossible request than about your answer.
 
Jai Mani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although this is bad design but maybe you can use assert ?
 
Pradeep allada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, here is the workaround for my previous try, now working in java...

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both those last two solutions are so brilliantly far-fetched, I am tempted to move this thread to "Programming Diversions"
 
Campbell Ritchie
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Pradeep allada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



ThanQ man It's working perfectly....
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Touche. I didn't even think about try-catch being control statements as well.
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another solution:




Cheers!
Claudiu
 
Campbell Ritchie
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this discussion has now reached the standard of creativity meriting transfer to the diversions forum
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4395
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would think about it for a while. then i would just get up and leave. but that's me.
 
Myke Enriq
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic