• Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing a loop  RSS feed

 
scott hanson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question in my book poses is:
what is reasonable test data for a loop that does not execute a fixed number of times.
This chapter deals with boundry conditions,extreme codtions,equivalence classes and truth tables.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't loop a "fixed number of times" then you must have some other way to decide when you're done. In fact, Java doesn't really have syntax to loop a fixed number (like 3) of times, but to loop until some condition is true or while some condition is true.
A very common loop is:

The first bit "int i = 0" is initialization. This is done once before the loop. The second bit "i < someVar" is the condition. If the condition is false at the start, we won't even go once through the loop. It is tested again after each time through the loop to see if we should go again. The third bit "i++" is an action to take right before testing again.
This kind of integer and increment stuff is very common, but not required. You could write

A completely different bit of syntax is

This just tests the condition without the initialization and update bits.
Does any of that sound like the kind of thing you were looking for?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, all three parts of for(init;condition;increment) can be omitted if you want to...
If you initialise the loop iterator elsewhere, you don't have to do it again.
If you omit the end-loop condition your loop runs forever unless interrupted by a break.
Omit the increment and it will run forever unless you increment the iterator elsewhere in the loop.

yields an eternal loop unless specifically terminated

is a common way to process List elements in sequence.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!