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

I am stuck at this question, please could you assist me with the answer

Please could you let me know what the answer is to this question as I cannot figure it out thank you

variable_assignment.mm
int mathTotal;
bool isComplete;
for (int = 5; i=25; i++) {
  if(isComplete=25){
    break;
    isComplete=TRUE;
  }
}

3 Answers

Elena Willen
PLUS
Elena Willen
Courses Plus Student 3,268 Points

Hi Kathleen,

You forgot to add the incrementing value to mathTotal. Also in the for loop, make sure to declare i (int i = 5), and remember the difference between "=" (assign a value) and "== or <=" (compare values). You don't need to break if your loop declaration is correct.

(and for this line:

if(isComplete=25)

I am not sure what you try to achieve but you can't assign an int value to a bool and if you want to compare, it's better to compare like this

if (isComplete == true)

than using an int as Xcode suggest

"Comparison of constant 25 with expression of type 'bool' is always false")

Hope I clearly explained. Let me know if you need further help. :)

I think I sort of understand what you mean, but still can't seem to get the correct answer for the question: 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)

The first question asked me to create int mathTotal; bool isComplete;

Elena Willen
PLUS
Elena Willen
Courses Plus Student 3,268 Points

You declare a for loop like this:

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

You increment the value with this operation:

            mathTotal += i;

And you set 'isComplete? to true like this:

        isComplete = true;

Of course, you add the incrementing value to mathTotal inside the loop.

Hope it helps :)