BackPrevious Page Next PageNext

Formula Syntax

Statements

Declare statement

Expression statement

Assignment statement

If - else statement

Return statement

For statement

While statement

Select statement

Expression

Values in formula expressions

Value data types

Report level global variables

JReport formula syntax is a syntax with simple flow control and other statements. You should follow JReport formula syntax when writing 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 user input and generate different results.

Statements

The statement is the smallest executable unit in the syntax. A formula 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, and return statement.

Declare statement

The declare statement is used to declare variables.

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 variables. The statement calculates the "amount * price" and assigns 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 that depend on the evaluation result of the expression.

Syntax:

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) {
    return "PASS";}
else {
    return "FAIL";}

In this example, if the score is greater than or equal 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 necessary. 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:

The initialization is an expression that initializes the loop - it's executed once at the beginning of the loop. The termination expression 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, increment is an expression that gets invoked after each iteration through the loop. All these components are optional.

Examples:

integer i=0, j=0;
For(i=0;i<100;i=i+1){ 
    j=j+1;
};
return j;

Any time there is a single statement you do not need the curly braces so the following formula works the same.

integer i=0, j=0;
For(i=0;i<100;i=i+1) j=j+1;
return j;

Both examples will return 100 as the result.

integer siteint=0; 
string stite="&_isMultiple_jrs.param$Site=true"; 

if(@P_int==0) then 
stite=stite+"&jrs.param$Site=%07" else { for(siteint=0;siteint<@P_int;siteint=siteint+1) stite= stite+"&jrs.param$Site=" + @P_int }; return stite;

While statement

The while statement is used to continually execute a block of statements while a condition remains true.

Syntax:

Firstly the while statement evaluates expression, which must return a Boolean value. If it returns true, the while statement executes the statement associated with it. The while statement continues testing the expression and executing its block until the expression returns false.

Examples:

integer i=0; 
While(i<100) do {
    i=i+1; 
}; 
return i;

integer i=0; 
While(i<100) do i=i+1; 
return i;

Both examples will return 100 as the result.

JReport also provides another statement that is similar to the while statement, the do-while statement.

Syntax:

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.

Examples:

integer j=0;
Do { 
    j=j+1;} 
while(j<100);
return j;

integer j=0; 
Do j=j+1 while(j<100);
return j;

Both examples will return 100 as the result.

Select statement

The select statement is usually used in the case that the value of a single variable may determine one of a number of different choices.

A select statement is given a variable and compares its value to all cases in the switch; if there is a case that matches the value, all statements in the matching case are executed. If none match, and a default is given, all statements following the default keyword are executed.

Syntax:

select (variable name){
    case expression_1: statement
    case expression_2: statement
    ...
    default: statement
};

The expression_1 and expression_2 should be variables or constants. The statement in each should be a single statement or multi-statement (compound statement). When multi-statement is used, they must be enclosed by {}.

Notes:

Examples:

integer i = 0 ; string j = 'a';
i = @"Customers_Customer ID";
select(i) {
    case 1: j='bb' 
    case 2: j='cc'
    default: j ='dd'
};
return j;

string state="",result="";
if (IsNull(@Customer_State)) then     state="Others" else     state = @Customer_State; select (state) {     case "BC","ID","MT","WA":result="Northwest"     case "CA","AZ","TX","NM":result="Southwest"     case "ME","MA","NH","NY":result="Northeast"     case "FL","NC","GA","SC":result="Southeast"     default: result="Rest of Country" }; return result;

Expression

The expression is a combination of values, operators and functions that produce a result. The value in the expression can be a literal value or a variable, while the operator defines the operation between values; the function is used to perform an action and return a value.

Values in formula expressions

The value specified in an expression can be a literal value or a variable value. JReport formula syntax supports seven data types of value.

Value data types

The following table lists the date types JReport formula syntax supports.

Type Description
Numeric   Integer int 32-bit two's complement Defines a field containing a whole number between -2,147,483,648 and 2,147,483,647. Fractional values inserted into an Integer field are truncated. If specified as NOT NULL WITH DEFAULT, null values will be replaced by zero.
long 64-bit two's complement A data type that holds large integers.
BigInt   Immutable arbitrary-precision integers.
  Real Number float 32-bit IEEE 754 This data type defines an 32bit-IEEE standard 754 floating-point field. Floating-point fields can be defined as single or double (default) precision based on the value of Integer. If the value of integer is between 1 and 21, the type is single precision floating-point, which requires 4 bytes of storage. If the value of integer is between 22 and 53 inclusive, the type is double precision floating-point, which requires 8 bytes of storage. If a field is specified as NOT NULL WITH DEFAULT, null values will be replaced by zero.
double 64-bit IEEE 754 This data type defines an 64-bit IEEE standard 754 floating-point field. Floating-point fields can be defined as single or double (default) precision based on the value of integer. If the value of integer is between 22 and 53 inclusive, the type is double precision floating-point, which requires 8 bytes of storage. If a field is specified as NOT NULL WITH DEFAULT, null values will be replaced by zero.
currency   This data type contains a dollar amount between; -1012 and 1012 and is displayed in a user-defined format.
DateTime   Date   MM/dd/yy MM/dd/yyyy Dates designate a point in time according to the Gregorian calendar. Historical dates may not follow this calendar. The standard input format for this data type is MM/dd/yy or MM/dd/yyyy (automatic conversion is performed between these two formats).
  Time   HH:mm:ss
hh:mm:ss
The Time field data type is defined by the standard input format of HH:mm:ss (using hour in day 0--23) or hh:mm:ss (using in am/pm 1--12).
  TimeStamp   yyyy-MM-dd hh:mm:ss This combination Date and Time data type is displayed in the format yyyy-MM-dd-HH: mm.ss.nnnnnn.
 
Format Description
yyyy An Integer from 0001 to 9999, representing a year.
MM An Integer from 1 to 12 representing a month
dd An Integer from 1 to 31 (maximum depends on the month and year) representing the day of the month.
HH An Integer from 0 to 23, representing the hour in day.
hh An Integer from 1 to 12, representing the hour in am/pm.
mm An Integer from 0 to 59, representing minutes.
ss An Integer from 0 to 59 representing seconds (default 0).
nnnnnn An Integer (up to 6 digits) representing microseconds. If any trailing digit is omitted, 0 is assumed.
Other Types   Char   16-bit Unicode character A single character.
  String     A data type that holds character information.
  Boolean   true or false True or false.

Report level global variable

To control the formula more flexibly, JReport offers you one more global variable scope to operate besides component level global variable. That is report level global variable. You can use "report" as the keyword to define the global variable in a formula. Report level variables are only available to page reports.

For the two levels of global variable, the following definitions will help you distinguish them:

When using the keyword "global" to define a global variable in a formula, if you add no words before the keyword, the global variable is on the component level; if you add "report" before the keyword, the global variable will be a report level global variable.

The following example uses the report global variable to do a calculation in the report. First, create three formulas based on the report global variable as follows:

Then use them in one report including two banded objects (Banded Object 1 and Banded Object 2) using the same dataset as follows:

  1. Insert Formula1, Formula2, Formula3 to the Banded Object 1 (15 records) as follows:

    Banded Header: Formula1

    Detail Panel: Formula2

    Banded Footer: Formula3 - returns 15.

  2. Then insert Formula2 and Formula3 to the Banded Object 2 (25 records) as follows:

    Detail Panel: Formula2

    Banded Footer: Formula3 - returns 40.

Notes:

BackPrevious Page Next PageNext