| View previous topic :: View next topic |
| Author |
Message |
abskiz Guest
|
Posted: Fri Dec 12, 2003 7:17 pm Post subject: newbie help loop |
|
|
how do u find out how many times a loop executes?
like
for (int i=8; i<12; i++);
that will execute 12-8+1 tiems right?
|
|
| Back to top |
|
 |
Wendy S Guest
|
Posted: Fri Dec 12, 2003 7:53 pm Post subject: Re: newbie help loop |
|
|
"abskiz" <someone (AT) microsoft (DOT) com> wrote
| Quote: | how do u find out how many times a loop executes?
for (int i=8; i<12; i++);
that will execute 12-8+1 tiems right?
|
Probably the easiest way is to put a print statement inside the loop, and
execute it:
for (int i=8; i<12; i++) {
System.out.println( "i = " + i );
}
The question isn't very clear. Do you want to know how many times the
condition will be evaluated? Or how many times the code in the body of the
loop will be executed?
--
Wendy
|
|
| Back to top |
|
 |
abskiz Guest
|
Posted: Sat Dec 13, 2003 3:18 am Post subject: Re: newbie help loop |
|
|
how many times the code in the body of the loop will executes
"Wendy S" <wendywds (AT) hotmail (DOT) com> wrote
| Quote: | "abskiz" <someone (AT) microsoft (DOT) com> wrote in message
news:KtoCb.14339$aF2.1601848 (AT) news20 (DOT) bellglobal.com...
how do u find out how many times a loop executes?
for (int i=8; i<12; i++);
that will execute 12-8+1 tiems right?
Probably the easiest way is to put a print statement inside the loop, and
execute it:
for (int i=8; i<12; i++) {
System.out.println( "i = " + i );
}
The question isn't very clear. Do you want to know how many times the
condition will be evaluated? Or how many times the code in the body of
the
loop will be executed?
--
Wendy
|
|
|
| Back to top |
|
 |
Brad BARCLAY Guest
|
Posted: Sat Dec 13, 2003 3:49 am Post subject: Re: newbie help loop |
|
|
abskiz wrote:
| Quote: | how do u find out how many times a loop executes?
like
for (int i=8; i<12; i++);
that will execute 12-8+1 tiems right?
|
No, the above example will only iterate 12-8 times. If you want it to
iterate 12-8+1 times, you need to be inclusive of the end range, such as:
for (int i=8;i<=12;i++);
HTH!
Brad BARCLAY
--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Sat Dec 13, 2003 4:01 am Post subject: Re: newbie help loop |
|
|
"abskiz" <someone (AT) microsoft (DOT) com> wrote
| Quote: |
Probably the easiest way is to put a print statement
inside the loop, and execute it:
for (int i=8; i<12; i++) {
System.out.println( "i = " + i );
}
The question isn't very clear. Do you want to know how
many times the condition will be evaluated? Or how many
times the code in the body of the loop will be executed?
how many times the code in the body of the loop
will executes
|
So just place a 'print' statement in the loop body, as Wendy illustrated, or
use a counter variable:
int loopCount = 0;
for (...)
{
...
++loopCount;
...
}
System.out.println("Loop executed " + loopCount + " times");
I hope this helps.
Anthony Borla
|
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Sat Dec 13, 2003 8:23 am Post subject: Re: newbie help loop |
|
|
"abskiz" <someone (AT) microsoft (DOT) com> writes:
| Quote: | how do u find out how many times a loop executes?
|
By counting.
int count = 0;
for (int i=8; i<12; i++) count++;
System.out.println("Executed "+count+" times");
|
|
| Back to top |
|
 |
Paul Thompson Guest
|
Posted: Sun Dec 28, 2003 8:43 pm Post subject: Re: newbie help loop |
|
|
Tor Iver Wilhelmsen wrote:
| Quote: | "abskiz" <someone (AT) microsoft (DOT) com> writes:
how do u find out how many times a loop executes?
By counting.
int count = 0;
for (int i=8; i<12; i++) count++;
System.out.println("Executed "+count+" times");
|
int i;
for (i=8; i<12; i++) loop;
print (i- ;
|
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Mon Dec 29, 2003 10:23 am Post subject: Re: newbie help loop |
|
|
Paul Thompson <pault (AT) HiWAAY (DOT) net> writes:
| Quote: | for (i=8; i<12; i++) loop;
print (i- ;
|
Only works when the incrementor is +1, if you used a loop with i+= 2
or i-- that would not work, while mine would. :)
|
|
| Back to top |
|
 |
|