Jump to content
• Sign Up

# Strange behavior using ternary operator

## Recommended Posts

Can someone shed light on the following problem:

```For \$ix = 0 To 3
\$i = \$ix
\$i1 = (\$i = 1 Or \$i = 2 ? \$i : 0) ; This shows TRUE if \$i = 1     WHY ?
\$i = \$ix
\$i2 = (\$i = 2 Or \$i = 1 ? \$i : 0) ; This shows TRUE if \$i = 2     WHY ?
\$i = \$ix
\$i3 = ((\$i = 1 Or \$i = 2) ? \$i : 0) ; This is OK. It shows always the digits 0, 1 or 2
\$i = \$ix
\$i4 = ((\$i = 2 Or \$i = 1) ? \$i : 0) ; This is OK. It shows always the digits 0, 1 or 2

MsgBox(Default, \$ix, "\$i1: " & \$i1 & @LF & "\$i2: " & \$i2 & @LF & "\$i3: " & \$i3 & @LF & "\$i4: " & \$i4 & @LF, 0)
Next```

UDF: _SingleScript()                                        If you like my post, just click the like button         here -->

#### Share this post

##### Share on other sites

It's because you're not encasing the expression in () e.g. ((\$i = 1 Or \$i = 2) ? \$i : 0)

#### Share this post

##### Share on other sites

Exit,

It looks like an "operator priority" problem to me. The first 2 are actually Oring \$ix = 1/2 and the result of the ternary - which is why you get a Boolean return. Once you add a few more parentheses to determine the evaluation order you get the expected results.

I always use external and internal parentheses when using ternary operations to avoid such problems:

`( ( Expression ) ? ( True result ) : ( False result) )`
Now there is no doubt how AutoIt should parse the content.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...