Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > if statement that, when false, skips first statement in its block, executes second?

Reply
Thread Tools

if statement that, when false, skips first statement in its block, executes second?

 
 
Jay McGavren
Guest
Posts: n/a
 
      01-14-2006
`I've got some very strange behavior here involving an if statement
which, when false, skips the first statement in its block, but then
*executes the second*! I'm hoping I've made some mistake which will be
obvious to someone in the group, that I just haven't noticed yet.

Here's output from a jdb session (my comments preceded with "*"):

*The if statement is about to be evaluated:
main[1] list
54 Iterator conditions = conditionIterator();
55 boolean conditionFailed = false; //If behavior has no
conditions, it should be performed by default.
56 conditionLoop: while (conditions.hasNext()) {
57 Condition condition = (Condition) conditions.next();
58 => if (condition.isMet((GameObject) subject, (GameObject)
object) == false) {
59 conditionFailed = true;
60 break conditionLoop;
61 }
62 }
63
*As you can see, it *should* evaluate to false:
main[1] print condition.isMet((GameObject) subject, (GameObject)
object)
condition.isMet((GameObject) subject, (GameObject) object) = true
main[1] next
>

Step completed: "thread=main", net.sourceforge.zyps.Behavior.perform(),
line=60
bci=44
*And yet, here it is ready to execute the *second* statement
*in the if block! (Even though it skipped the first!)
60 break conditionLoop;

main[1] list
56 conditionLoop: while (conditions.hasNext()) {
57 Condition condition = (Condition) conditions.next();
58 if (condition.isMet((GameObject) subject, (GameObject)
object) == false) {
59 conditionFailed = true;
60 => break conditionLoop;
61 }
62 }
63
64 //Process all actions.
65 Iterator actions = actionIterator();

JDB was run with the command:
jdb -classpath build;test_build;C:\junit\junit.jar -sourcepath
source;test net.sourceforge.zyps.BehaviorTest

Full source can be accessed here:
Behavior:
http://cvs.sourceforge.net/viewcvs.p...=1.4&view=auto
BehaviorTest (probably not relevant):
http://cvs.sourceforge.net/viewcvs.p...=1.4&view=auto
Entire source tree:
http://cvs.sourceforge.net/viewcvs.py/zyps/zyps/

If anyone has any ideas, they would be most appreciated!

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      01-14-2006

"Jay McGavren" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> `I've got some very strange behavior here involving an if statement
> which, when false, skips the first statement in its block, but then
> *executes the second*! I'm hoping I've made some mistake which will be
> obvious to someone in the group, that I just haven't noticed yet.
>
> Here's output from a jdb session (my comments preceded with "*"):


<snip code / jdb trace>

In my experience, when I have observed a debugger jumping to the wrong
statements (especially around ifs, whiles, etc) it's usually because I
changed the source code and forgot to recompile / redeploy and I'm seeing
the old code execution with the new source lines.

Cheers,
Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
 
Jay McGavren
Guest
Posts: n/a
 
      01-14-2006
Matt Humprey wrote:
> In my experience, when I have observed a debugger jumping
> to the wrong statements (especially around ifs, whiles,
> etc) it's usually because I changed the source code and
> forgot to recompile / redeploy and I'm seeing the old code
> execution with the new source lines.


Unfortunately, this is a fresh compile. (And this problem has
persisted for a week through multiple compilations on both Windows and
Linux.) I'm certainly willing to believe that my compiled code and my
source aren't "aligned" correctly, but I don't know how to rectify
that...

I'm compiling via Ant, BTW. Here's the task:

<target name="compile" depends="initialize" description="Compile the
source">
<javac srcdir="${source_directory}" destdir="${build_directory}"
debug="on">
<classpath refid="project.class.path"/>
<!-- <compilerarg value="-Xlint:unchecked"/> -->
</javac>
</target>

