Note that there are some explanatory texts on larger screens.

plurals
  1. PONot using an if statement in Java
    primarykey
    data
    text
    <p>This is a very odd, and quite specific question.</p> <p>Ultimately I am trying to write a program convert that takes in java source, and transforms it such that it does not use (Among other things)</p> <ul> <li>Arrays</li> <li>Loops</li> <li>User defined methods</li> <li>If statements</li> </ul> <p>This is a challenge that I set for myself, after my teacher told me that it was impossible to write a program without using these things.</p> <p>I have most of these worked out, including function inlining and array substitution, however I cannot work out how to manage an if statement.</p> <p>In C++ I'd use labels and gotos and maybe ?:, however Java does not support GOTO statements.</p> <p>My question is this: Given a section of code,</p> <pre><code>if(CONDITION) { //More code in here } </code></pre> <p>How can transform it such that it is functionally the same, however does not use the if keyword. Note that loop structures are also out of the question.</p> <p>Given this, it would be easy to create else and else if statements. However I am also unsure of how to create loops using this, as there is no GOTO statement and methods are out of the question.</p> <p>Edit: Please note that switches are also not allowed, nor is recursion (Ruled out by the fact that you cannot define user methods, and a recursive main function wouldn't work with every program) The ?: operator does not work for all situations. AFAIK you cannot call a void function with ?: as it wants to assign a value as part of its operation.</p> <p>These conditions come from the IB Computer Science SL requires course, I am taking HL and as a class we were laughing at the 'mastery' factors for SL which include 'if' statements (And if fact 3/15 of them are 'User defined methods with params and return types) The challenge is effectively to FAIL a mastery test in SL while still producing a program that functions correctly.</p> <p>Answer: (By bdares)</p> <pre><code>String result = (CONDITION)?"0":"A"; try{ Integer.parseInt(result); //Condition is true } catch(NumberFormatException e){ //Condition is false } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload