# How to create this forloop

• (2 Pages)
• 1
• 2

## 15 Replies - 1392 Views - Last Post: 09 October 2012 - 05:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294754&amp;s=77f5c8c57410e3c4977c8a31cab06035&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 Skydiver

• Code herder

Reputation: 6116
• Posts: 21,052
• Joined: 05-May 12

## Re: How to create this forloop

Posted 09 October 2012 - 05:33 PM

Do you have any guarantees of the following?

Quote

end == begin + change * k
where
k >= 1

If so. You can just swap begin and end and flip the sign of change if begin is greater than end. Remember that multiplication is commutative: 1 * 2 * 3 == 3 * 2 * 1.

If you really need to numbers to go from begin to end, then you can probably set up a loop that breaks out after the loop variable has become equal to end and the appropriate computation has been done. Remember that the condition for a for loop need not be the loop variable. So you can do something like:
```for(int i = begin; true; i += change)
{
do work with i
break out of loop when i equals end
}

```