The Conditional Operator ? :
Making Decisions in a Report: Conditional Programming – Asking “If” in the ReportWriter
The most powerful capability is the ability to embed decision making (“IF” Statements) into report column Expressions. The ?: operator gives you the ability to include decision making into your column expressions.
How does the conditional ?: operator work?
A ? B : C can be read as “If A is true, then do B, otherwise do C” – or simply: IF A, then B, else C
A – the true/false test. This is contents of the ‘decision’ to be made. The “A” part of the “A ? B : C” must contain some kind of relational operator and must evaluate to a true or false answer.
E.g. 15 > 5
E.g. row.field == parameter
E.g. row.addressType == ‘Guardian 1’ (if the addressType field is set to ‘Guardian 1’)
B is the value that is “returned” if A is true. (That is, what to do when A is true)
C is the value that is “returned” if A is false. (That is, what to do when A is false)
Here, true and false are what are known as “Boolean Constants” – they are Boolean values, not “text strings” – that is, they are more like numbers (2, 42, 867, …) than words.
When using the conditional operator, all three parts, A, B, and C must all be present. You cannot leave any part out.
What happens when the conditional operator is evaluated?
Step 1: The test in PART A is evaluated
Step 2: If the test evaluates to true, PART B is evaluated If the test in PART A evaluates to false, PART C is evaluated
The overall ‘value’ of the “?:” operator is determined by whether it is PART B or PART C that gets evaluated – that is, by whether PART A is true or false.
For example, if you are filling in a form and wish to use a gender word or pronoun (e.g. he/she, him/her, his/hers, son/daughter), you can use a gender test to select the word to use:
(row.gender == “Female”)? “daughter” : “son”
This expression tests the contents of the “gender” column against the word “Female”.
If there is a match, “daughter” is returned. If there is no match, “son” is returned.
You can do the same test multiple times in different circumstances:
(row.gender==”Female”)? “She” : “He” (row.gender==”Female”)? “hers”: “his”
(row.gender==”Female”)? “her” : “him”
(row.gender==”Female”)? “girl” : “boy”
This conditional, decision making operator is a very powerful tool that makes many new reports possible.
Nesting Decisions within Decisions - Stringing Decisions Together
You are not limited to one “decision”: You can ‘nest’ these tests to be make to make decisions based on prior decisions:
(row.gender==”Female”)? “She” : (row.gender==”Male”)? “He” : “They”
(row.gender==”Female”)? “hers”: (row.gender==”Male”)? “his” : “theirs”
Here is one where both B and C have nested conditionals:
(row.gender==’Female’) ? (row.age <16)? ‘girl’ : ‘young woman’) : (row.age <16) ? ‘boy’ : ‘young man’)
In this example, B and C are themselves complete conditional expressions:
- A is (row.gender==”Female”)
- B is (row.age<16)? “girl” : “young woman”)
- C is (row.age><16) ? “boy” : “young man”)
- This will print “girl” if the data in the row has: gender=”Female” and age less than 16.
- It will print “young woman” if the data in the row has: gender = “Female” and age greater than 16.
- It will print “boy” if gender != “Female” and age is less than 16.
- It will print “young man” if gender != “Female” and age is greater than 16.
There is no limit to how many ‘nested’ conditional expressions you can use.
Any part of A ? B : C can be substituted with another full A ? B : C expression: A ? (a ? b : c) : (d ? e : f) where "B" has become "(a : b ? c)" and "C" has become "(d ? e : f)".
These substitutions can go on indefinitely. There is no limit to the number of 'nested' conditionals you can embed, as long as each of them is correctly formed.
This decision making capability is very powerful: it will enable many things described in the rest of this document.
Decision Making: Testing Various Types of Data
The result of any test is either true or false: All decisions in ReportWriter are either true or false. The decisions themselves can include any of the basic data types:
- Boolean Fields– In the ReportWriter Expressions all Boolean fields are tested with the true and false "Boolean constants" To test if a Boolean field is set to true, compare it to true. To test if a Boolean fieldis set to false, compare it to false. This is NOT how Booleans work on the "Filters" screen of the report. On the Filters screen, Booleans must be tested against Y for true and N for false. But in Expressions themselves, the Boolean constants true and false must be used.
- E.g. ${row.isRaceAsian == true ? …. ß This is true if “isRaceAsian” is set to true.
- E.g. ${row.specEd == false ? ß This is true if “specEd” is set to false.
- Boolean Functions- A "boolean function" is one of the Expression Language functions that tests if something is true. For example: rw: contains. This tests whether a field or a parameter contains another String. rw:contains(row.streetName, "Ln") - tests whether the street name contains "Ln". Boolean functions must be tested with the Expression Language's built-in constants true and false.
- E.g. ${rw:contains(row.streetName, "Ln") == true ? .... }
- E.g. ${rw:startsWith(params.myParam, "Y") == false ? .... }
- A test for "true" can also be omitted because the function itself returns "true" or "false": ${rw:startsWith(params.someParam,"Y") ? ....} This implicitly tests for "true". Numbers –
- Numbers can be compared in the usual ways: with the standard set of comparative operators:
- Greater than: > or gt
- Greater than or equal to: >= or ge
- Less than: < or lt
- Less than or equal to: <= or le
- Equal to: == or eq
- Not equal to: != or ne
- Expression Result Columns- A column can contain just a test - without also making a decision using the outcome of the test. These columns can then be used as Boolean value in other Expression columns.
- ${1 > (4/2)} <--- this will have the value false
- ${4.0 >= 3} <--- This will have the value true
- ${100.0 == 100} <--- this will have the value true
- ${(10*10) ne 100} <--- 'ne' means "not equal" and this will have the value false
- ${'a' < 'b'} <--- This will have the value true because the numeric value of the literal 'a' is less than the numeric value of the literal 'b'
- ${'hip' gt 'hit'} <--- 'gt' also means Greater Than and the numeric value of the literal 'hip' is less than the numeric value of the literal 'hit' ('hi' is equal to 'hi' but 'p' < 't') so this is false
- ${4 > 3} <-- This has the value true
- Dates –
- toDate(“mm/dd/yyyy”) -- converts the “mm/dd/yyyy” to a real DATE that can be used to compare with a DATE column.
- rw:dateCompare(date1, date2) - This compares two dates and gives a number depending on the relationship of the two days:
- If date1 < date2 this returns 1 (Date2 is after Date1)
- If date2 == date2 this returns 0 (Date 1 and Date2 are the same day)
- If date1 > date2 this returns -1 (Date2 is before Date1)
- NOTE: the ‘Type’ of the data that is returned from this function is an Integer.
- Testing the result of the compare: ${dateCompare(today,params.selectedDate) == 1? “not yet” : dateCompare(today,params. selectedDate)==0 ? “today” : “too late”}
- Strings – The "String" data type means "character string" or plain text.
- Contains(value,testValue) – returns true if the testValue is contained by the value. ${rw:contains(row.fullName,'Zinn')}
- startsWith(value,testValue) – returns true if the value starts with the testValue ${rw:startsWith(row.lastName,'Z')}
- endsWith(value, testValue) – returns true if the value ends with the testValue § ${rw:endsWith(row.lastName,'inn')}
- Empty Columns -- Columns are themselves a form of data and it is necessary to be able to determine if a Column has some data in it or whether it is empty ('Empty' means the field has nothing in it - it was never set - or is expressly set to a String with nothing in it):
Expression | Result |
${!empty param.myParam} | False if the parameter myParam is null or the empty string |
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article