Beginning Java for Xojo Programmers V

Before we get going on the next section perhaps its time to step up our Java IDE to something more than just a text editor.
Something like VS Code, on macOS, works quite well and gives you a debugger and several other nice capabilities.
While not a full blown graphical WYSIWYG IDE like Xojo is for Java its a darn sight nicer than just a text editor.

There ARE other IDEs that get a lot closer to a drag and drop WYSIWYG IDE for Java.
But I’m going to be using VS Code for now.

Loops

One of the most common things to do in a program, besides making comparisons, is to have some way to run some code over and over.

Since we’re focusing on users that already know Java,or Xojo, that want rot know how to read/write the syntax of the other we’ll skip introductions.
Both languages have FOR loops.
Some variation exists between them but for common usage they are very similar.

    for i as integer = 0 to 99              for (int i = 0; i < 100 ; i++ ) {
      // loop body                            // loop body
	next                                    }

both of these loops will run from 0 to 99
a few things to note

  1. neither requires you declare the loop variable, i, as part of the loop itself. it can be defined before hand

  2. Xojo requires you define the upper & lower bound of the loop as part of the loop. Java does not.
    For instance, while the following appears very similar it will work differently

    dim i as integer = 100                  int i = 100
    for i = 0 to 99                         for (; i < 100 ; i++ ) {
      // loop body                            // loop body
	next                                    }

In Xojo, because it requires you to put bounds for the loop it always initializes the loop variable at the start of the loop.
So in this case the Xojo code would run from 0 to 99. In java you can skip the initializer and, in this case, the loop would never execute since i starts with a value of 100.
The for loop is extremely flexible in Java. In this case the Xojo equivalent would be a while loop like

    dim i as integer = 100                int i = 100
    while i < 100                         for (; i < 100 ; i++ ) {
      // loop body                            // loop body
      i = i + 1
	wend                                  }

In general a Java for loop is structured like

	for ( _initializer statements_ ; _loop termination condition_ ; _increment statements_ )

Note that in Java you can have MANY statements that make up the initialization, condition, and increment
Its not terribly common but it is possible
For instance this is perfectly legal

	for (int i = 0, j = 90 ; j > 0 && i < 100 ; j-=3, i++ ) {
		System.out.println("i=" + i + " j=" + j ) ;// loop body 
	}

The parts are separated by a ;

In Xojo you would need to use a while loop for the above for loop

	dim i as integer =  0
	dim j as integer = 90
	while j > 0 and i < 100 
		print("i=" +str(i) + " j=" + str(j) ) // loop body 
		j = j - 3
		i = i + 1
	wend

Java & Xojo both have a variation of the FOR statement - the FOR EACH statement

   dim listOfIntegers() as integer = Array(1,2,3,4,5,6,7,8,9)     int[] listOfIntegers = { 1,2,3,4,5,6,7,8,9};

   for each item as integer in listOfIntegers                     for (int item : listOfIntegers ) {

        print( "item = " + str(item)                                System.out.println( "item = " + item) ;

   next                                                           }

And in each language you can use different types

dim listOfStrings() as string  = Array( "a","b","c","d","e")      String[] listOfStrings = { "a","b","c","d","e"} ;
For each item as string in listOfStrings                          for (String item : listOfStrings) {
  Print("item is: " + item)                                         System.out.println("item is: " + item);        
next                                                              }

Both language have a WHILE statement that is very similar.
In each, the loop continues as long as the condition is true.

dim i as integer = 0                                              int i = 0 ;
While (i < 10 )                                                   while (i < 10 ) {
  Print("i is: " + Str(i))                                          System.out.println("i is: " + i);   
  i = i + 1                                                         i++ ;
Wend                                                              }

There really isnt too much to say about either - they are so similar.

Both languages also have a DO loop form.

dim count as integer = 1                                          int count = 1;
Do                                                                do {
  Print("Count is: " + Str(count))                                  System.out.println("Count is: " + count);
  count = count + 1                                                 count++;
Loop Until count >= 11                                            } while (count < 11);

There are some notable differences here.
First off notice that the condition check between Java and Xojo is different.
Java checks for the condition it needs to CONTINUE running the loop.
Xojo checks for the condition that EXITS the loop.

So be careful when you more code that uses do loops.
This can be a sbtle but significant difference.

The other major difference is that Xojo has a form that puts the checks at the top of the loop - much like a while loop
Java doesnt have this style

dim count as integer = 1                                          int count = 1;
Do until count => 11                                              while (count < 11) {
  Print("Count is: " + Str(count))                                  System.out.println("Count is: " + count);
  count = count + 1                                                 count++;
Loop                                                              } 

And Xojos do loop can have the condition at both the top and bottom of the loop.dim count as integer = 1

Do until count => 11                                       
  Print("Count is: " + Str(count))                         
  count = count + 1                                        
Loop until count >= 11

I’ve never encountered this form but it is legal and, if you ever do, really requires a while loop in java with another check at the end of the while loop to exit the loop early.

3 Likes