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

Can't complete this "for" loop

Not sure what I'm doing wrong here? I know I'm missing something, just not sure what....

variable_assignment.mm
int mathTotal;

bool isComplete;

for (int mathTotal = 5; mathTotal++; mathTotal == 25;) {
    iscomplete = true;
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

There's a few things. The isComplete value should be set to true only after the loop has completed running. The way you have it it keeps setting it to true with every iteration. Secondly your increment operator and the number of times you're running through the loop are in the wrong places. And, you're going to need a separate variable to keep track of the number of times you're going through the loop. Take a look at how I solved it and see if it makes sense.

bool isComplete;
int mathTotal;

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

isComplete = true;