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

Need Help with Syntax

This works in Xcode. i am wondering why it's not working in this compiler.

variable_assignment.mm
int mathTotal;
bool isComplete;

for (int i = 5; i < 30; i++)
{
  mathTotal++;
}
isComplete= true;

2 Answers

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You only have two things wrong with your code. You need to change 30 to 26 in the second statement in the parentheses of your for loop, because the challenge asks you to add the numbers 5 to 25 to mathTotal using the loop, not the numbers 5 to 29. You also need to change "mathTotal++" to "mathTotal += i". "mathTotal++" increments mathTotal by 1, no matter what the value of i is. Since the challenge asks you to add the numbers 5 to 25 to mathTotal, and i goes from 5 to 25 in the loop, you need to add i to mathTotal, not 1.

int mathTotal;
bool isComplete;

for (int i = 5; i < 30; i++) // CHANGE 30 TO 26
{
  mathTotal++; // ADD i TO mathTotal RATHER THAN INCREMENTING mathTotal BY 1
}
isComplete= true;

I hope this helps!

your code works! and it was a good description thank you !