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

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: