Can't subtract in a for loop in C/Objective-C

Posted by user1612935 on Stack Overflow See other posts from Stack Overflow or by user1612935
Published on 2012-09-27T03:29:47Z Indexed on 2012/09/27 3:37 UTC
Read the original article Hit count: 140

Filed under:
|

I'm going through the Big Nerd Ranch book on Objective-C, which takes you through some early C stuff. I've played with C before, and am pretty experienced in PHP.

Anyhow, I'm doing the challenges and this one is not working the way I think it should. It's pretty simple - start at 99, loop through and subtract three until you get to zero, and every time you get a number that is divisible by 5 print "Found one." Pretty straightforward. However, subtracting by three in the for loop is not working

#include <stdio.h>

int main (int argc, const char * argv[])
{

    int i;
    for(i = 99; i > 0; i-3){
        printf("%d\n", i);
        if(i % 5 == 0) {
            printf("Found one!\n");
        }
    }
    return 0;
}

It creates and endless loop at 99, and I'm not sure why.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about c