Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics Scope and Loops Review Scope and Loops

Ryan Maneo
Ryan Maneo
4,342 Points

It compiled fine...

I don't understand why this isn't working in the Code Challenge... It works perfectly fine in Xcode... it prints 5 through 24 and stops once isComplete is == to true...

variable_assignment.mm
    int mathTotal;
    bool isComplete;

   for (int n = 5; n <25; n++) {
        n = n + 1;
        if (mathTotal <= 25) {
            isComplete = true;
        }
        NSLog(@"%i", n);
    }

1 Answer

Stone Preston
Stone Preston
42,016 Points

The task states Create a for loop that will begin with a value of 5 and end with a value of 25. In each iteration, add the incrementing value to mathTotal. When the loop has finished running, set 'isComplete' to true. (HINT: the last value used INSIDE the loop should be 25)

you need to loop from 5 up to and including 25. Thats the first thing wrong with your code. You need to use <= 25 not < 25

you also need to add n to mathTotal, not increment n by 1

then since the last value of the loop is going to be 25, we test if n == 25 and if it is we set isComplete to true. You tested if mathTotal <= 25 which is not what the task asked for.

int mathTotal;
 bool isComplete

for (int n = 5; n <= 25; n++) {

    mathTotal = mathTotal + n;

    if (n == 25) {
     isComplete = true ;
    }

}