#### Archived

This topic is now archived and is closed to further replies.

# loop values and compile time

## Recommended Posts

Prod    134
I was wondering if the values for loops are calculated at compile time. In my database program, the first thing it does is load the number of records and then loop through that number to load the information.For the loop to work correctly i have to "hard code" in the value for the number of records. For example (of my problem) :
example code:

fin >> numOfRecords;
printf("%i", numOfRecords); /*prints the correct number of records*/

for (int i = 1; i < numOfRecords; i++)
printf("%i", i); /*never stops incrimenting*/


[edited by - prod on April 29, 2003 11:16:44 PM]

##### Share on other sites
Colin Jeanne    1114
Loop values are calculated at runtime.

Qui fut tout, et qui ne fut rien

##### Share on other sites
petewood    819
If something is ''hard coded'' the value can''t change. Your value can change.

I can''t see how your code is incorrect. It also works fine on my machine (if that''s anything to go by!). I just changed fin to cin.

##### Share on other sites
Zipster    2365
numOfRecords might be a bad value due to either the file being loaded incorrectly or corrupt. In that way it would appear to loop forever, when in reality it's going to a really high limit. Output is extremely slow, take away the printf and it should run to the limit rather quickly, assuming the loop isn't optimized away and my theory was correct

[edited by - Zipster on April 30, 2003 4:02:26 AM]

##### Share on other sites
flangazor    516
I think the OP is confusing macro loop limits with variable loop limits.

#define MAX_STRING_LENGTH 100

for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char

is different from

unsigned int MAX_STRING_LENGTH = 100;

for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char

The first one is compile time (before compile time, actually). The second is at runtime.

##### Share on other sites
petewood    819
quote:
Original post by flangazor
for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char

with that semi-colon you won''t be doing anything for each char