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

Mia Pivirotto
Mia Pivirotto
5,557 Points

Unable to get answer for Code Challenge in Review of Scope and Loops of Objective C

I've been working on this code for awhile and even skipped it, but I don't know if its because I don't comprehend the question or what I'm missing. But the question states:

Create a for loop that will begin with a value of 5 and end with a value of 25. In each iteration, add the incrementing value to mathTotal. When the loop has finished running, set 'isComplete' to true. (HINT: the last value used INSIDE the loop should be 25)

But the way I can think of doing it returns with a final number for mathTotal of 26. I'm not sure how to manipulate the code to stop at 25 without actually doing the math to figure out what number to make the condition for the program to stop running.

With this code I'm getting a compiler error, and when I try with mathTotal<=25 as the conditional statement in the for loop I get the error of "better check your syntax!"

variable_assignment.mm
int mathTotal;
bool isComplete;

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

isComplete = true;
Mia Pivirotto
Mia Pivirotto
5,557 Points

Okay, so I figured it out that I just had to change the code in the {} to mathTotal+=i, but now I find I just don't understand the code challenge.

We're starting with i being equal to 5, and then adding that to mathTotal, which starts out at 0 and incrementing i up one, so it would then be equal to 6 after one go around. But why are we exiting the loop when i equals 25 and mathTotal equals 315, when the question doesn't seem super clear to me that it's talking about exiting the loop when i is 25. Are you only allowed to use 'i' in the beginning parenthesis statements of the code? I know this is a question of semantics..