My systems crushes down if I am testing my method

public void zehnDurchlaeufe(){for (float x=17000000f;x=17000010f; ++x)}Does anybody know what my mistake is ?
[151 byte] By [JustLilla] at [2007-11-26 14:53:20]
# 1

That won't compile. Try posting code that compiles and provide details about what problems occur when you run it.

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

(I think I know what your problem is, but I want you to post the real code and ask a better question.)

jverda at 2007-7-8 8:41:38 > top of Java-index,Java Essentials,Java Programming...
# 2

okay :) you are right !

Here is the real code :

/**

* runs the sharpens ten times

*/

public void tenRuns()

{

for (float x=17000000f;

x<17000010f; ++x);

}

Compiling is no problem. The system doesn't report any errors, but when I try to test my method my pc calculates and cannot find and end.

I think there have to be something wrong with my formula..

JustLilla at 2007-7-8 8:41:38 > top of Java-index,Java Essentials,Java Programming...
# 3

> public void zehnDurchlaeufe()

> {

>for (float x=17000000f;

>x=17000010f; ++x)

> y know what my mistake is ?

1. the second expression must be a boolean expression, not an integer expression (I assume what you meant to say was x == 17000010f).

2. The second term is a continuation condition, not a termination condition. If you execute it as written, in the very first pass, it will test the condition, fail, and exit without having ever moved through the loop even once.

I assume that what you really wanted is x < 17000010f. This will cause the loop to exit when x is equal to (or larger than) 17000010.

- Adam

guitar_man_Fa at 2007-7-8 8:41:38 > top of Java-index,Java Essentials,Java Programming...
# 4

hmmm... I see nothing wrong with that code. try this:

public void foo() {

System.out.println("Entering the loop");

for (float x = 17000000f; x < 17000010f; x++) {

System.out.println(x);

}

System.out.println("Exiting the loop");

}

Take a look at the output of that, and see if that tells you anything about the behavior.

This should tell you whether the code is ever exiting the loop, or whether its even ever entering the loop in the first place...

And if it's getting stuck in the loop, it should tell you why -> i.e., what the value of x is at all times.

- Adam

guitar_man_Fa at 2007-7-8 8:41:38 > top of Java-index,Java Essentials,Java Programming...
# 5

Are you assuming that adding 1 to 17000000f will give you 17000001f? Well, that isn't necessarily so. In fact (I just checked with a little test program on my computer) it's actually 17000000f. Floating point representation isn't exact and floats can only represent about 6 significant figures. You have 8 figures there so adding 1 is less than the round-off error.

Everybody always posts this link to explain things to beginners:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Edit: ... like those other people said while I was starting up RAD to write my little test program...

Message was edited by:

DrClap

DrClapa at 2007-7-8 8:41:38 > top of Java-index,Java Essentials,Java Programming...