Coder Tidbit 01

Keep coming across things like this at work that make me stop and think for a while so i thought they would be interesting to post up here for others to think about too.

Heres one i came accross today that was causing a strange error in the game:

[code lang="java"]
boolean a = true;
if(a){ System.out.println("HERE1"); } { System.out.println("HERE2"); }
[/code]

Which one is printed? HERE1 or HERE2 or both?

If a = false which is printed?

4 Responses to Coder Tidbit 01
  1. Joel Martinez Reply

    both will be printed because the first scope ( { … } ) is the only one affected by the if statement. the second one is just a standalone block. if a was false, only HERE2 would be printed.
    :-)

  2. admin Reply

    Correctomondo joel, you win the prize… if there was a prize :P

  3. Pete Reply

    Cakes.

  4. Ramon Fritsch Reply

    Hello,

    You missed the “else” statement
    ;)

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>