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

How do i set a value to true ,after a loop, in objective-c?

The exact request is, 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 is finished running, set 'isComplete' to true. I need help with the statement park of the for loop.

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

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Alex,

There are a few issues going on with your code:

  • First, are some spacing issues inside of the for conditions. A couple are just for readability, but one is a syntax error. The one that will result in an error is the incrementer. There cannot be a space between the variable and the ++.

  • Next is the adding of the values of the incrementor. Right now you are trying to add it to the boolean variable isComplete. Also, with just a plus sign, you are performing a mathematical function instead of adding the new increment to the existing value. This needs a += But, the big thing is the values need to be added to the mathTotal.

  • Finally is changing the boolean to true. This has to be done outside of the loop after the loop has finished its iterations. This is simply done by assigning a value to the already declare variable.

Below is the corrected code for you to review. Have a look, and if it doesn't make sense, I suggeted you review the video before moving on, as these are fairly common and integral in Objective-C.

int mathTotal;
bool isComplete;

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

isComplete = true;

Keep Coding! :)

:dizzy: