For Loop, While Loop, and Recursion

I recently stumbled across a blog post that talked about developers applying for development positions. In this post, the author poked fun at individuals that can’t tell the difference between int and float. Not all programming languages use int or float, so the poking was a bit misplaced.

However, the author did point out an interesting fact – if you apply as a developer, you should be able to write three functions that compute the sum of the numbers in each list using a for-loop, a while-loop, and recursion. This is also a question that may come up in an interview if you are applying for a developer position. As a form of practice and education, I decided to tackle this project using Lua.

First, let’s create a table that contains six numbers totaling 21.

-- There is a total of 6 numbers that are equal to 21.
local numbersList = {1, 2, 3, 4, 5, 6}


For loop

Let’s tackle the for loop first. I almost always use for loops in my projects and this was the easiest of the three to do. I start off declaring a variable, startNum, and set it to the value of 0. Next, I start the for loop at 1 and end at the length of the numbersList table. Inside the for loop, I add each table element to the startNum variable. It all gets wrapped up with a print statement confirming our value of 21.

local startNum = 0
for i=1,#numbersList do
 startNum = startNum + numbersList[i]


While loop

A while loops is like a for loop in that it can iterate over a series of values. To solve this project, I declare the variables i and startNum and set them to 1 and 0, respectively. Then, I set the condition for the while loop to numbersList[i]. This will return if there’s an element in the i position and when there’s not, the loop will break. Inside the loop, I add the element to the startNum variable and increment i by 1. To my knowledge, Lua doesn’t provide an increment shortcut like i++. We finish by confirming our value using the print statement.

local i = 1
local startNum = 0

while numbersList[i] do
 startNum = startNum + numbersList[i]
 i = i + 1



Before we dive into recursion, I’d like to put out a disclaimer that I got some recursion help from other online resources by evaluating C and Java. Armed with my newly found online knowledge, I built the following script. The script below does not use the numbersList table that we’ve previously declared.

This situation asks for recursion by calling a function within the same function. I created a function called addup() that accepts one parameter. Within the function, I use an if/then statement to determine whether the parameter is greater than one and here’s where it gets a bit tricky. I return the value of n plus the value of n-1. On the first go around, it’s 6 + 5. Then, 11+4, 15+3, 18+2, 20+1 for a total of 21. When n reaches 0, we leave the addup function and print out 21. Here’s the full code.

local function addup(n)
 if n >= 1 then 
  return n + addup( n - 1 )
 return 0

This example will only work if you are adding or subtracting. If you decided to change the return statement to multiplication or division, the end result will be 0.

Recursion is a tricky subject, especially if you’re new to programming. As follow up reading, I would recommend reading this plain English explanation and the Wiki article. If you have questions or comments, please let me know. Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *