7 Replies - 2950 Views - Last Post: 17 December 2011 - 09:00 PM Rate Topic: -----

#1 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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! :online2long:

Is This A Good Question/Topic? 0
  • +

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

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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) {
Was This Post Helpful? 1
  • +
  • -

#3 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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 :bananaman:
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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

View PostKarel-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"

Must admit that I'd forgotten about this. It's the same shorthand in VB too. Doh on my part!

Thanks for the insight. Cheers! :bananaman:


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

Was This Post Helpful? 0
  • +
  • -

#7 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

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

Posted 17 December 2011 - 08:52 PM

View Postsouth73paw, 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.
Was This Post Helpful? 1
  • +
  • -

#8 south73paw  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1