### 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.

#### 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. See the section Data type for details.

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 use. The declared variable is a real variable and 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.

##### 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.

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

- Component level: Its lifetime is the same as the component's lifetime. In its lifetime, whenever a formula is executed, the variable will be executed if it is used in the formula.
- Report level: Its lifetime is the same as the report's (primary report) lifetime. Even in different components (using same data source), the same report level global variable will have only one instance. The report level variable can be passed to different components (using same data source). The components include banded objects, tables, charts, tabulars, crosstabs, and subreports (including many layers of subreports).

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.

Example

This 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:

Formula1: report global number a =0;

Formula2: a = a+1;

Formula3: return a;

Then use them in one report set including two BandedObjects(BandedObject1 and BandedObject2) in the same dataset as follows:

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

   BandedHeader:Formula1

   DetailPanel:Formula2

   BandedFooter:Formula3 - returns 15.

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

   DetailPanel:Formula2

   BandedFooter:Formula3 - returns 40.

Notes:

- For nested data containers, report level global formulas may lead to an unexpected result. If you add "pagenumber" in the formula's definition, the generated result can be the one expected.
- Although one report can have two or more data sources, report level global variable cannot span across data sources, otherwise the formula will not run correctly, thus, even the report global variables with the same variable name in different data sources are different in one report.
- If there is a report level global variable report global integer g, and it is used both in the primary report and subreport, then this variable g can communicate with each other.
- If you want to calculate the record number of the subreport and primary report separately, you can define a different report level variable from the primary report, but if one subreport is inserted into one primary report more than once you will be unable to correctly use the variable separately, in such circumstance, you can change the report level variable into component level variable to solve the problem.

#### Operator

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, see the section Operators in this chapter.

#### Function

Functions return a value as its result. JReport formula syntax provides many built-in functions. You can refer to the section Built-in functions for detailed information about function.