Javagony/Other conditions and loops

Other conditions

edit

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.

public class More
{
   public static void main(String[] args)
   {
      boolean b1=7.5/5>1; //replace with another condition
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
         
         System.out.println("Condition is false");
      }
      catch(Exception IO)
      {
         System.out.println("Condition is true");
      }
   }
}

Do/while loop

edit

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.

public class More
{
   public static void main(String[] args)
   {
      loop(1);
   }
   
   public static void loop(int n)
   {
      System.out.println(n);
      boolean b1=n%10==7;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
         
         loop(n*3);
      }
      catch(Exception IO)
      {
         
      }
   }
}

While loop

edit

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.

public class More
{
   public static void main(String[] args)
   {
      loop(1234567890);
   }
   
   public static void loop(int n)
   {
      boolean b1=n<1;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
      }
      catch(Exception IO)
      {
         return;
      }
      
      System.out.println(n%10);
      loop(n/10);
   }
}

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

public class More
{
   public static void main(String[] args)
   {
      System.out.println(loop(45454365));
   }
   
   public static int loop(int n)
   {
      boolean b1=n<10;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
      }
      catch(Exception IO)
      {
         return n;
      }
      
      return loop(n/10);
   }
}

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.

import java.util.ArrayList;

public class More
{
   public static void main(String[] args)
   {
      System.out.println(loop(3874897));
   }
   
   public static ArrayList<Integer> loop(int n)
   {
      boolean b1=n<10;
      
      try
      {
         int temp=Boolean.compare(b1,true);
         temp=1/temp;
      }
      catch(Exception IO)
      {
         ArrayList<Integer> a=new ArrayList<Integer>();
         a.add(n);
         return a;
      }
      
      ArrayList<Integer> a=loop(n/10);
      a.add(n%10);
      return a;
   }
}