The "indexing starts from zero" actually comes from how memory is accessed in c/c++ programs(actually it comes from assambly but the same reasons still aply). In c/c++ and a lot of other languages the following two lines of codes are functionally identicalneoc wrote: ↑Sat Sep 22, 2018 6:16 pmAt first I felt like any language that doesn't use zero-based indexing is just bad and I wouldn't want to use it, but after a while you realize that it's really no big deal. For example if you switch from zero-based to 1-based in an indexing loop, you only have to change the comparism operator:darkfrei wrote: ↑Sat Sep 22, 2018 9:36 amI found that indexing from 1 is much easier for real practice, but normally I don't use the index, just the values of elements. But here can be also "a-b-c" indexing, when I want it.WeirdConstructor wrote: ↑Fri Sep 21, 2018 6:55 pm Yes, the weird 1 based indexing and ~= are warts, and they hurt experienced programmers like me. But on the other hand JavaScript and Python have even more warts if you look closely.
Code: Select all
for(i = 0; i < 10; ++i)
Once you get used to it, it's just a context switch in your thinking.Code: Select all
for(i = 1; i <= 10; ++i)
Code: Select all
print(table[i])
Code: Select all
print(table+i)
The zero based index comes from a purely practical reason of not having to do extra steps or waste memory on padding. This was especially important back in the 80's and early 90's when computers were slow and memory was expensive.
Also two dimensional arrays become simpler with zero based indexing. The memory location for an array[xsize][ysize] is array+xindex+yindex*xsize. When a 1 based index array it becomes array+(xindex-1)+(yindex-1)*xsize unless you add an xsize+ysize+1 worth of padding to the array.
The cost of nonzero based indexing is nonzero.