# How to increment the result of a calculation inside a loop

Page 1 of 1

## 5 Replies - 3135 Views - Last Post: 18 November 2012 - 03:10 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=300664&amp;s=cbb0340590a371ae38593d1a5b189214&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sizza

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 18-November 12

# How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 06:53 AM

Hi,
Basically i have a loop, and i need to increase z by 30 every time y-x is greater than or equal to 1000.
X and Y are inputted by the user
the value of Z depends on the difference of x and y,
the loop only works when Y is greater than or equal to X
If there is another way to do this without loops, I'd be happy to consider it.

so for example

4500 - 4000 = 500, so Z = 0
4500 - 3500 = 1000, so Z = 30
2000 - 947 = 1053, so Z = 30
3500 - 1000 = 2500, so Z = 60
4000 - 900 = 3100, so Z = 90
10000 - 500 = 9500, so Z = 270

the answer should be the same for most C languages,
this is what i have - sometimes works but is very hit and miss:

```//x and y already inputted
double z = 0;
while (y >= x)
{
y = y - x;
if (y >= 1000)
{
z+=30;
}

}

```

but it doesn't work, even though I've been told it should work
any help?

Is This A Good Question/Topic? 0

## Replies To: How to increment the result of a calculation inside a loop

### #2 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 07:18 AM

At what times do you see your loop missing?

Personally, I wouldn't use a loop. I would use some simple integer division and multiplication.
Was This Post Helpful? 0

### #3 Sizza

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 18-November 12

## Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 08:12 AM

Well, I input 147 as X, and 2560 as Y,
Z should come out as 60, but it comes out as 300 instead.

also if X= 0 and y = anything > x the program just hangs in the loop and doesn't break.
really confusing piece of code
Was This Post Helpful? 0

### #4 Sizza

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 18-November 12

## Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 08:45 AM

```sub = Y - X;

if (sub >= 1000 && sub < 2000)
Z += 30;
if (sub >= 2000 && sub < 3000)
Z += 60;
if (sub >= 3000 && sub < 4000)
Z += 90;
if (sub >= 4000 && sub < 5000)
Z += 120;

```

There has to be a more practical way than this,
i need to deal with any numbers that are input
Was This Post Helpful? 0

### #5 JackOfAllTrades

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 10:58 AM

Have you heard of the modulus operator?
Was This Post Helpful? 0

### #6 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 03:10 PM

```double z = (((int)Math.Abs(x - y)) / 1000) * 30;
```

If you just care when y > x (instead of the difference no matter which is larger) then use

```double z = y > x ? ((int)(y-x)) / 1000 * 30 : 0;
```

This post has been edited by Momerath: 18 November 2012 - 03:13 PM

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }