rares985
rares985

Reputation: 391

How to loop for a fixed number of iterations?

Is there any way of making a for loop iterate a fixed number of times, even though the right side of the interval might be increasing ? I want to do this without declaring an additional variable to use as a copy of the initial inc. For example:

for (i = 0; i < inc; i++)
{
  if (condition) 
  {
     inc++;
  }
}

I am pretty sure that if inc increases, the for will execute more than inc - 1 times. How can I iterate exactly inc times, without using a copy of inc ?

Upvotes: 1

Views: 1698

Answers (2)

user3062260
user3062260

Reputation: 1644

I think you are possibly looking for a 'range', you don't say what language its for but in python something like:

 inc = 2
 for i in range[0:10]:
      if i > inc:
           inc=inc+1
      print inc

Upvotes: 0

Floris Velleman
Floris Velleman

Reputation: 4888

for (i = inc; i > 0; i--) {
    if (condition) {
        inc++;
    }
}

This would work since you only assign inc once.

Upvotes: 2

Related Questions