Re: for puzzlement

new topic     » goto parent     » topic index » view thread      » older message » newer message

Given that looping constructs are very common in most programs, the idea of having the loop variable automatically declared and visible only within the loop is a good one. It prevents accidentally "clobbering" a variable by the same name declared elsewhere in your program. That can create a bug that can be very difficult to track down.

The reason the loop variable is (often) named i, j, k, l or similar is partly tradition (from Fortran, perhaps?) and partly common sense: more often than not, the loop variable is going to be used within the loop, perhaps to access an item in an array. It's just easier, quicker, and neater to code:

for i = 1 to 100 do 
   something[i] = something_else[i][2] 

than:

for my_var_1 = 1 to 100 do 
  something[my_var_1] = something_else[my_var_1][2] 

And, of course, this also means you have to invent fewer unique variable names. This is not a minor consideration when you are writing large programs.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu