Javagony/Other conditions and loops

Other conditions
We know how to write conditions in Javagony that compare two numbers. What if we want to check for other conditions? Well, this can be done using a Boolean variable, and the function Boolean.compare(b1,true) when b1 is the variable that stores the result of the condition (true or false). It will return 0 if the Boolean variable is true, and -1 if it’s false. She because we already know how to check in Javagony if a number is 0, that means we can check any condition.

Do/while loop
Now that we know how to check if any condition is true, it’s easy to implement an alternative to a do/while loop in Javagony. All we need to do is to create a function that does something and then calls to itself if the certain condition is met.

For the purpose of this book, we will write a code that prints all powers of 3, until we reach a number that its least significant digit is 7. The last number it will print will be 27.

While loop
A while loop will work similar to a do/while, only this time we check for the condition at the beginning of the function and not the end. If the condition is met, we execute the code. If it isn’t, we use the return statement to exit from the function. At the end of the function, after executing the code, it will unconditionally call itself.

Let’s use this knowledge to write a code that receives a positive integer, and prints all the digits of the number - starting from the least significant one, and ending with the most significant.

What if we want the function to return the most significant digit? Well, we can easily modify this code to do so.

Let’s modify this code to return an ArrayList that will include all the digits of the number starting from the most significant and ending with the least significant one.