In programming you often want to repeat a bit of code (a set of instructions for the computer) a certain number of times. Maybe you have data in a list (an array), say a list of five names, and you want to print each of them to the screen. To do that easily, you create a for loop. In simple terms, you tell the computer where to start, where to finish, and how to get from one step to the next. So in English you’d maybe write, “Start at 1 and go to 5, increasing the count by 1 each time.” In programming, though, you’d usually say, “Start and 0 and go to 4.” I won’t explain why programmers start counting at 0 right now.
So in some common programming languages, like C and Java, you’d write
for (int i=0; i<5; i++)
The above code is very common. i is the counter variable, possibly coming from “index” or “integer” (the int declaration states that i is an integer). So i starts at 0 (i=0). It will continue to execute the code as long as i is less than 5 (i<5) and each time the code is executed, i is increased by 1 (i++ is shorthand for i=i+1). So the code will execute 5 times, when i=0, i=1, i=2, i=3, and i=4, but once i=5, it will stop because i is no longer less than 5.
Why i=1?
I didn’t start there. I actually got a degree in Pastoral Studies and served as a pastor for 15 years. But then I felt like it was time to do something different. Thankfully, all the while I was pastoring I had a part-time job where, after being hired for data entry, I ended up programming Microsoft Access. That job has transitioned to where I am full-time and writing in OmniScript, a proprietary script language. I’m enjoying that and am very thankful to have a job in programming in my forties without any formal training, however, I want to learn more. And so for the past year I have been learning more about more popular programming languages. I’m doing that on my own through several great online resources. So this is my i=1.
for (int i=1; i<finished; i++)
No comments:
Post a Comment