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

Mazen Halawi
Mazen Halawi
7,806 Points

Objective-C Exercise Not Working

Im not sure why it says its wrong, it keeps saying "Check your Syntax" i applied it in a project and it worked.

variable_assignment.mm
int mathTotal;
bool isComplete;

for (int i = 5; i <= 25; i++) {
  mathTotal++;
  if (i == 25) {
    isComplete = TRUE;  
  }
}
Mazen Halawi
Mazen Halawi
7,806 Points

Nevermind i got the solution:

int mathTotal = 0;
bool isComplete;

for (int i = 5; i <= 25; i++) {
  mathTotal += i;

  if (i==25) {
    isComplete = TRUE;    
  }
}

1 Answer

Hi Mazen

I'm glad you got to a solution for the challenge. You could be more simplistic in your code; rather than testing for i == 25 just set the boolean outside of the loop. The question asks you to set the boolean to true once the loop has finished, (which amounts to the same thing as that means i == 25), but means you don't run the i == 25 test 21 times unnecessarily:

int mathTotal;
bool isComplete;

for(int i = 5; i <= 25; i++){
  mathTotal += i;
}

isComplete = true;

I hope that helps a little - it's just a different take on the same solution.

Steve.

Mazen Halawi
Mazen Halawi
7,806 Points

yeah you are right about that, it got in when i was attempting different approaches to solve the question and i guess it stayed there but originally i put it outside like you did.

cheers!