Liberty BASIC Community Forum
« iif( bool , true , false ) »

Welcome Guest. Please Login or Register.
Apr 28th, 2017, 4:47pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: iif( bool , true , false )  (Read 257 times)
bluatigro
Guru
ImageImageImageImageImage


member is offline

Avatar

cxiu diversas el tio respondas cxiu samvaloras [ thats esperanto for : we are al different therefore we are al equal ]


PM

Gender: Male
Posts: 868
xx iif( bool , true , false )
« Thread started on: Apr 16th, 2017, 07:31am »

for if you need lots of simple if...then coides
Code:



function iif( bool , t , f )
''c++ : y = bool ? true : false
  uit = f
  if bool then uit = t
  iif = uit
end function

function iif$( bool , t$ , f$ )
  uit$ = f$
  if bool then uit$ = t$
  iif$ = uit$
end if

 
User IP Logged

basic's : C64 Simons C128 Amiga Amos quick4.5 Visual5 Visual6 Visual2005 Visual2008 just and last but not least liberty
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5251
xx Re: iif( bool , true , false )
« Reply #1 on: Apr 17th, 2017, 01:42am »

I am struggling to see how this simplifies code. Perhaps I am missing the point.

First off the intermediate value uit is not required, you can more simply use iif.

Secondly to call the function you must surely code the if then statement to create the bool value. So why bother calling the function?

bool = ( a > b )
If iff(bool,1,0) then

If a > b then

Somehow this seems simpler code, what am I missing?
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5251
xx Re: iif( bool , true , false )
« Reply #2 on: Apr 17th, 2017, 04:46am »

Richard points out that the code mimics the c++ ? operator. This is written in the form

result = bool ? true : false

So if bool is true we get true returned else false. Like any transformation you can wrap it in a function min() max() iff()

I probably need to see it used in context to understand its benefit. Or is it just an easily read and verbose way to check a condition in c++?

More words would help folks understand but perhaps I am too BASIC a coder get the point smiley



User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1618
xx Re: iif( bool , true , false )
« Reply #3 on: Apr 18th, 2017, 2:04pm »

Why to refer to C++? It was normal VB function back then, at least in VB6.
User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
Dan Teel
Guru
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1117
xx Re: iif( bool , true , false )
« Reply #4 on: Apr 18th, 2017, 7:34pm »

https://en.wikipedia.org/wiki/Conditional_(computer_programming)#C-like_languages

It has a much richer history I'm sure, but if you dont need to have an if-then statement after using the Iif function to assign a value to a variable, this would reduce the code, and, in certain situations, increase readability.

For example, I want the a bar in a bar graph to turn red when it goes below X.

Code:
for i = 1 to 10
    barColor(i)=Iif(barValue<X,RED,GREEN)
next i
 


Code:
for i = 1 to 10
    if barValue(i)<X then
      barColor(i)=RED
   else
      barColor(i)=GREEN
   end if
next i
 
« Last Edit: Apr 18th, 2017, 7:36pm by Dan Teel » User IP Logged

ZPtr.net
Brandon Parker
Board Moderator

member is offline

Avatar




PM

Gender: Male
Posts: 1074
xx Re: iif( bool , true , false )
« Reply #5 on: Apr 23rd, 2017, 09:50am »

This is how I have always written a version of the "IIf" function. I tend to always use True/ False as Global variables to make things easy so that will make things work a certain way; especially when evaluating "True"/ "False". This way forces the function to test the conditional which is how I like it...

This function has always worked well for me and tends to adapt well to every situation I use it in which is quite a lot.

Code:
Global False : False = 0
Global True : True = 1

varTest$ = "True"
Print IIf$(varTest$, "True", "False")
varTest$ = "False"
Print IIf$(varTest$, "True", "False")


varTest = 50
Print IIf$(str$(varTest), "True", "False")
varTest = 0
Print IIf$(str$(varTest), "True", "False")

varTest = 50
Print IIf$(str$(varTest);">=55", "True", "False")
varTest = 56
Print IIf$(str$(varTest);">=55", "True", "False")

End

    Function IIf$(condition$, trueAnswer$, falseAnswer$)
        IIf$ = falseAnswer$ : If condition$ = "" Then Exit Function
        If Val(Eval$(condition$)) Then IIf$ = trueAnswer$
    End Function 



{:0)

Brandon
User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2749
xx Re: iif( bool , true , false )
« Reply #6 on: Apr 24th, 2017, 07:55am »

on Apr 18th, 2017, 7:34pm, Dan Teel wrote:
https://en.wikipedia.org/wiki/Conditional_(computer_programming)#C-like_languages

It has a much richer history I'm sure, but if you dont need to have an if-then statement after using the Iif function to assign a value to a variable, this would reduce the code, and, in certain situations, increase readability.

For example, I want the a bar in a bar graph to turn red when it goes below X.

Code:
for i = 1 to 10
    barColor(i)=Iif(barValue<X,RED,GREEN)
next i
 


Code:
for i = 1 to 10
    if barValue(i)<X then
      barColor(i)=RED
   else
      barColor(i)=GREEN
   end if
next i
 

This is a nice idea, and might be made even more readable if you craft versions of the function with carefully chosen names, for example?

Code:
for i = 1 to 10
    barColor(i)=computeColor(barValue<X,RED,GREEN)
next i
 


-Carl
User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1618
xx Re: iif( bool , true , false )
« Reply #7 on: Apr 24th, 2017, 3:27pm »

Quote:
This is a nice idea, and might be made even more readable if you craft versions of the function with carefully chosen names, for example?
...
barColor(i)=computeColor(barValue<X,RED,GREEN)


Nope.
The point is IIF is standard way of doing such things
And as such is as readable as Code:
If condition THEN 
sequence.

Here, using "carefully chosen names" would hide that, obscuring the code.

If I would really want to hide that stuff completely I would write along this
Code:
barColor(i)=computeColor(barValue, X) 

« Last Edit: Apr 24th, 2017, 3:30pm by tsh73 » User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2749
xx Re: iif( bool , true , false )
« Reply #8 on: Apr 24th, 2017, 6:23pm »

I respectfully disagree. If you want to use a standard way of doing things, then great. More power to you. ;D

I was merely suggesting a different idea, a twist on the original post. I do think the iff idea is very cool.

-Carl

on Apr 24th, 2017, 3:27pm, tsh73 wrote:
Nope.
The point is IIF is standard way of doing such things
And as such is as readable as Code:
If condition THEN 
sequence.

Here, using "carefully chosen names" would hide that, obscuring the code.

If I would really want to hide that stuff completely I would write along this
Code:
barColor(i)=computeColor(barValue, X) 

User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls