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

Luis Paulino
PLUS
Luis Paulino
Courses Plus Student 1,779 Points

it says I'm not using the right increment?

I know I'm doing something wrong...but What?

variable_assignment.mm
int mathTotal;
bool isComplete;

for( int mathTotal=5; <=25){
  isComplete=true;
}

1 Answer

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

Hi there! There's a couple of problems here. First, and foremost, your for loop is set up incorrectly. This style of for loop (which is deprecated in Swift 3.0 as I understand it) requires three parts. The start of the variable we're incrementing, the place where we stop the loop, and the value to increment by.

Secondly, our mathTotal is supposed to be a running total. So every iteration we want to add the number that we're incrementing to the current total. For example, when we start mathTotal needs to be set to 5. Then we need to add 6 to that which will give us a running total of 11. Then we add 7 to that to give a running total of 18 etc... all the way up to 25.

Third, isComplete should be set to true only after the loop exits. Currently, your code sets isComplete to true in every iteration.

Here's how I did it:

int mathTotal;
bool isComplete;

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

isComplete = true;

Here we have our declaration for mathTotal and isComplete. We begin our for loop at 5 and go up to 25. Each iteration we increment our counter or i by one. During the loop we take our mathTotal and add the current value of i to it which will give us a running total. After the loop has exited we set isComplete = true.

Hope this helps! :sparkles: