Flow of control
Overview
The J language has very few control structures. Those it does have are as follows:
· the conditional structure (if)
· the sending of a message structure
· set structures (select, while, diffusion and anonymous methods)
Conditional structure (if)
Conditional
structure syntax takes a classic form:
if (condition1)
{
// instructions
}
or with the "else" instruction:
if (condition1)
{
// instructions
}
else
{
// instructions
}
or with the
"else if" instruction:
if (condition1)
{
// instructions
}
else if (condition2)
{
// instructions
}
else
{
// instructions
}
Example
For any
class of a given package and for any navigable association from this class, the
example below is used to generate a descriptive sentence for document purposes. The sentence built depends especially on the multiplicity of the
associations.
Note: The "describeAssociations" command on a package executes this program.
Package:describeAssociations()
{
OwnedElementClass Scanning of all the package's classes
{
PartAssociationEnd.<describeAssociation(Name);
}
} End of example method
AssociationEnd:describeAssociation (in String className)
{
AssociationEnd currentAssociation = this;
String oppositeClassName;
// Describe only navigable associations
if (! IsNavigable()) return;
RelatedAssociation.<ConnectionAssociationEnd
{
if (currentAssociation != this)
{
oppositeClassName = OwnerClass.Name;
}
}
StdOut.write(NL, "Thanks to the", Name, "association, a representative of", className, "is linked");
if (MultiplicityMin == "0")
{
StdOut.write(" optionally to");
}
else if (MultiplicityMin == "1")
{
if (MultiplicityMax != "1")
StdOut.write ("to at least one representative and to
at most");
}
else
StdOut.write(" to at least ", MultiplicityMin,
"representatives and to at most");
if (MultiplicityMax == "1")
if (MultiplicityMin == "1")
StdOut.write ("to one and only one representative
of",
oppositeClassName,".");
else
StdOut.write ("at most one representative of",
oppositeClassName,".");
else if (MultiplicityMax == "*")
StdOut.write("an unlimited number of representatives
of",
oppositeClassName,".");
else
StdOut.write(" ",MultiplicityMax, "representatives of",
oppositeClassName,".");
Result
When this processing
is applied to the classes of a package, the following sentences are produced.
Thanks to the
ContractualLink association, a representative of Insurance is linked to one and
only one representative of
Thanks to the Direction association, a representative of Man is linked optionally to an unlimited number of representatives.
etc.
Boolean expressions
Boolean
expressions are all sorts of expressions combining boolean values (variables,
attributes, or a method's return value).
For more
details on the boolean class, please see "Basic classes".
The "for structure" syntax
The "for structure" syntax takes a classic form:
for( initialization; condition; increment)
instruction;
or
for( initialization; condition; increment)
{
// instructions
}