Puzzle 54 – No More If’s

Sunday, October 4, 2009
Language – Java | Type – Concept | Last date 11-Oct-2009 12:00 p.m. IST | Points 3

Today’s unearthly puzzle finds its roots in code written by a Jalvanian master. The question in today’s puzzle should be self explanatory.

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println(
"Print This");
if(true){}
System.out.println(
"Print This - Not!");
if(true){}
System.out.println(
"Print This");
}
}


It’s kind of pretty obvious what needs to be done – the code says it all.

You must of course follow the Three Jalvanian Laws.

1. You may only add but not delete nor comment out any of the code.

2. A real Jalvanian would never use an else so neither must you.

3. You may not use any more boolean variables (there are three in the code – which must remain unaltered) or conditions that evaluate to a boolean value. (So no true==false – since false is a boolean value etc).

That’s all folks!!

Got an answer? Do leave it here.

9 comments:

TheMalkolm said...

public static void main(String[] args) {
try{
if(true){}
System.out.println("Print This");
}catch(Exception e){
if(true){}
System.out.println("Print This - Not!");
}finally{
if(true){}
System.out.println("Print This");
}
}

Summers said...

if(true){}
System.out.println("Print This");
not:
{
if(true){break not;}
System.out.println("Print This - Not!");
}
if(true){}
System.out.println("Print This");

vector9x said...

System.out.println("Print This - Not!".substring(0,10));

radu said...

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println("Print This");

try
{
if(true){throw new Exception();}
System.out.println("Print This - Not!");
}
catch (Exception e) {
}
if(true){}
System.out.println("Print This");
}
}

radu said...

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println("Print This");

try
{
if(true){throw new Exception();}
System.out.println("Print This - Not!");
}
catch (Exception e) {
}
if(true){}
System.out.println("Print This");
}
}

Vinod Pahuja said...

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){{}
System.out.println("Print This");}
if(!true){{}
System.out.println("Print This - Not!");}
if(true){{}
System.out.println("Print This");}
}
}

radu said...

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println("Print This");

for(;;){
if(true){break;}
System.out.println("Print This - Not!");
}
if(true){}
System.out.println("Print This");
}
}

Eric Jablow said...

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println("Print This");
try {
if(true){throw new RuntimeException()}
System.out.println("Print This - Not!");
} catch (RuntimeException e) {}
if(true){}
System.out.println("Print This");
}
}

Anonymous said...

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println("Print This");
if(true){}
try {}
catch(Exception e) {
System.out.println("Print This - Not!");
}
if(true){}
System.out.println("Print This");
}
}

Post a Comment

Solution for this question?