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, One of the Challange Task

Hey All:-) what is the correct code for this task? 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).

For me I think its the problem with the interpretation of question rather than coding mistake...

Jason Anders
Jason Anders
Treehouse Moderator 145,863 Points

We will need to see what you have coded, in order to help you troubleshoot. Please post your code here (Refer to the Markdown Cheatsheet for instructions on how to post code.

:dizzy:

3 Answers

Hi, Megha. First off I will encourage you to make use of the Get Help and Help buttons from within the section you are currently having questions about. This will add a link to the current course and section you are working in which provides more context for people that want to answer your questions. Also, always post any code you have already written so we can see where exactly you are in solving the challenge yourself.

You stated that you were having an interpretration issue. So, I'm gonna assume you already understand some basics already. However, I'm still going to walk through each step to make sure it's clear what the challenge is asking. Now, moving on to your question. Assuming you are at this challenge, and at task 2, let's say you've done this much

int mathTotal;
bool isComplete;

You're first objective now is to create a for loop that begins with a value of 5 and ends with a value of 25. Here's a for loop's structure

for (/* Instantiate local variables*/ ; /* Condition to keep looping. */ ; /* End of loop expressions */) {
    // Do something.
}

Now let's implement what the challenge is asking of the for loop

// Create a for loop that will begin with a value of 5 and end with a value of 25.
for (int i = 5; i <= 25; i++) {
    // Do something.
}

You can see that we've created a local variable i that is assigned the initial value of 5. We then added a condition to keep looping while i is less than or equal to 25. Lastly we added an expression to increment the value of i after each iteration.

The challenge now asks "In each iteration, add the incrementing value to mathTotal". To do this, we will add a line of code within the loop body

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

Lastly, it asks for you to set isComplete to true when your for loop has finished running. So, let's do that and add everything else from above together to see what the final product should be

int mathTotal;
bool isComplete;

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

Hope this helped you out!

Hey:-),

thnx Addison. It did work.

Earlier when I was trying to solve it, I was doing it this way:

int mathTotal , i ; bool isComplete;

for(i = 5 ; i <= 25 ; i++) { mathTotal = mathTotal + i; if(i == 25) { isComplete = true; } break; }

Overanalyzed the question... :-D

though it worked when I did it ur way but I hv a question... Aren't mathTotal = mathTotal + i ; and mathTotal += i ; same things ? Well, it didn't accept the answer when I used the upper one! Sorta Confused...

It does give you the same result. The problem you're seeing might be because you're evaluating mathTotal before it is initialized. This can be fixed by refactoring your declaration at the top of the file

// Change this
int mathTotal;

// To this
int mathTotal = 0;

This initializes mathTotal with a value of 0.

ohkeyyy...

So using mathTotal += i; We are kinda tricking compiler into thinking that we are assigning it a value doing some computations on "i" without using mathTotal hence initialization for mathTotal is not needed while actually we are using mathTotal as well...

Is it?