Board Logo
« IIF$(uWantExample, "Here", "Never Mind") »

Welcome Guest. Please Login or Register.
Sep 26th, 2017, 07:32am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: IIF$(uWantExample, "Here", "Never Mind")  (Read 330 times)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1157
xx IIF$(uWantExample, "Here", "Never Mind")
« Thread started on: Apr 17th, 2017, 08:47am »

Code:
'IIF only there was a shorthand way to assign a value from an if test in a single line

chanceOfRain = rnd(0)
print "Chance of rain is ";chanceOfRain
print iif$(chanceOfRain > .25, "Better take an umbrella with you today.", "Probably won't need umbrella today.")


function iif$(booleanExpression, returnMeIfTrue$, returnMeIfFalse$)
    if booleanExpression then iif$ = returnMeIfTrue$ else iif$ = returnMeIfFalse$
end function

 


I learned it as IFF and it works better in BASIC's that don't need $.
It is needed in BASIC's that don't do a single line if... then... else...
« Last Edit: Apr 17th, 2017, 08:55am by bplus » User IP Logged

B+
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3089
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #1 on: Apr 17th, 2017, 2:46pm »

So the point is that in a Just BASIC it isn't needed? So too Liberty BASIC?

If a>b then result$="true" else result$="false"

Im really struggling to understand this debate.
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1157
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #2 on: Apr 17th, 2017, 3:42pm »

Yes, for JB (or LB) it's not same convenience at it is for languages that expect a block of code after 'if', another after 'else if' or 'else' and of course a required 'end if' or similar.

Don't overthink it, it just saves on some typing.
User IP Logged

B+
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3089
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #3 on: Apr 18th, 2017, 01:41am »

Minimum typing to achieve an if then else assignment in JB.

r=(a>b)

User IP Logged

rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 22
xx Re: IIF$(uWantExample, "Here", "Nev
« Reply #4 on: Apr 18th, 2017, 11:15am »

on Apr 18th, 2017, 01:41am, Rod wrote:
Minimum typing to achieve an if then else assignment in JB.

It does nothing of the sort. There seems to be a lack of understanding of the ternary conditional operator. Perhaps this Wikipedia article will help.


« Last Edit: Apr 18th, 2017, 11:18am by rtr » User IP Logged

tenochtitlanuk ( John F)
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 421
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #5 on: Apr 18th, 2017, 12:48pm »

Can't say this will change my programming world-view. That no doubt is virtually fossilized. But it has introduced me to the 'Elvis operator' for which I am grateful. ( The name "Elvis operator" refers to its resemblance to an emoticon of Elvis Presley, apparently.)

I'm currently in Budapest which has a statue of 'Anonymus'. Rubbing his writing pen/finger helps you create original literature. ( No, this is not a late April Fool joke)

I rubbed it and now hope to create a function 'anonymus()' which when called writes a section of code which I see no need for, but others will see as indispensable, especially if well versed in computer language development and use. Perhaps I'll even put it on the forum and it will feature in a future Wikipedia article!

PS as a 'Brit' I might start with the function 'TomJones( n)' which puts n coloured knickers up on the graphics screen ( you have to know who he is and his reputation to 'get' this). Anyone got other ideashuh Perhaps not...
User IP Logged

Visit my LB/JB pages at http://www.diga.me.uk/index.html
I use XP and Ubuntu Linux on old machines!
tenochtitlanuk ( John F)
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 421
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #6 on: Apr 18th, 2017, 1:17pm »

Yup. First attempt. Must make note to myself- add rotating and scaling and colorizing the original image and changing to masked sprites. But I liked the look...
User Image
PS I apologise for the political incorrectness of such images. I had to wear dark sun glasses to protect my delicate sensitivities before I dared search the 'web for a suitable image.
User IP Logged

Visit my LB/JB pages at http://www.diga.me.uk/index.html
I use XP and Ubuntu Linux on old machines!
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3089
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #7 on: Apr 18th, 2017, 1:21pm »

Now that I have cleaned my keyboard of splattered coffee and biscuit I will sit tight to see a practical example of iff.

I do accept that the original c++ code might return something other than true or false but I stand by r=(a>b) as the shortest code to return the results of an if then else comparison. (challenge)
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3553
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #8 on: Apr 18th, 2017, 2:15pm »

*I have nothing to say really*

ok then

IIF (cond, truePart, falsePart)
is old function of at least MS VB 6
It is sure supported in Excel VBA
EDIT and it has Wikipedia article

So it is natural that folks who got used to this convenience just re-create it in JB/LB.
(like I did in 2007)
(like recreating word$() functionality elsewhere).

So it's not "not needed". It's "not needed by you", and it's really fine. But others could find some uses.

EDIT
for the record, search for *.BAS containing "IIF(" returned 100 files in my JB folder, with lines as
Code:
piece2Capture = findPiece(enPassantCol, iif(move=clr.Black,4,5)) 
or Code:
    fillLen = iif(BMP.width mod 4, 4 - BMP.width mod 4, 0) 
or Code:
        indentLevel=iif(indentLevel>1, indentLevel-1, 1) 

« Last Edit: Apr 18th, 2017, 2:27pm by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1157
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #9 on: Apr 18th, 2017, 4:18pm »

Well I can't recommend IIF for JB, I went to code a cool example using it and ran into something strange, meanwhile I reduce the JB version of reverse function to a single line.

Code:
'reverse test.txt
'another IFF$ example and discovered something strange


test$ = "1234567"

'test iif$
print iif$(len(mid$(test$, 1)) <> 7, test$;" hasn't length of 7.", test$;" has length of 7.")

'reverse test
print rev$(test$)

'test reverse$ with reverse method and iif$, does not work???
print reverse$(test$)

function iif$(booleanExpression, returnMeIfTrue$, returnMeIfFalse$)
    if booleanExpression <> 0 then iif$ = returnMeIfTrue$ else iif$ = returnMeIfFalse$
end function


function reverse$(s$)

    'debug stuff
    scan
    print s$;" ";len(s$);" ";len(s$) > 1
    call pause 1000

    'this should do it but doesn't!
    reverse$ = iif$(len(s$) > 1, reverse$(mid$(s$, 2));left$(s$, 1), s$)

end function

function rev$(s$) 'reverse a word
    if len(s$) > 1 then rev$ = rev$(mid$(s$, 2));left$(s$, 1) else rev$ = s$
end function

sub pause mil   'tsh version has scan built-in
    t0 = time$("ms")
    while time$("ms") < t0 + mil : scan : wend
end sub


 


Don't understand why the reverse function with iif$ is not stopping.
« Last Edit: Apr 18th, 2017, 4:25pm by bplus » User IP Logged

B+
rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 22
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #10 on: Apr 18th, 2017, 5:34pm »

on Apr 18th, 2017, 4:18pm, bplus wrote:
Don't understand why the reverse function with iif$ is not stopping.

It's because in rev$() the recursive call happens only when len(s$)>1 but in reverse$() the recursive call happens every time irrespective of s$. This difference in the way the ternary function is implemented is called out explicitly in the Wikipedia article:

"Note that some languages may evaluate 'both' the true- and false-expressions, even though only one or the other will be assigned to the variable. This means that if the true- or false-expression contain a function call, that function may be called and executed (causing any related side-effects due to the function's execution), regardless of whether or not its result will be used. Programmers should consult their programming language specifications or test the ternary operator to determine whether or not the language will evaluate both expressions in this way. If it does, and this is not the desired behaviour, then an if-then-else statement should be used."
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1157
xx Re: IIF$(uWantExample, "Here", "Never Mind")
« Reply #11 on: Apr 18th, 2017, 6:39pm »

Thanks rtr!

Well at least I found an illustration of that note. smiley
User IP Logged

B+
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

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