QB64 Phoenix Edition
Good Coding with ElseIF - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10)
+---- Thread: Good Coding with ElseIF (/showthread.php?tid=1661)

Pages: 1 2


Good Coding with ElseIF - Dimster - 05-04-2023

Going back through some of my coding I found I've used Else and If where I perhaps should have used ELSEIF. It seemed to work out ok but I'm not sure if there is difference in how these two work together.

For example

If ..... then
....
....
...Else
... if .. then
...
...
Else
......
......
End if

V's

If ... then
....
....
ElseIF .... then
.....
....
Else
....
End If

The logic flow would suggest there is no difference.


RE: Good Coding with ElseIF - mnrvovrfc - 05-04-2023

You will need an additional "END IF" for the first example shown above. That is only if the inside "IF... THEN" were also a block, placed after the first "ELSE".

Code: (Select All)
IF A THEN
:
ELSE
  IF B THEN
  :
  ELSE
  :
  END IF
END IF

I could think of a situation where I would have to work this block instead of the other one but can't show it to you right now. The second block is more like using "SELECT... END SELECT" but could involve more than one variable being compared.


RE: Good Coding with ElseIF - Dimster - 05-04-2023

A double End IF .... thank you. Seems the IDE did not show a problem with the way I laid out that first example but I haven't finished the subroutine to see if throws an error in the running of the code. I appreciate the help you provided.


RE: Good Coding with ElseIF - mnrvovrfc - 05-04-2023

Sometimes I wish BASIC were just like Lua or Pascal, insisting only on boolean result out of "IF... THEN".

Because it's not, because it's zero for "false" and any other value for "true", I have to design things with the first form. Like I said, I'm trying to think of a good way which has a complex "boolean" expression. Yet I'm not that confident neither in doing it in Lua, and that interpreter has given me problems with some of my ineffective designs.

Another thing is that QB64(PE) doesn't (yet) feature short-circuit evaluation, while Lua does. If it did, then even more things could be done with only one "IF" in the huge "IF... THEN... ELSEIF... ELSE... END IF" block. I wouldn't like short-circuit evaluation supported with "ANDALSO" and "ORELSE" keywords though, better with a compiler switch, if the aim isn't to be compatible with Freebasic and Visual Basic code.

Another thing is to gather the "IF" and "ELSEIF" stacks carefully when testing the same variable. Consider:

Code: (Select All)
IF i > 5 THEN
:
ELSEIF i > 10 THEN
:
END IF

What if you want code posted under "i > 10" to be executed while "i" is eleven at least? Instead of code that is executed in the first section? Something like this is a common mistake made with "IF... THEN... ELSEIF... ELSE... END IF". This was just an example with one variable involved, which could be rewritten with "SELECT CASE... END SELECT". But there is code which need to test two or more variables at the same time and relying on function results and other stuff.

Block "IF" is one of the best things that ever happened to BASIC. Together with looping structures and real subprograms, it made "GOTO" and line numbers unnecessary.

LOL Steve will answer you with more detail soon.


RE: Good Coding with ElseIF - James D Jarvis - 05-04-2023

Or maybe use Select Case?

Code: (Select All)
For i = 1 To 15
    a$ = Str$(i)
    Select EveryCase i
        Case Is > 5
            a$ = a$ + " " + Str$(i * 2)
        Case Is > 10
            a$ = a$ + " " + Str$(i * 3)

    End Select
    Print a$
Next i



RE: Good Coding with ElseIF - TerryRitchie - 05-04-2023

(05-04-2023, 09:23 PM)James D Jarvis Wrote: Or maybe use Select Case?

Code: (Select All)
For i = 1 To 15
    a$ = Str$(i)
    Select EveryCase i
        Case Is > 5
            a$ = a$ + " " + Str$(i * 2)
        Case Is > 10
            a$ = a$ + " " + Str$(i * 3)

    End Select
    Print a$
Next i

Yeah, SELECT CASE is my go to as well when ELSEIF comes into play with my IF ... THEN statements.

Hey! When was EVERYCASE added to SELECT CASE?? Holy cow, how do I miss these things? I can definitely make use of EVERYCASE.

I think it's time I take a journey through the Wiki to see what other gems were added that I somehow missed.


RE: Good Coding with ElseIF - mnrvovrfc - 05-04-2023

(05-04-2023, 09:23 PM)James D Jarvis Wrote: Or maybe use Select Case?

Code: (Select All)
For i = 1 To 15
    a$ = Str$(i)
    Select EveryCase i
        Case Is > 5
            a$ = a$ + " " + Str$(i * 2)
        Case Is > 10
            a$ = a$ + " " + Str$(i * 3)

    End Select
    Print a$
Next i

This is only an example I've provided. "EVERYCASE" is confusing and isn't supported by many modern BASIC's. Let's see what Dimster has to say about it. Otherwise he wants to be convinced about which block is better, a single long clause involving "ELSEIF", or "ELSE" section which has its own "IF" block. "SELECT CASE... END SELECT" could help even with multiple variables involved but could get ornery, might have to put specific code sections into a subprogram, perhaps with shared variables to make all the code easier to read.


RE: Good Coding with ElseIF - James D Jarvis - 05-05-2023

It's a matter of legibility more than logic flow. The more nested if...thens the harder it will be to track the if...efseif...thens as the code grows in complexity.

Code: (Select All)
x = 3: y = 5

'this block is easier to read
If x > 5 Then
    Print "x"; x
Else
    If y > 2 Then
        Print "Y"; y
    Else
        Print "X+Y"; x + y
    End If
End If

'this block isn't as easy to read but will produce the same results as the one above
If x > 5 Then
    Print "x"; x
ElseIf y > 2 Then
    Print "Y"; y
Else    
    Print "X+Y"; x + y
End If



RE: Good Coding with ElseIF - James D Jarvis - 05-05-2023

(05-04-2023, 09:51 PM)TerryRitchie Wrote: Hey! When was EVERYCASE added to SELECT CASE?? Holy cow, how do I miss these things? I can definitely make use of EVERYCASE.

I think it's time I take a journey through the Wiki to see what other gems were added that I somehow missed.

It is nifty isn't it? I stumbled on it sometime in the past few months (I think). It's pretty darned useful.


RE: Good Coding with ElseIF - Dimster - 05-05-2023

Hello mnrv .... I like the IF statement as a more precise decision in a perceptron. At the moment I'm playing with the Select Case for the various layers of a neural net but within each case is multiple IF's.  

Select Case within a Select Case is elegant but by mixing the decision types (ie Select Case & IF) it feels more like how our brains may order decisions.

I'm unfamiliar with any other computer languages, BASIC is pretty well it for me, so it's very interesting how other languages reformate the IF block statement. I'm going to have to look up "short-circuit evaluation"

In terms of "EveryCase", I haven't used it in the construction of my neural net because I'm afraid it will crash it, drive the brain crazy to do everything at once, but within the perceptrons and IF's I do have a final ELSE which I can code something similar which I call "All the Above". So in this regard I feel JD D and I are on the same page.

I do find ELSEIF useful to branch into some completely different condition then the original IF condition. Gives an added flexibility to my perceptrons.

You know, BASIC and QB64PE, has so much for me to explore in terms of data handlings and decision making .... not to mention you guys in the community to broaden the application of language ... that one day I may have a General AI of my own. Scary I know.