ternary_operation

Questions about the BASICtools and MakeItC
Post Reply
olzeke51
Posts: 394
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

ternary_operation

Post by olzeke51 »

I'm having problems with the ternary operator/function
Your example showed :: ch = IF(ch > = "a", ch AND &H5F, ch) ' this is a crude upshift
ARE there limitations on what thetrue expression, false expression can perform
I'm using ARMbasic 9.34g Basictools 5.37 on an '812 with kernel 8.33a
'
this function style compiled::
GV_timer = IF (state_GV_timer, min_20, min_V)
'
I error out like the following [whether I put a space (or not) - to the " +=1" operator]
I have also tried to do some variable assignments [eg. timer = 300 ] in the expression but got errors also
'
state_load_switch = IF(in(17) = 0, state_load_switch +=1,state_load_switch)
-ERROR C:/Coridium/Basic/Projects/812_hvac_relay_brd02.bas: 137: Expected Operand, found =1,state_load_switch)


state_load_switch = IF(in(17) = 0, state_load_switch +=1,state_load_switch)
-ERROR C:/Coridium/Basic/Projects/812_hvac_relay_brd02.bas: 137: Expected Operand, found +=1,state_load_switch)
'
BTWthe two 'Ternary' pages have different "See also" links -- design or oversight??
'
'
NOTE: I downloaded the latest 'SetupBasic.exe' from my email link that I got when I purchased the mbed-Archproweb software
it reports Armbasic 9.34e
AND it finds the same kind of error



basicchip
Posts: 1029
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ternary_operation

Post by basicchip »

Ternary operators allow expressions, but something += somethingelse is a statement and is not allowed.

It is the equivalent of-

something = somethingelse += anyyetsomethingelse

Which is not legal in BASIC, and even in C most compiler will balk at, as it is sensitive to compilation order.

olzeke51
Posts: 394
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: ternary_operation

Post by olzeke51 »

I was trying to increase the variable by one - there was a comma after the '+=1' operator. seperating the true expression from the false expression
in your example - you did an 'AND' operation on a variable.
is there a difference between a logical (yours) and arithmetical/mathematical (mine) operation??

basicchip
Posts: 1029
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ternary_operation

Post by basicchip »

Like C a += is a statement.

x +=1 ' is the equivalent of x=x+1

Unlike C, BASIC does not use the last value calculated in a statement as an expression, so it is not legal to do

y = x+= 1 ' will cause an error

In C there is the ++ increment which is what you are looking to do, but there is no equivalent in BASIC

y = x++; // legal in C -- no equivalent in BASIC

olzeke51
Posts: 394
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: ternary_operation

Post by olzeke51 »

basicchip wrote:Like C a += is a statement.

x +=1 ' is the equivalent of x=x+1
is the above statement okay in BASIC? it seems like it would be okay -- a 'compound operator' [your description indicated it was two operations - -
is that why it failed as a true expression in the Ternary function ???
'
I guess what it comes down to, is: what are valid "true/false expression" in the Ternary function?,
you showed a logical example , I tried an assignment that wasn't associated with the calling variable [ x = IF( boolean, time = 30, 25) - it failed. I sort of
expected that one to fail]
I tried a compound operator on the same calling variable - it failed -the compiler didn't like the syntax (inside the ternary function)

y = x++; // legal in C -- no equivalent in BASIC

basicchip
Posts: 1029
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ternary_operation

Post by basicchip »

Expressions and statements are different things.

The simplest statement is an assignment statement--

lvalue = expression

lvalue is anything that you can assign a value to, variable, IO, array element

Among expressions are the ternary expression which is

... IF(expressionTEST, expressionTRUE, expressionFALSE) ...

An expression is any combination of rvalues which can be variables, IO, array elements, functions, constants; with operators like +, - ...
Now BASIC can use = as an operator as well as an assignment. This might lead to confusion that C has = for assignment and == for is equal to, some languages even have ===

In all cases += would NOT be an operator, it is actually part of a more complex compound assignment statement.

Your x=IF(boolean, time=30, 25) might compile as time=30 would be evaluate as "is time equal to 30" with a result of 0 or 1. Probably not what you meant

--------------------------------------------------------------

Compound assignment statements ARE legal in ARMbasic

lvalue OP= expression

is equivalent to

lvalue = current_value_of_lvalue OP expression

where OP is + - AND OR XOR / * and the rest

basicchip
Posts: 1029
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ternary_operation

Post by basicchip »

One other comment here, while most people think you write code for the compiler, you should really write code so that YOU or someone else can read the code later.

As it turns out code gets read by humans more than it gets written. If you write something overly complex and or dense you will be scratching your head about it 6 months later as you struggle to understand what it did.

So while this is completely legal

Code: Select all

IF (x = IF(y=z,a+b,c -d)) then t += 1
My comment would be WTF were you thinking about?

BASIC has pretty simple syntax and that makes it easy to write and understand. Now many languages offer abstractions like types, structures, or unions. Those can make understanding easier in some cases, in some cases you just make the code harder to understand.

Other languages have complex operators that are very powerful but also nearly unreadable. APL is an example of that, fun to play with but really a write only language. My personal opinion is Forth also falls into that category, and I have written lots of short Forth programs in the past.

AMDlloydsp
Posts: 99
Joined: Mon Apr 15, 2013 3:51 pm
Location: NE Central FL

Re: ternary_operation

Post by AMDlloydsp »

So while this is completely legal

Code: Select all

IF (x = IF(y=z,a+b,c -d)) then t += 1
My comment would be WTF were you thinking about?
-----------------------------

Heh! +1!!

I spend FAR more time deciphering (even my own) code than I do writing it.

Having sort of "grown up" with languages as they developed (started programming in 1969), I've seen all sorts of obtuse syntaxes.

Give me simple every time. That's why, even though I program in C and C++, I prefer a version of BASIC for stuff I must maintain commercially.

It's simple, if you write it that way. Even pointers to structures can obfuscate... although you need them sometimes.

LLoyd

olzeke51
Posts: 394
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: ternary_operation

Post by olzeke51 »

Thanks Guys -
I'm trying to save some code - I need to toggle 'state flags' and the ternary seemed easiest.
It saves Basic code - but when it compiles ????
I know the SELECT CASE format takes up more code space than the IF/THEN syntax -- I saved about 30bytes (reported)
on a 0 to 9 choice tree. There is a forum string on code optimization that I will re-read.
I did try to push the edge by trying to do a "time = 30" as a true expression
'
Thanks basicchip on explaining 'expression - - I used 'values' for the same concept
the " value + 1" is compiling okay for an increment action
'
Gary - aka Olzeke51

Post Reply