Javagony/For loop

Introduction
As noted in the preface, Javagony does not allow any loops. In this chapter you will learn the alternative for a for loop in Javagony. We will be creating a recursive function that will provide the functionality of the following for loop:

You could write i++, however I have written i+=1 on purpose to demonstrate that you can write also i+=2 or any other number you want. We will be using recursion as well as knowledge from the previous chapter to emulate the functionality of this loop.

Implementation
Notice that we have reused code from the previous chapter, Comparing 2 integers to check which one is greater.

This function gets three values: firstNum, lastNum, and step. For this section, assume that step is a positive number. We will discuss negative values of step in the next section of this chapter. This code in regular java can be written as following:

First the function prints the value firstNum. This is in order to show that it iterates over all the numbers that a regular for loop would. You can replace it with the code you want running. Then we increase firstNum by step, and use the knowledge we gained in the previous chapter to check if firstNum is lower than lastNum.

Notice that I wrote firstNum+1 instead of just firstNum. This is because I want firstNum to be less that lastNum, but this method checks if firstNum is less or equal to lastNum. If firstNum+1<=lastNum then we can conclude that firstNum<lastNum. In this code we are only working with integer numbers. Omitting the +1 will result an Javagony version of the following regular Java code:

If firstNum (which at this point contains the value of the next number) is less than lastNum, then we proceed to the next iteration.

If step<0
Let’s now change the values given to the function in the main method to 0,-10,-1 and observe how the code will work.

If we run the code now, we will see that it prints the following output: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10

But this is not what we want. We don’t want the code to iterate over the last value. Let’s check how the code runs to see how to fix it.

The code stars with the variable firstValue as 0 and prints it. Then it increases the variable by -1, which is the same as subtracting 1 from it and we get -1.

Now, we get to the comparing. The value of lastNum (in this case -10) is subtracted from the maximum value that is possible to store in an int. This is the same as adding 10. One would expect this to raise an error, but instead an integer overflow will occur, and we will get a negative number.

Then, using the method forFunc which does not allow for integer overflow we add the value of firstNum+1 (in our case the result is 0) to the large negative number, and if an integer overflow does not occur, we proceed to the next iteration.

What will happen after the code prints -9? Well, it will change the value of firstNum to -10, and then check if -9 plus this large negative number will result in an integer overflow. However, it does not and goes one to iterate on i=-10.

How to fix it? Well, we can fix it by omitting the +1, but then we will have the same trouble with step>0. We can have two functions, one that omits the +1, and one that doesn’t.

The first one will be a replacement of the following loop if step=1:

and if step=-1:

The second one will be a replacement of the following loop if step=1:

and if step=-1:

Now, you have got all the options covered, you just need to figure out which one is right for you in the next production app you write, since from now on you will write codes only in Javagony. Not really.