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

Rodrigo Alarcon
Rodrigo Alarcon
8,028 Points

Please help, what am i doing wrong

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)

variable_assignment.mm
int mathTotal;
bool isComplete;
for (int = 5; int < 26; int++) {
  isComplete = true;
}

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

int is the type of your mathTotal variable and not the variable itself, so you can't assign values to the type. Moreover int is a reserved keyword, so you can't use it as a variable name either. Having said that, the syntax of your for loop is not correct. It rather works like this, specifically in this case:

for (int indexVariableName = intitalValue; indexVariableName < maxValue; indexVariableName++) {
   // indexVariableName is available in this scope
}

Moreover, the assignment asks you to "In each iteration, add the incrementing value to mathTotal", which is not included in your code. This is how you could do that:

int mathTotal;
bool isComplete;

for (int i = 5; i < 26; i++) {
  mathTotal += i; // In each iteration, add the incrementing value to mathTotal.
  isComplete = true;
}

Hope that helps :)