JReport formula syntax is a syntax with simple flow control and other statements. The large Formula Editor panel is the place where you write your formula statements. You should follow JReport formula syntax to write formulas.
In this syntax, sequence and selection control structure are supported. You can use it to control the execution of the statements. In this syntax, a parameter can be used as variable, and its value can be used in computation or making decisions. Since the value of the parameter is input before the report is run, the report can respond to the user input and generate different results.
Reference Note: If you want to define your own functions by using Java language, please refer to User's Guide | JReport Formulas.
The statement is the smallest executable unit in the syntax. A SQL procedure is a sequence of statements. Statements are separated by semicolon (;). There are many kinds of statements, such as declare statement, expression statement, assignment statement, if-else statement, return statement etc.
- Declare Statement
The declare statement is used to declare variables.
Syntax:VariableType VariableName
Example: String sql;
This statement declares a local String type variable called sql.
Syntax:global VariableType VariableName
Example: global integer i;
This statement declares a global Integer type variable called i.- Expression Statement
The expression statement is used to calculate the value of an expression.- Assignment Statement
The assignment statement is used to calculate the expression on the right side of assignment operator and assign the value to the variable on the left side.
Syntax: Variable = Expression
The type of value of expression must be compatible with the type of the variable.
Example: total = amount * price
In this example, where the total, amount and price are declared variable. This statement calculate the "amount * price" and assign the value to "total".- If - Else Statement
The if-else statement provides conditional control of an action. Normally, the statements are executed one by one sequentially. The if-else statement enables alternative actions depending on the evaluation result of the expression.
Syntax:
if (expression)
{
statement(s)
}
or
if (expression)
{
statement(s)
}
else
{
statement(s)
}
Where, the expression must be a logical expression and have a boolean value. The if clause checks the condition presented by the logical expression. If the condition is true, the statement following the if will be executed. Otherwise, the statement following the else will be executed.
Example:
If (score >= 60)
{
result = "PASS";
}
else
{
return = "FAIL";
}
In this example, if the score is greater than or equals to 60, the result is pass, otherwise the result is fail.- Return Statement
The return statement is used to stop the execution of procedure and return a value if need. The return statement is the last statement executed.
Syntax: return or return expression;
The expression will be calculated before the procedure returns.
Example: return results;- For Statement
The For statement provides a compact way to iterate over a range of values.
Syntax:
for (initialization; termination; increment) {or
statement
}
The
for (initialization; termination; increment) statement
initializationis an expression that initializes the loop - it's executed once at the beginning of the loop. Theterminationexpression determines when to terminate the loop. This expression is evaluated at the top of each iteration of the loop. When the expression evaluates to false, the loop terminates. Finally,incrementis an expression that gets invoked after each iteration through the loop. All these components are optional.- While Statement
The While statement is used to continually execute a block of statements while a condition remains true.
Syntax:
while (expression) {
statement
}
Or
while(expression) statement
First, the while statement evaluatesexpression, which must return a boolean value. If the expression returns true, then the while statement executes thestatement(s)associated with it. The while statement continues testing the expression and executing its block until the expression returns false.
JReport Designer provides another statement that is similar to the while statement - the do-while statement.
syntax:
do {
statement(s)
} while (expression);
Or
do statement while(expression)
Instead of evaluating the expression at the top of the loop, do-while evaluates the expression at the bottom. Thus the statements associated with a do-while are executed at least once.- Select Statement
The select statement is used to conditionally perform statements based on the value of a variable.
Syntax:
select (variable name){
case expression_1: statement
case expression_2: statement
...
default: statement
};
The statement in each should be a single statement or multi-statement (compound statement). When multi-statement is used, they must be enclosed by {}.
Example:
integer i=0, j=0;
select ( i ){
case j: // the case expression is another variable
i=100
case 100: // the case expression is a constant, and the case statement is a compound statement
{
i=i+1;
j=j+2
}
default:
200
};
The expression is a combination of values, operators and functions, that produces a result. The value in the expression can be a literal value or a variable; the operator defines the operation between values; the function is used to perform an action and return a value.
The value specified in an expression can be a literal value or a variable value. JReport formula syntax supports seven data types of value:
- Numeric
- Currency
- Boolean
- String
- Date
- Time
- DateTime
Reference Notes: For detailed information about each data type, please refer to User Reference | Formula Syntax | Data Type.
Literal Value
The Literal Value is a value that is used exactly as it is displayed. A literal value represents a value such as a number, string, or date. For example, "Name", 98.6...Variables
A variable is a named storage unit used to store a value. The identifier (name) of variable can be used to refer to the value of the variable or refer to the storage space of the variable. In an expression, the identifier is used to refer to the value, and in assignment statement, the identifier on the left is used to refer to the storage space.
- Undeclared Variables: The undeclared variables include parameter, DBfield, special field and summary defined in the catalog. They can be used as a variable in a formula.
- Declared Variables: Another type of variable which must be declared in a declare statement before using. The declared variable is a real variable; it can be assigned in the assignment statement. There are two kinds of declared variables:
Local Variable: Variable which is only valid in the formula where it is declared.
Global Variable: Variable which is valid in all the formulas once it is declared.
An operator is a symbol that indicates an operation to be performed on values. The JReport procedure language has several classes of operators, including math, comparison and logical operators. For detailed information about operator, please refer to User Reference | Formula Syntax | Operators in this chapter.
The function returns a value as the result. JReport formula syntax provides many built-in functions. Please refer to User Reference | Formula Syntax | Built-in Functions for detailed information about function.
In JReport Designer, some special fields can be used in formulas, they are
User Name,Print Date,Print Time,Fetch Date,Fetch Time,Modified Date,Modified Time,Record Number, andPage Number. All the referencable special fields are divided into two types according to the time their values are ready. The types arepage levelandconstant level.Page Level
The value of the special field will be ready at the time when the report result is generated. So the page-level special fields include:Fetch Date,Fetch Time,Record Number, andPage Number.Constant Level
The value of the special field will be ready at any time before the engine runs. So the constant-level special fields include:User Name,Print Date,Print Time,Modified Date, andModified Time.In the previous versions of JReport Designer, as long as special fields are used in a formula, the formula is considered to be a page-level formula, and it will be calculated more than one time and filtered at the local machine. Therefore, the performance is slowed down. Now, the formula in which the constant-level special fields are used, is treated as constant-level formula, in this case, this formula will be calculated only one time, as a result, the performance is improved.