• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

alternate code for if/else needed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why this is in the Jobs section. Moved to Beginning Java.

Welcome to the JavaRanch.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Federico Cardelle wrote:


Isn't valid Java.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about switch and ? : are they not allowed as well ?
 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say no, since they're still control statements..
 
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, here is the workaround for my previous try, now working in java...

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Pradeep allada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



ThanQ man It's working perfectly....
 
Federico Cardelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another solution:




Cheers!
Claudiu
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this discussion has now reached the standard of creativity meriting transfer to the diversions forum
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic