function in for loop

hello,

if I do this

for(int i=0; i<Math.pow(10,2);i++)

;// do nothing loop

does it mean that Math.pow subroutine will be called 101 times ?

should it be like this then:

int end = (int)Math.pow(10,2);

for(int i=0; i><end;i++)

;//do nothing loop

>

[580 byte] By [taisao003a] at [2007-11-26 20:48:59]
# 1
Yep, the second version is much better for exactly the same reason asyou described yourself.kind regards,Jos
JosAHa at 2007-7-10 2:12:29 > top of Java-index,Java Essentials,New To Java...
# 2
> hello,> > if I do this> > [code]> for(int i=0; i<Math.pow(10,2);i++)>; // do nothing loop> es it mean that Math.pow subroutine will be called> 101 times ?Well, 100 anyway. Yes.
warnerjaa at 2007-7-10 2:12:29 > top of Java-index,Java Essentials,New To Java...
# 3

> Does it mean that Math.pow subroutine will be called

> 101 times ?

Yes.

> should it be like this then:

> > int end = (int)Math.pow(10,2);

> for(int i=0; i<end;i++)

>; //do nothing loop

>

In most cases it probably doesn't matter. Go with what's easier to understand, or better fits what you're trying to do. Then, if you determine that it is causing a bottleneck, you can change it.

jverda at 2007-7-10 2:12:29 > top of Java-index,Java Essentials,New To Java...
# 4
thanks :-Dand 101 times right? i goes from 0 to 99 when it's still inside the loop, (100 times)the last times i = 100, it still need to be checked by the condition (+1) and then it jump out of the loop, so 101 right?
taisao003a at 2007-7-10 2:12:29 > top of Java-index,Java Essentials,New To Java...
# 5

> i goes from 0 to 99 when it's still inside the loop, (100 times)

> the last times i = 100, it still need to be checked by the condition (+1)

> and then it jump out of the loop, so 101 right?

Yep, you're right and warnerja was wrong :-P

kind regards,

Jos (< inventor of the Off-By-One-Error ;-)

JosAHa at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 6

> > i goes from 0 to 99 when it's still inside the

> loop, (100 times)

> > the last times i = 100, it still need to be checked

> by the condition (+1)

> > and then it jump out of the loop, so 101 right?

>

> Yep, you're right and warnerja was wrong :-P

>

> kind regards,

>

> Jos (< inventor of the Off-By-One-Error ;-)

Um, no I'm right.

Math.pow(10, 2) == 100, right?

for (int i = 0; i < 100; i++)

will loop 100 times, not 101.

When i is 0, that's the first time.

When i is 1, that's 2.

...

When i is 99, that's 100.

When i is 100, it's out of the loop, because 100 < 100 is false.

Edit: OOPS, now I get it.

It will execute the BODY of the loop 100 times, but it will evaluate pow(10, 2) 101 times.

Never mind, Jos is right.

*Storms off in a huff with head hanging low*

Message was edited by:

warnerja

warnerjaa at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 7

> > i goes from 0 to 99 when it's still inside the

> loop, (100 times)

> > the last times i = 100, it still need to be checked

> by the condition (+1)

> > and then it jump out of the loop, so 101 right?

>

> Yep, you're right and warnerja was wrong :-P

Yes, both of them were, all three times the four of them posted those five comments. That's the only six things I have to say about these seven topics.

jverda at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 8
Warnerja confessed:> Never mind, Jos is right.> > *Storms off in a huff with head hanging low**bump*kind regards,Jos (< modesty is my middle name ;'-)
JosAHa at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 9

> Warnerja confessed:

>

> > Never mind, Jos is right.

> >

> > *Storms off in a huff with head hanging low*

>

> *bump*

>

> kind regards,

>

> Jos (< modesty is my middle name ;'-)

I don't know how those posts got here! It wasn't me, trust me ;-). Someone like Goldie must have hacked my login and posted those inaccuracies to hurt my reputation! I would never make such a mistake myself ;-)

warnerjaa at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 10

> > Warnerja confessed:

> >

> > > Never mind, Jos is right.

> > >

> > > *Storms off in a huff with head hanging low*

> >

> > *bump*

> >

> > kind regards,

> >

> > Jos (< modesty is my middle name ;'-)

>

> I don't know how those posts got here! It wasn't me,

> trust me ;-). Someone like Goldie must have hacked my

> login and posted those inaccuracies to hurt my

> reputation! I would never make such a mistake myself

> ;-)

next you'll be telling us your "little brother borrowed my comp." etc

georgemca at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 11
> next you'll be telling us your "little brother> borrowed my comp." etcOh yeah, that was it. It wasn't Goldie, it was my little brother. Yeah, that's the ticket ;-)
warnerjaa at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 12

> > next you'll be telling us your "little brother

> > borrowed my comp." etc

>

> Oh yeah, that was it. It wasn't Goldie, it was my

> little brother. Yeah, that's the ticket ;-)

't could be that you forgot to activate your seventeen firewalls and swith

ip addresses 1000 times per second ;-)

kind regards,

Jos ;-)

JosAHa at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 13

> > > next you'll be telling us your "little brother

> > > borrowed my comp." etc

> >

> > Oh yeah, that was it. It wasn't Goldie, it was my

> > little brother. Yeah, that's the ticket ;-)

>

> 't could be that you forgot to activate your

> seventeen firewalls and swith

> ip addresses 1000 times per second ;-)

>

> kind regards,

>

> Jos ;-)

Don't be silly. I only have fifteen firewalls, and everyone knows you can't switch IP addresses quite that quickly.

warnerjaa at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 14

> > > > next you'll be telling us your "little brother

> > > > borrowed my comp." etc

> > >

> > > Oh yeah, that was it. It wasn't Goldie, it was

> my

> > > little brother. Yeah, that's the ticket ;-)

> >

> > 't could be that you forgot to activate your

> > seventeen firewalls and swith

> > ip addresses 1000 times per second ;-)

> >

> > kind regards,

> >

> > Jos ;-)

>

> Don't be silly. I only have fifteen firewalls, and

> everyone knows you can't switch IP addresses quite

> that quickly.

you can if you know kung fu 2.0

georgemca at 2007-7-10 2:12:30 > top of Java-index,Java Essentials,New To Java...
# 15

> > > > next you'll be telling us your "little brother

> > > > borrowed my comp." etc

> > >

> > > Oh yeah, that was it. It wasn't Goldie, it was

> my

> > > little brother. Yeah, that's the ticket ;-)

> >

> > 't could be that you forgot to activate your

> > seventeen firewalls and swith

> > ip addresses 1000 times per second ;-)

> >

> > kind regards,

> >

> > Jos ;-)

>

> Don't be silly. I only have fifteen firewalls, and

> everyone knows you can't switch IP addresses quite

> that quickly.

You should use IPv6 for a fifty percent speed gain ;-)

kind regards,

Jos (not: 127.0.0.42, oh now it is, I mean *was* ;-)

JosAHa at 2007-7-21 18:04:24 > top of Java-index,Java Essentials,New To Java...
# 16
> next you'll be telling us your "little brother> borrowed my comp." etcYou sick little brother of course, who's only dream is to be a great java developer ;-P
Aknibbsa at 2007-7-21 18:04:24 > top of Java-index,Java Essentials,New To Java...
# 17

> > next you'll be telling us your "little brother

> > borrowed my comp." etc

>

> You sick little brother of course, who's only dream

> is to be a great java developer ;-P

w.r.t. Gosling that implies that you have to wear funny T-shirts, glasses

and be almost bald-headed except for a little beard?

kind regards,

Jos (< long haired piece of lazy scum ;-)

JosAHa at 2007-7-21 18:04:24 > top of Java-index,Java Essentials,New To Java...