Control Flow Statements
Java supports the usual control flow statements if-else, for, and while. You can use the Language Elements tool to insert template code corresponding to a number of control flow, of or block, statements.
The following examples illustrate some of the most common uses of control flow statements.
The IF-ELSE Statement
This is an example of a general if-else statement:
if (a < b) {
  alert("Value too small.");
} else {
  alert("Value is just right.");
}
Between curly braces {} you can include multiple lines of code, each terminated with a semicolon. If you only need one line of code, such as in the example above, this shortened syntax is available:
if (a < b)
  alert("Value too small.");
else
  alert("Value is just right.");
The For Statement
Java supports several different types of for statements. This example uses the perhaps most conventional syntax:
// Iterate i from 1 to N:
int N = 10;
for (int i = 1; i <= N; i++) {
  // Do something
}
In an alternative syntax the loop is over all form objects in a list of form objects:
for (FormObject formObject : app.form("form1").formObject()) {
  if ("Button".equals(formObject.getType())) {
    formObject.set("enabled", false);
  }
}
where the local iteration variable looped over is formObject of the type, or class, FormObject. The collection of objects, in this case app.form("form1").formObject(), can be an array or other types of lists of objects. Using this syntax, the iteration variable loops over all entries in the collection, from start to finish.
The While Statement
This example shows a while statement.
double t = 0, h = 0.1, tend = 10;
while(t < tend) {
  // do something with t
  t = t + h;
}
For a more advanced example of a while statement, see Creating and Removing Model Tree Nodes.
Note that Java also supports do-while statements.
The With Statement
When writing methods in the Method Editor, in addition to the standard Java control flow statement, there is also an optional with statement, specific to the Method Editor, that can be used to make Application Builder code more compact and easier to read (you enable this in File > Preferences). A simple example is shown below:
// Set the global parameter L to a fixed value
with(model.param());
  set("L", "10[cm]");
endwith();
The code above is equivalent to:
model.param().set("L", "10[cm]");
In this case using the with statement has limited value since just one parameter is assigned but for multiple assignments readability increases. See Parameters and Variables for an example with multiple assignments.
Note that the with statement is only available when writing code in the Method Editor. It is not available when using the COMSOL API for use with Java®. You can turn off the use of with statements in the section for Methods in Preferences.
The method descr returns the variable description for the last parameter or variable in a with statement:
with(model.param());
  set("L", "10[cm]");
  String ds = descr("L");
endwith();
Assuming that the parameter description of the parameter L is Length. The string ds will have the value Length.
Exception Handling
An exception is an error that occurs at runtime. The Java® programming language has a sophisticated machinery for handling exceptions and each exception generates an object of an exception class. The most common way to handle exceptions is by using try and catch, as in the example below.
double d[][] = new double[2][15];
try {
  d = readMatrixFromFile("common:///my_file.txt");
} catch (Exception e) {
  error("Cannot find the file my_file.txt.");
}
where an error dialog is shown in case the file my_file.txt is not found in the application file folder common.
To inform the user of the underlying cause you can use an additional input argument to the error method, as shown in the example below.
double d[][] = new double[2][15];
try {
  d = readMatrixFromFile("common:///my_file.txt");
} catch (Exception e) {
  error("Cannot find the file my_file.txt.",e);
}
This can be used to “wrap” native COMSOL Multiphysics error messages with custom error messages. See the Java® documentation for more information about using try and catch.