Home > Archive > Java Help > September 2004 > How do I count the test steps in a for loop
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How do I count the test steps in a for loop
|
|
| Big Daddy 2004-09-27, 3:57 am |
| I need to know how to count the test steps in a for loop for a project.
I think it should work if the count is done in a method that the test is
compared to but can't get it to work.
for( int i =0; i < n || countTest(); i++)
countTest(){
countCompare++;
return true;
}
What am I doing wrong?
| |
|
| On Mon, 27 Sep 2004 11:21:08 +1000, Big Daddy wrote:
> I need to know how to count the test steps in a for loop for a project.
> I think it should work if the count is done in a method that the test is
> compared to but can't get it to work.
>
> for( int i =0; i < n || countTest(); i++)
>
>
> countTest(){
> countCompare++;
> return true;
> }
>
> What am I doing wrong?
I usually don't like doing other people's homework but... ;-)
Make your || an && and swap the operands (countTest() && i<n). Go see:
http://java.sun.com/docs/books/tuto...relational.html
to find out why (hint: "conditionally evaluates op2").
Cheers,
--
Sean
Age and treachery will beat youth and skill any day
| |
| Oscar kind 2004-09-27, 3:57 am |
| Big Daddy <bigdoglinux@yahoo.comm> wrote:
> I need to know how to count the test steps in a for loop for a project.
> I think it should work if the count is done in a method that the test is
> compared to but can't get it to work.
>
> for( int i =0; i < n || countTest(); i++)
>
>
> countTest(){
> countCompare++;
> return true;
> }
>
> What am I doing wrong?
The countTest() method isn't called until i >= n; then it is called each
iteration. The loop loops forever.
What you meant to do is to call the method each time the test "i < n"
succeeds, but not otherwise. You need one of the shortcut operators for
that (that was a good choice of you), but you need the "and" shortcut
operator:
a || b -> Evaluate a; if it is false, evaluate b; return the logical OR
a && b -> Evaluate a; if it is true, evaluate b; return the logical AND
--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website
PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
| |
| Stefan Schulz 2004-09-27, 3:57 am |
| On Mon, 27 Sep 2004 01:21:08 GMT, Big Daddy <bigdoglinux@yahoo.comm> wrote:
> I need to know how to count the test steps in a for loop for a project.
> I think it should work if the count is done in a method that the test is
> compared to but can't get it to work.
>
> for( int i =0; i < n || countTest(); i++)
>
>
> countTest(){
> countCompare++;
> return true;
> }
>
> What am I doing wrong?
Two things:
1) You are abusing the operator. Why not move the call to countTest()
into the
body of the loop?
2) If you insist on abusing an operator, pick the one that works. && in
this case.
--
Whom the gods wish to destroy they first call promising.
|
|
|
|
|