ELSEIF Block Would be Good.


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1
    T.Jackson's Avatar
    T.Jackson Guest

    Post ELSEIF Block Would be Good.

    Be great if PBP supported ELSEIF structure - something a bit like the following...

    Code:
       ElseIf GetAsyncKeyState(vbKeyF1) Then               '(F1) Show palette
          If Key_Pressed(vbKeyF1) = 0 Then                 'Key not held down?
             LED_Palette.Show                              'Show palette
             Key_Pressed(vbKeyF1) = 1                      'Set flag
          End If
             
       ElseIf GetAsyncKeyState(vbKeyF6) Then               '(F6) Next frame
          If Key_Pressed(vbKeyF6) = 0 Then                 'Key not held down?
             Adj_Button = 0                                'Set flag
             Call Apply_Frame_Num_Adj                      'Jump to proc
             Key_Pressed(vbKeyF6) = 1                      'Set flag
          End If
    ELSEIF can be quite advantageously in some cases, consider the following complex procedure and how it would need to be done if, ELSEIF wasn't supported in VB
    <hr/>
    Code:
    Public Sub Poll_Keyboard()
       '-----------------------------------------------------------------------------
       '// Check for menu control keys, ESC terminates app, all other keys restore
       '   menu and cursor if they have been turned off. Proc includes key de bounce
       '-----------------------------------------------------------------------------
       Dim i As Long                                       'General working var
                                                              
       If GetAsyncKeyState(vbKeyEscape) Then               'Quit   (ESC)
          Terminate_App = True                             'Set flag
    
       '-----------------------------------------------------------------------------
       '// Check combination keys first
       '-----------------------------------------------------------------------------
       ElseIf GetAsyncKeyState(vbKeyControl) Then          'CTRL key down?
             If Popup_Insert.Enabled Then                  'Legal operation?
                If GetAsyncKeyState(vbKeyI) Then           'CTRL+I Insert
                   Call Popup_Insert_Click                 'Jump to proc
                   Exit Sub                                'Bail out
                End If
             End If
             
          If Popup_Save.Enabled Then                       'Legal operation?
             If GetAsyncKeyState(vbKeyShift) Then          'Shift?
                If GetAsyncKeyState(vbKeyS) Then           'Shift+CTRL+S Save As
                   Call Initiate_ROM_Save("Save As *.bin") 'Jump to proc
                   Exit Sub                                'Bail out
                End If
             End If
             
             If Not Run_ROM Then
                If GetAsyncKeyState(vbKeyN) Then           '(CTRL + N) New
                   Call New_ROM_Click                      'Jump to proc
                   Exit Sub                                'Bail out
                End If
       
                If GetAsyncKeyState(vbKeyO) Then           '(CTRL + O) Load
                   Call Load_ROM_Click                     'Jump to proc
                   Exit Sub                                'Bail out
                End If
             End If
             
          '//
          Else '(always allow open or load, if not running)
          '//
             If GetAsyncKeyState(vbKeyN) Then              '(CTRL + N) New
                Call New_ROM_Click                         'Jump to proc
                Exit Sub                                   'Bail out
             End If
       
             If GetAsyncKeyState(vbKeyO) Then              '(CTRL + O) Load
                Call Load_ROM_Click                        'Jump to proc
                Exit Sub                                   'Bail out
             End If
          End If
       '-----------------------------------------------------------------------------
       '// Function keys with no CTRL, all used except (10-12)
       '-----------------------------------------------------------------------------
       ElseIf GetAsyncKeyState(vbKeyF1) Then               '(F1) Show palette
          If Key_Pressed(vbKeyF1) = 0 Then                 'Key not held down?
             LED_Palette.Show                              'Show palette
             Key_Pressed(vbKeyF1) = 1                      'Set flag
          End If
             
       ElseIf GetAsyncKeyState(vbKeyF6) Then               '(F6) Next frame
          If Key_Pressed(vbKeyF6) = 0 Then                 'Key not held down?
             Adj_Button = 0                                'Set flag
             Call Apply_Frame_Num_Adj                      'Jump to proc
             Key_Pressed(vbKeyF6) = 1                      'Set flag
          End If
       
       ElseIf GetAsyncKeyState(vbKeyF7) Then               '(F7) Previous frame
          If Key_Pressed(vbKeyF7) = 0 Then                 'Key not held down?
             Adj_Button = 1                                'Set flag
             Call Apply_Frame_Num_Adj                      'Jump to proc
             Key_Pressed(vbKeyF7) = 1                      'Set flag
          End If
       
       ElseIf GetAsyncKeyState(vbKeyF8) Then               '(F8) Next group
          If Key_Pressed(vbKeyF8) = 0 Then                 'Key not held down?
             Adj_Button = 4                                'Set flag
             Call Apply_Group_Num_Adj                      'Jump to proc
             Key_Pressed(vbKeyF8) = 1                      'Set flag
          End If
       
       ElseIf GetAsyncKeyState(vbKeyF9) Then               '(F9) Previous group
          If Key_Pressed(vbKeyF9) = 0 Then                 'Key not held down?
             Adj_Button = 5                                'Set flag
             Call Apply_Group_Num_Adj                      'Jump to proc
             Key_Pressed(vbKeyF9) = 1                      'Set flag
          End If
       
       ElseIf GetAsyncKeyState(vbKeyF5) Then               '(F5) Run
          If Key_Pressed(vbKeyF5) = 0 Then                 'Key not held down?
             Call Run_Click                                'Jump to proc
             Key_Pressed(vbKeyF5) = 1                      'Set flag
          End If
          
       ElseIf GetAsyncKeyState(vbKeyF2) Then               '(F2) Pause
          If Key_Pressed(vbKeyF2) = 0 Then                 'Key not held down?
             Call Pause_Click                              'Jump to proc
             Key_Pressed(vbKeyF2) = 1                      'Set flag
          End If
          
       ElseIf GetAsyncKeyState(vbKeyF3) Then               '(F3) Reset
          If Key_Pressed(vbKeyF3) = 0 Then                 'Key not held down?
             Call Reset_Click                              'Jump to proc
             Key_Pressed(vbKeyF3) = 1                      'Set flag
          End If
       
       ElseIf GetAsyncKeyState(vbKeyF8) Then               '(F8) Jump to
          If Not Key_Pressed(vbKeyF8) Then                 'Key not held down?
             Call Jump_To_Frame_Click                      'Jump to proc
             Key_Pressed(vbKeyF8) = 1                      'Set flag
          End If
       
       ElseIf GetAsyncKeyState(vbKeyF9) Then               '(F9) Dump
          If Key_Pressed(vbKeyF9) = 0 Then                 'Key not held down?
             Call Dump_Click                               'Jump to proc
             Key_Pressed(vbKeyF9) = 1                      'Set key flag
          End If
       
       ElseIf GetAsyncKeyState(vbKeyDelete) Then           '(Del) All Off
          If Total_Frames <> 0 Then                        'Legal operation?
             If Key_Pressed(vbKeyDelete) = 0 Then          'Key not held down?
                Call All_Off_Click                         'Jump to proc
                Key_Pressed(vbKeyDelete) = 1               'Set flag
             End If
          End If
       '-----------------------------------------------------------------------------
       Else '// Any other key press restores menu & cursor (if switched off)
       '-----------------------------------------------------------------------------
          If Not Menu.Visible Then
             For i = 0 To 255                              'Test for keys
                 If GetAsyncKeyState(i) Then               '
                    Menu.Visible = True                    'Restore
                    Title.Visible = True                   '
                    Credits.Visible = True                 '
                    Call ShowCursor(True)                  'Mouse pointer
       '//
                    If Palette_Active Then                 'Palette visible prior?
                       LED_Palette.Show                    'Restore it
                    End If                                 '
                    Exit For                               'Bail out (no time wasted)
                 End If
             Next
          End If
       End If
       '-----------------------------------------------------------------------------
       '// Reset flags if keys are up
       '-----------------------------------------------------------------------------
       For i = 0 To 255                                    'Scan through all keys
           If GetAsyncKeyState(i) = 0 Then                 'Key held down?
              Key_Pressed(i) = 0                           'Reset flag if not
           End If                                          '
       Next                                                '
    End Sub
    Last edited by T.Jackson; - 9th May 2007 at 06:48.

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    A billion and a half plus 2 ways of doing things...yes...
    That 2nd code chunk looks an awful lot like, and in this case could be replaced by, a 'Select Case/End Select' block.
    EDIT: But, 'Select Case' is limited to one parameter, an ELSEIF would open that up quite a bit.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    As far as I know it already does. (just not all in one word)
    Code:
    IF Condition then
        Something
    else 
        if Condition2 then
            something else
        endif
    endif
    _
    DT

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    And in some forms, literally KILLS free flash space...

  5. #5
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post

    That's not the same. True ELSEIF structure will exit out of the nest as soon as it hits the first true condition. Same as Select Case.

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    But PBP doesn't have the virtually (pun intended) unlimited stack space like Windows does.
    Gotta draw the line somewhere I suppose.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    And in some forms, literally KILLS free flash space...
    No difference, no additional code space.

    Quote Originally Posted by T.Jackson View Post
    That's not the same. True ELSEIF structure will exit out of the nest as soon as it hits the first true condition. Same as Select Case.
    No difference. That's what it does.

    Added: No additional Stack space either.

    _
    Last edited by Darrel Taylor; - 9th May 2007 at 09:39. Reason: Stack
    DT

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    What I'm saying is that multiple IF/THEN statements:
    IF this then
    that
    ENDIF
    IF this then
    that
    ENDIF

    made back-to-back-to-back, takes more flash space than successive IF/THEN statements:
    If this then
    if this then
    if this then
    that
    endif
    endif
    EndIf

    I had to use the successive version of the IF/THEN vs. the back-to-back version in my MP3 player in a number of places (no way around it due to how the program was set up, either it was bad style or I just had too much going on to keep tabs on, just the nature of the program's complexity). Anywhos...when I changed everything over to the successive type, I saved a load of code space. I never did check in to why I saved as much space as I did, it just did, and I'm happy.

    As far as stack space goes, are you saying that there aren't any calls involved with doing an if/then?

  9. #9
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    As far as I know it already does. (just not all in one word)
    Code:
    IF Condition then
        Something
    else 
        if Condition2 then
            something else
        endif
    endif
    It's not the same if a multitude of ELSEIF is required ...

    Code:
    IF Condition then
        Something
    else 
        if Condition2 then 'ELSEIF 1
            something else
        endif
    
       '// Even if ELSEIF 1 is true then we still validate this argument  
       if Condition2 then 'ELSEIF 2
            something else
        endif
    
       '// Likewise, even if ELSEIF 2 is true then we still validate this argument  
        if Condition3 then 'ELSEIF 3
            something else
        endif
    endif
    I guess you could have a GOTO in each block to exit out of the nest - but as I said before, true ELSEIF would make a nice addition to PBP.
    <hr/>

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    It'll work. You just need to add the next condition in the last one's ELSE clause.
    Code:
    IF Condition then
        Something
    else 
        if Condition2 then
            something else
        else
            if Condition3 then
                even more
            else
                if Condition4 then
                   4's good
                else
                    this is the final ELSE, if none of the others match
                    it ends up here
                endif
            endif
        endif
    endif
    Quote Originally Posted by skimask
    As far as stack space goes, are you saying that there aren't any calls involved with doing an if/then?
    Correct. If/Thens don't use the stack. They use GOTO's only.

    Added: There is no limit to the "Nested" levels of IF/THEN's.
    _
    Last edited by Darrel Taylor; - 9th May 2007 at 18:25. Reason: Nested Levels
    DT

  11. #11
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Post

    O.K. I agree that to be logically equivalent to ELSEIF. But there's one downside, how do you plan on indenting 20 or more nested arguments?
    But, you have clearly illustrated just how easy it would be for MELABS to implement support for ELSEIF. PBP already has the raws required.
    Last edited by T.Jackson; - 10th May 2007 at 03:38.

Similar Threads

  1. config bits
    By brianD in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 9th February 2010, 13:45
  2. Setting up internal clock correctly?
    By JohnM in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th March 2008, 20:29
  3. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  4. Bootloader and Instant Interrupts Atn:_DT_
    By Josuetas in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 16th May 2007, 01:59
  5. PortE problems (PIC18F4455)
    By RubenR in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 12th July 2006, 15:26

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts