Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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...

    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 ;