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 arrona
luis arrona
5,885 Points

everytime i try to add the for loop, the task one stops working

this is my code

int mathTotal; BOOL isComplete;

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

2 Answers

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

You're reasonably close, but things are sort of misplaced and you have two closed curly braces but only one open curly brace. And you have two open parentheses but only one closed parentheses. Note that when it says a previous task has stopped working, it generally means that the current code you're trying has introduced a syntax error. Here's what it's looking for:

int mathTotal;
bool isComplete;

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

isComplete = true;

We start the loop with i equal to 5 and then we go all the way up to 25. Every time through the loop we increment i by one. We take mathTotal and add what it is currently plus i. So the first time through the loop it'll be 5 because mathTtoal isn't initialized to anything. The next time through we'll add 6 so the result will be 11. The next time through we'll add 7 to the previous value (11) and get 18. This happens all the way up until i is greater than 25. At that point the loop will exit. When it does, we set the value isComplete to true. Because... it's complete! :)

Hope this helps!

luis arrona
luis arrona
5,885 Points

thank you, finally got it to work