Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrement a For Loop?

I can increment a FOR loop in xcode, but for some reason the reverse, namely decrementing, doesn't work.

This incrementing works fine, of course:

for (int i=0; i<10; ++i) {
    NSLog(@"i =%d", i);
}

But, this decrementing doesn't produce a thing:

for (int i=10; i<0; --i) {
    NSLog(@"i =%d", i);
}

I must have the syntax wrong, but I believe this is correct for Objective C++ in xcode.

like image 588
Michael Young Avatar asked Nov 06 '11 00:11

Michael Young


Video Answer


1 Answers

I think you mean > instead of <:

for (int i = 10; i > 0; --i) {

If you want the values of i to be the same as in the original code except in reverse order (i.e. 9, 8, 7, ..., 1, 0) then you also need to change the boundaries:

for (int i = 9; i >= 0; --i) {
like image 70
Mark Byers Avatar answered Sep 28 '22 06:09

Mark Byers