Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Why do I get a compile error when using special conditional IF statement (a == b)? (c): (d)?

Why do I get a compile error when using special conditional IF statement (a == b)? (c): (d)?

Problem:

Why do I get a compile error when using special conditional IF statement (a == b)? (c): (d)?

Resolution:

QUESTION
----------------------------------
Why do I get a compile error when using special conditional IF statement (a == b)? (c): (d)?

ANSWER
----------------------------------
SilkTest supports this special IF construct, however since the question mark character (?) is implemented as an operator and not as function this special IF construct can not be used as shown below. It will return compile errors from the SilkTest compiler.

(a == b) ? (c): (d)

Therefore, you would need to assign the result of the expression to a variable or use it within a function to work with it.

Any of the following approaches will overcome compile errors.

In the following example we compare a dot (.) delimited string. We compare the second element of the string, if it is blank the result is "ABC.com" otherwise its "ABC".

[ ] Print((GetField("ABC.net",".",2)=="") ? ("ABC"+".com") : ("ABC")) 


or

[ ] String test = (GetField("ABC.net",".",2)=="") ? ("ABC"+".com") : ("ABC")


Additional Information:
As explained above, although the expression (a == b) ? (c): (d) cannot be used in the 4Test editor, when using expression evaluator in the debug mode you can use this statement without problems.

Steps to run debug mode
1. Select [Run] | [debug] from SilkTest menu.
2. Press F9 or select [Debug] | [Run]
3. Select [View] | [Expression]. Expression dialog will appear.
4. Copy the above expression in the text field and press "Enter".

The reason why it works in expression evaluator is because it evaluates expressions using the variables of the current scope, it does not work with an allocation like c = a + b but will calculate a + b and print the result.

Old KB# 20918

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:15
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.