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

Andre Robinson
PLUS
Andre Robinson
Courses Plus Student 6,057 Points

what am i down wrong

what am i down wrong

variable_assignment.mm
int mathTotal = 5;
 bool isComplete = true ;

for( int mathTotal <= 25; mathTotal++) {

}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

First, it's not necessary to initialize your variables. You just need to declare them. Secondly, your for loop is set up incorrectly. You never tell it the starting point and you will want another variable here. Third, you never set isComplete to true after the loop has run. In fact, it's already marked as completed before you run it because you initialized it that way. Take a look at my solution:

int mathTotal;
bool isComplete;

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

isComplete = true;
Steven Deutsch
Steven Deutsch
21,046 Points

Hey Andre Robinson,

Let me see if I can clear a few thing sup for you. First, your mathTotal and isComplete variables don't need default values. Second, lets take a look at the logic of the loop. Your loop needs 3 things to be set up properly:

1) An Index 2) A condition 3) An incrementor

The index will be of type Int, we'll call it i and assign it a starting value of 5.

The condition will dictate when the loop evaluates the code inside its body. We want this loop to execute while i is less than or equal to 25.

The incrementor will be i++ which is just shorthand for i + 1. This means everytime the loop finishes an iteration, the value of i will increase by 1. The loop will then run again and continue to do so until the condition evaluates as false.

Finally, after this loop finishes we will set the value of isComplete to true by assigning it the value.

int mathTotal;
bool isComplete;

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

Good Luck