How to decrement by more than one in a C for loop?

Page 1 of 1

7 Replies - 14820 Views - Last Post: 17 December 2011 - 09:00 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=260271&amp;s=56e2bea8742c83f85f29350041d36b75&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 south73paw

Reputation: 3
• Posts: 88
• Joined: 06-October 08

How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 08:37 PM

Hi

I'm a beginner to C as a precursor to learning Objective-C although I've coded in VB .NET and (a long time ago) Javascript and ActionScript.

My exercise is to count backwards from 99 to 0 outputting a message if the number is divisible by 5. I'm able to do this, but can only decrease the counter by one each time. How can I get it to count down in 3s, for example? I tried adding
```i = i - 3
```
after if statement, but it seemed to take off 4 - I'm guess i-- was being included to??

My code so far is:
```int main (int argc, const char * argv[])
{
//counter
int i;
//count down from 99 to 0
for (i = 99; i >= 0; i--) {
//output value of counter
printf("%d \n", i);
//check if value of counter is divisble by 5
if (i % 5 == 0) {
//output if number divisible by 5
printf("Found one! \n");
//break this iteration
continue;
}
//decrease counter by 3 each time
i = i - 3;
}
return 0;
}
```

Thanks for the help!

Is This A Good Question/Topic? 0

Replies To: How to decrement by more than one in a C for loop?

#2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

Re: How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 08:40 PM

(i = 99; i >= 0; i-=3) {

#3 south73paw

Reputation: 3
• Posts: 88
• Joined: 06-October 08

Re: How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 08:43 PM

Thanks!
Can't believe I missed that....
Doh!

Cheers

#4 Karel-Lodewijk

Reputation: 454
• Posts: 864
• Joined: 17-March 11

Re: How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 08:43 PM

"i = i - 3" is correct, the reason it decreases by f4 is because the loop also decreases i by one.

```for (i = 99; i >= 0; i--)

```

You can avoid this by just putting nothing there

```for (i = 99; i >= 0;)

```

Or decrease by 3 right there in the loop.

```for (i = 99; i >= 0; i = i - 3)

```

You can also use the shorthand "i -= 3", which is the same as "i = i-3"

This post has been edited by Karel-Lodewijk: 17 December 2011 - 08:44 PM

#5 jimblumberg

Reputation: 5447
• Posts: 16,972
• Joined: 25-December 09

Re: How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 08:43 PM

Your counter is being decremented in the for loop and also your i = i - 3; line. If you want the loop to decrement the counter by three then do it in the for loop:
for (i = 99; i >= 0; i-=3) You should never really change this value outside of your loop construct.

Jim

This post has been edited by jimblumberg: 17 December 2011 - 08:45 PM

#6 south73paw

Reputation: 3
• Posts: 88
• Joined: 06-October 08

Re: How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 08:51 PM

Karel-Lodewijk, on 17 December 2011 - 08:43 PM, said:

"i = i - 3" is correct, the reason it decreases by f4 is because the loop also decreases i by one.

Yes, figured that's what was happening.

You can avoid this by just putting nothing there

```for (i = 99; i >= 0;)

```

Ok, didn't realize you could remove some of the items in the first line of the for loop

Or decrease by 3 right there in the loop.

```for (i = 99; i >= 0; i = i - 3)

```

I had been putting just i = 3, which caused an error. See the problem now.

You can also use the shorthand "i -= 3", which is the same as "i = i-3"

Thanks for the insight. Cheers!

Quote

for (i = 99; i >= 0; i-=3) You should never really change this value outside of your loop construct.

Jim

So add the step value in the first line of the for loop only.
It seems to be tidier that way.

Cheers!

This post has been edited by south73paw: 17 December 2011 - 08:49 PM

#7 Karel-Lodewijk

Reputation: 454
• Posts: 864
• Joined: 17-March 11

Re: How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 08:52 PM

south73paw, on 17 December 2011 - 08:48 PM, said:

Ok, didn't realize you could remove some of the items in the first line of the for loop

You can remove all items in the for loop if you want to.

```for (;;)/> {}

```

Will just loop infinitely.

#8 south73paw

Reputation: 3
• Posts: 88
• Joined: 06-October 08

Re: How to decrement by more than one in a C for loop?

Posted 17 December 2011 - 09:00 PM

[/quote]

You can remove all items in the for loop if you want to.

```for (;;)/> {}

```

Will just loop infinitely.
[/quote]

Interesting...
Thx