Decisions decisions decisions
I’ve never encountered a program that didnt have to make decisions
And every programming language has ways to make decisions - to compare and take action depending on the results of that comparison.
There are, in many programming languages, many ways to make these decisions.
Java and Xojo have ones that are similar.
If statements are in both. Xojo has select case. Java uses switch.
And both have an inline if statement.
In Xojo , and java to the right, an if statement might look like
dim something as boolean = false boolean something = false ;
if something = true then if (something == true)
print("something is true") System.out.println("something is true") ;
else else
print("something is false") System.out.println("something is false") ;
end if
In this simple example you’ll notice a couple things.
First Xojo uses = for both assignment AND comparison depending on the context.
Java uses = for assignment , and == for comparison.
This difference sometimes gets people in trouble because they write
if (something = true)
which assigns the value true to something and then checks if the result of doing that was true (which it would be)
So be careful about making sure which one you use or are reading. Its very subtle.
You may also see Java code that does
if (true == something)
because if the developer accidentally wrote
if (true = something)
the compiler would complain since you cannot assign to TRUE.
A longer example illustrates more differences, specifically how you do many statements in the branches of an IF statement
dim something as boolean = false boolean something = false ;
if something = true then if (something == true) {
print("something is true") System.out.println("something is true") ;
print("and this is line 2 of the true part") System.out.println("and this is line 2 of the true part") ;
else } else {
print("something is false") System.out.println("something is false") ;
print("and this is line 2 of the false part") System.out.println("and this is line 2 of the false part") ;
end if }
The big difference here is in the Java code where you need the { } to mark blocks of code to run in the true and false portions.
They always match up in pairs to enclose any block.
Another even longer example illustrates more differences, specifically with IF THEN ELSEIF style statements.
We’re going to change the type of the something variable just so things compile and we can make a decent comparison
dim something as integer = 2 int something = 2 ;
if something = 1 then if (something == 1) {
print("something is 1") System.out.println("something is 1") ;
print("and this is line 2 of the 1 part") System.out.println("and this is line 2 of the 1 part") ;
elseif something = 2 then } else if (something == 2) {
print("something is 2") System.out.println("something is 2") ;
print("and this is line 2 of the 2 part") System.out.println("and this is line 2 of the 2 part") ;
elseif something = 3 then } else if (something == 3) {
print("something is 3") System.out.println("something is 3") ;
print("and this is line 2 of the 3 part") System.out.println("and this is line 2 of the 3 part") ;
else } else {
print("something is " + str(something)) System.out.println("something is "+something) ;
print("and this is line 2 of the " + str(something) part") System.out.println("and this is line 2 of the "+something+" part") ;
end if }
Java uses the same style of IF ELSE regardless of whether the statement has 1 ELSE, ELSE IF etc.
Xojo on the other hand differentiates between ELSEIF and ELSE IF resulting in more deeply nested statements if you use ELSE IF when you meant ELSEIF.
Functionally though it should not matter. Just the Xojo IDE will indent your code differently depending on which you use. And you will need a lot more END IF’s than if you use ELSEIF.
Just be aware of Javas different use of == vs =
The other common statement used to make decisions is the Xojo SELECT CASE statement. For this Java uses SWITCH.
There are some subtle but important differences.
dim something as integer = 2 int something = 2 ;
select case something switch ( something ) {
case 1 case 1 : {
print("something is 1") System.out.println("something is 1") ;
print("and this is line 2 of the 1 part") System.out.println("and this is line 2 of the 1 part") ;
}
break ;
case 2 case 2 : {
print("something is 2") System.out.println("something is 2") ;
print("and this is line 2 of the 2 part") System.out.println("and this is line 2 of the 2 part") ;
}
break ;
case 3 case 3 : {
print("something is 3") System.out.println("something is 3") ;
print("and this is line 2 of the 3 part") System.out.println("and this is line 2 of the 3 part") ;
}
break ;
else default : {
print("something is " + str(something)) System.out.println("something is "+something) ;
print("and this is line 2 of the " + str(something) part") System.out.println("and this is line 2 of the "+something+" part") ;
end select }
}
Again, java uses () around the expression in the SWITCH statement where Xojo doesnt.
Java uses the { } to enclose all the cases where Xojo the initial select and end select statements.
One significan difference is that at the end of each CASE Java uses BREAK to cause the code to jump out of switch statement at that point.
Otherwise Java would continue executing the rest of the statements.
This has pros & cons.
In java you can do
switch ( something ) {
case 1 : {
System.out.println("something is 1") ;
System.out.println("and this is line 2 of the 1 part") ;
}
case 2 : {
System.out.println("something is 2") ;
System.out.println("and this is line 2 of the 2 part") ;
}
case 3 : {
System.out.println("something is 3") ;
System.out.println("and this is line 2 of the 3 part") ;
}
default : {
System.out.println("something is "+something) ;
System.out.println("and this is line 2 of the "+something+" part") ;
}
}
and if something is 1 all statements from case 1, 2, 3, and default will execute.
If its 2 then all statemente from 2, 3, ande fefault will execute.
So you can use this to avoid duplicating code that might be common between cases.
Xojo doest do this so its easier to manage the code but you can end up duplicating code.
One limitation of the SWITHC statement in Java is that it isnt quite as versatile as the one in Xojo.
It can ONLY be used with certain types (see The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics))
so writing something like
int something = 3 ;
switch ( true ) {
case (something == 1) : {
System.out.println("something is 1") ;
System.out.println("and this is line 2 of the 1 part") ;
}
break ;
case (something == 2) : {
System.out.println("something is 2") ;
System.out.println("and this is line 2 of the 2 part") ;
}
... etc ...
doesnt work in Java so you’re likely to see long IF statements in java code where Xojo might use a select case.
Note that in the Java examples each of the CASEs had all of the code to execute enclosed in a { } pair. While not strictly required its not uncommon to see.
Both Xojo & Java have an INLINE IF operator.
In Java this is the ternary you may have seen in code that looks like ?:
int something = 3 ;
System.out.println(" something is > 3 is " + ((something >3) ? "true" : "false")) ;
Note that the entire expression (something >3) ? "true" : "false"
needs to be enclosed in () for println to treat it as an expression that can be converted to a string.
Xojo’s version of this looks like
var something as integer = 3
print " something is > 3 is " + if(something >3,"true","false")
There are some differences in how these behave.
In java the types of the IF and ELSE portions of the trinary do NOT have to be the same type.
int something = 31 ;
System.out.println(" something is > 3 is " + ((something > 3) ? 45 : "false")) ;
will compile & work. the integer & string results can both be converted to a string for println and so there is no issue.
Xojo’s IF operator requires that they be the same type, or compatible types. For instance
var something as integer = 13
print " something is > 3 is " + if(something >3,45,"false")
will not compile as 45 and false are not compatible or even convertable into any common type.
So be careful about this sort of difference in Java’s ?: operator and Xojo’s IF()