I have been working on the book for about 8 weeks and I'm just about to start chapter 7. I've been trying to complete all the assignments and I am noticing that they sometimes will really stretch quite far beyond what is taught in the lesson. This may not pose a problem for those of you with other 'coding' experience but for a beginner it can be downright discouraging. All that being said I hadn't a clue on how to solve the problem from Chapter 6 ex 6 and finally had to peek at the answers that others had posted.

Here is a piece of code from someone's solution that I think I might understand but I'm not sure...

if(!(p%2)) continue;

isPrime = YES;

Could someone please explain this bit of code in simple layman's terms. Here's what I read...

if the result of p / 2's remainder is not 0 then continue on and make isPrime = YES

Can you post some more of that code? The if statement says that if p is evenly divisible by 2, then skip over the remaining statements in the loop but otherwise continue with its execution.

I would like to know which exercises you feel were based on material not taught in the chapter. They may require you to apply the material taught in the chapter in ways that test your understanding, but you shouldn't have to rely on using any language features or constructs that haven't been taught up to that point in the text.

Perhaps before moving onto to chapter 7, I should really review Chapters 1 to 6 in more detail. It might be that I am rushing along too quickly and have not quite mastered the use of the material up until this point. That would explain why I feel that I am having to make a leap in knowledge to answer the more challenging questions.

Here is the rest of the code that I grabbed the original code excerpt from. It came from another student.

Up until this point I have been able to either create the code myself or be able to look at another student's answer and understand not only how the program works but also why they took the approach they did. This one is a bit hard to follow for me and I am having trouble understanding how that ! operator changes the statement's meaningand outcome. When I read it ( if(!(p%2)) ) I understand it now to mean if p / 2's remainder 'is not' equal to zero than the 'continue' statement is triggered and the code that immediately follows the continue statement is skipped and we pick it up at the for loop that follows.

So the first test inside that loop is there to skip past all even numbers (the for statement could have also been written to count by 2's--starting at 3--to avoid this test altogether). If the number is odd, the isPrime flag is set to YES and then the inner loop is executed. The inner loop tests all possible divisors of p (and again, you don't need to test for division by multiples of 2, since we know the number is odd; so this inner loop could also count by 2's). If an even divisor is found, the isPrime flag is set to NO, indicating the number can't be prime. (Here again, once an even divisor is found a break statement could have been executed to terminate that inner loop.) When the inner loop terminates, if isPrime is still set to YES then no divisor was found and so the number must be prime.

The main purpose of this exercise is to get you to understand how to devise an algorithm, or a sequence of steps, to solve a particular problem, and how to translate that into a program. It's not easy for a new programmer to grasp, but a necessary skill to develop to become a good programmer. Don't worry too much at this point if it's still a little fuzzy to you.

What I did Spooky was after a few chapters I kind of skimmed over the Chapter exercises. After I read the whole book I am now in the process of going through it again more carefully and tackling the exercises I glossed over. It helped a bit. Might help a bit for you too.