http://cvs.sourceforge.net/viewcvs.p...=1.2&view=auto

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-14-2006
On 14 Jan 2006 13:35:16 -0800, "Jay McGavren" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>`I've got some very strange behavior here involving an if statement
>which, when false, skips the first statement in its block, but then
>*executes the second*!


Errors of this sort can be avoided by these rules:

1. always enclose if and else bodies in { }. Beware particularly the
construction
if ()
if ()

2. Check for ; after if (); it should not be there. If you really
mean it write:

/* do nothing */ ;

instead.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
opalpa@gmail.com opalinski from opalpaweb
Guest
Posts: n/a
 
      01-15-2006
Neither one applies here Roedy.

Re: 1

There is only one if, so nested if statements are not an issue. Also,
a note, Kernighan writes if statements without braces.


Re: 2

It's not there.

---

Sometimes compilers implement breaks and continues wrong. There was
one time when AT&T's telephone network, about twenty five years back,
went down cause a break from a switch was implemented wrong in
compiler. Anyway, looks like a bug; perhaps rewrite the code? I
believe this code could be written more idiomatically. Idioms rarely,
if ever, compile wrong.

Opalinski
(E-Mail Removed)
http://www.geocities.com/opalpaweb/

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-15-2006
On 14 Jan 2006 19:07:51 -0800, "(E-Mail Removed) opalinski from
opalpaweb" <(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>There is only one if, so nested if statements are not an issue. Also,
>a note, Kernighan writes if statements without braces.


and it is a dangerous practice for newbies.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      01-15-2006
Roedy Green <(E-Mail Removed) > wrote:
> On 14 Jan 2006 19:07:51 -0800, "(E-Mail Removed) opalinski from
> opalpaweb" <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
> >There is only one if, so nested if statements are not an issue. Also,
> >a note, Kernighan writes if statements without braces.

>
> and it is a dangerous practice for newbies.


Not just for newbies...

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      01-15-2006
Roedy Green wrote:
> On 14 Jan 2006 19:07:51 -0800, "(E-Mail Removed) opalinski from
> opalpaweb" <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
>
>>There is only one if, so nested if statements are not an issue. Also,
>>a note, Kernighan writes if statements without braces.


Famous as a C programmer who writes compressed code with lots of
symbols, undescriptive identifiers and little compiler support.
Hopefully most of us have left that sort of stuff behind.

> and it is a dangerous practice for newbies.


It's a dangerous practice for anyone.

I once spent a day or two trying to optimise an embedded system in order
to get a half-decent sound quality. Then I noticed a stray semicolon at
the end of a {}less if().

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-15-2006
On Sun, 15 Jan 2006 16:41:27 +0000, Thomas Hawtin
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>I once spent a day or two trying to optimise an embedded system in order
>to get a half-decent sound quality. Then I noticed a stray semicolon at
>the end of a {}less if().


I would like it if there were a lint option to insist on {} for if and
no empty statements. Algol 68 had "nihil" when you wanted an explicit
empty statement. If's without them look so innocent but do things so
different from what they first appear.

I can stare at the code for 10 minutes and still not see the problem.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Jay McGavren
Guest
Posts: n/a
 
      01-15-2006
Everyone, thanks for the advice, but it now appears as if this is a
case of jdb inaccurately reporting which line it is about to execute.
I placed an executable line following the if block, thusly:

conditionLoop: while (conditions.hasNext()) {
Condition condition = (Condition) conditions.next();
if (! condition.isMet((GameObject) subject, (GameObject) object)) {
conditionFailed = true;
break conditionLoop;
}
System.out.println("foobar");
}

....and when the if statement was false, it then reported that the line
it was about to execute was the println (instead of the break). A
check of the variable values suggests that no code within the if block
was ever executed.

I think I'm starting to see why very few people use jdb. When I have
time, I'm going to try out the Eclipse debugger instead. (I also
intend to make this code a little cleaner.) Thanks for the advice,
everyone!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why the destructor executes after its friend operator (in which thereis a copy constructor) runs fl C++ 5 01-06-2013 08:32 PM
file find skips first letter Wanderer Python 4 02-15-2011 07:11 PM
Effect.SlideUp is too slow--next statement executes immediately nolo contendere Javascript 7 11-09-2007 08:44 PM
Samsung DVD-1080P7 skips first few seconds of chapters eduardorp1@gmail.com DVD Video 0 05-22-2007 06:43 AM
jar file executes fine in netbeans file manager but on its own... Joe Vasher Java 3 05-13-2004 10:48 PM



Advertisments