I would like to say thank you to all the contributing members of the forum. Daniweb has been the result of many late night google searches and has provided some valuable knowledge to myself and I'm sure countless others. I'll start by saying I'm no _real_ programmer. I've played around with scripts for 10+ years, occasionally delving into more intricate languages and efforts. I've managed to hack my way through some unusual circumstances over the years, and am once again trying to further my programming knowledge.

My real question in this thread is about the simple for loop. I'm currently reading a book that expresses the loop such as this:

// Program to visually display a for loop

#import <stdio.h>

int main (int argc, char *argv[]) 
{ 
	int i;
	for ( i = 1; i <= 200; i++ ) 
		printf ("The loop is at %i\n", i); 
	
	return 0; 
}

Yet I will also see the loop also expressed as this in other texts:

// Program to visually display a for loop, again

#import <stdio.h>

int main (int argc, char *argv[]) 
{ 
	int i;
	for ( i = 0; i < 200; i++ ) 
		printf ("The loop is at %i\n", i); 
	
	return 0; 
}

The only difference is that the Initializer and the Conditional are modified. In the first example, it starts at 1 and increments until it reaches 200. In the second, it starts at 0 and increments until it reaches 199, since 200 is not less than 200. Both examples will effectively run identical to each other. This seems to fall under the same category as Indent Style and Programming Style [wikipedia.org]

As more experienced programmers, what are your opinions on this issue. I myself can read both examples the same, but is one generally preferred over the other? I don't want to start developing a bad habit of preferring the wrong one over the other.

Recommended Answers

All 8 Replies

Depends on the circumstances. Most of the time you will want loop counters to start at 0 because all arrays in C and C++ are 0 based, not 1 based. So the index number of the first element of any array is always 0. For consistency sake I always start loop counters out at 0 unless there is a good reason to initialize them with some other number.

A sorting algorithm is a good example of when to initialize the loop counter to something other than 0. Example

int foo(int array[], int nval)
{
    for(int i = 0; i < nval-1; i++)
    {
         for(int j = i+1; j < nval; j++)
         {
             // do something
         }
    }
}

I will comment however that users tend to like things to start at 1 rather than 0.

If I'm asking a user questions, it makes more sense to them to answer questions 1 - 10 rather than questions 0 - 9.

This is however also easily handled at the output point:

for (int ii = 0; ii < 10; ii++)
{
    printf("Question #%d:\n", ii+1);
}

The loop still ran from 0 to 9, making it more compatible with arrays, but the user 'sees' 1 to 10.

For simplicity just want to add that, loops are just loops. The loop statement is used to execute same piece of code number times.

Further its fully up to you to decide how to loop the number of times.

for(int i = 0; i < 2; i++ )
    printf("\nLoop");

OR

for(int i = 1; i <= 2; i++ )
    printf("\nLoop");

Both will fulfill your need. Until you need i variable to be used in the looping statements.

for(int i = 0; i < 2; i++ )
    printf("\nLoop at %d",i);

OR

for(int i = 1; i <= 2; i++ )
    printf("\nLoop at %d",i);

Now both will give different outputs(unlike in previous example).
So when you want some piece of statement to execute number of times which does not depend upon the looping-index. Then both will result same. Otherwise be careful in selecting the correct one as per your requirments.

>Both will fulfill your need.
Though all things being equal, the first is arguably easier to prove correct without giving it much thought beyond knowing your "one past the end" value.

you can really do anything with the loop, you dont even have to declare i a value, you can keep it how it is, and you can set it equal to another variable, its totally up to the programmer. for instance what i learned on here the other day

for( ; fscanf( fin, "%d", &x[i] ) = 1 ; i++ )

will continually read in an unknown number of ints until there are no more to be read

>>for( ; fscanf( fin, "%d", &x ) = 1 ; i++ )

Two things wrong with that:
1) where is variable i declared?
2) need to use operator == instead of = (common mistake)
3) what happens when the value of i exceeds the size of the array x ?

HAHAHA cool catches!!

>Both will fulfill your need.
Though all things being equal, the first is arguably easier to prove correct without giving it much thought beyond knowing your "one past the end" value.

I hate to bump this old thread, but I like Narue's reason.

I think if I'd need to print the value of the variable i to the console in this sequence:

for( i = 0; i < 10; ++i ) {
    printf("%i\n", i);
}

I can just add one to the value before it prints.

printf("%i\n", i+1);

so it is more readable to an end user as Murtan pointed out.

As the programmer, I have full authority to make it do whatever I want, and it seems that everyone will do whatever they are more comfortable with, and there isn't a "correct" way of doing it.

Thanks for all your inputs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.