It depends what you are counting to and from:
Code:
for (int i = 0; i < 10; i++) // Start counting at 0, loop 10 times
for (int i = 1; i < 10; i++) // Start counting at 1, loop 9 times
for (int i = 10; i > 0; i--) // Count backwards from 10, loop 10 times
It's useful to start at zero because 'for' loops are often used for looping through arrays, which are zero based, but it honestly depends entirely on your need for the program.
Oh, I see, I was wondering at first how you said your examples above loop 10 times, 9 times, and 10 times, I was originally looking at the "iteration" or expr 2 in your above example, and thinking that the 10 meant it would loop 10 times, and the 0, zero times. But I see now that the amount of times the loop loops (if that makes any sense) is actually the difference between the expr1 and the expr2. From 0-10, then from 1-10, then from 10-1. I get it.
My problem was this...
"Write a program that prompts the user to enter a number n, then prints all even squares between 1 and n. For example, if the user enters 100, the program should print the following."
4
16
36
64
100.
At first I tried to think of how to "on paper" establish a pattern that would go from 4 to 16 to 36, etc. I didn't think there was any way I could increment any variable by 1, like i++, to get there. I don't know of any way to increment a variable by any amount greater than 1, like i+++, or i++++. Then, as I was trying to go to sleep, it came to me (long time since I've been in a math class, so give me a break) I just needed to go from 2 to 4 to 6 to 8 to 10, etc because the squares of even numbers are all even numbers, and consecutively as well. So I figured out that I'd want to do something from 2, then add 2, then add 2, etc.
I wasn't sure how to represent my thinking in terms of code, so I cheated on this one. The book has just a couple of the programs in the exercises on it's website already worked out, and this was one of them.
I felt more comfortable looking at the completed code as a tutorial or example that I could study and learn from. Every once and I while a construction worker has got to see a completed house to serve as a guide and see the big picture of what he's there to accomplish.
I got to thinking the other day..... as I'm new at this, I really haven't seen any code in my entire life, except the few examples in each chapter. Imagine being given a task to build something that you've never seen or used before, given all the tools and parts and supplies, being told to put it all together to make what's in the picture! Only to have been shown a photo or two of what your finished product should look like? Pretty difficult. That's what I've been doing through 6 chapters so far. Trying to use what I learned in the chapter, to build something I've never seen before and was completely unfamiliar with.
So with this particular problem, I took a different route. I decided to take a walk through the completed house, and examine it's build, structure, and layout, go through the front door, then out the back, then in through the back and out the front, familiarizing myself with what I'm being asked to build chapter after chapter. I think it will help me with future problems. Let's hope so!
Thanks, here's the code...
Code:
#include <stdio.h>
int main(void)
{
int i, n;
printf("Enter limit on maximum square: ");
scanf("%d", &n);
for (i = 2; i * i <= n; i += 2)
printf("%d\n", i * i);
return 0;
}