Bug? Keyword 'Continue' not consistent in behaviour

Ed Brown

Active Member
Licensed User
Longtime User
I've recently encountered some code that was not executing as expected and narrowed it down to a 'Continue' statement. I have had to rewrite two affected code blocks to remove the Continue statement but, this is not always the ideal solution.

The issue found is that the Continue statement does not skip the remaining code within the loop (so far, all loop types are affected by this) if it is itself inside a Select block but, this behaviour is not consistent throughout the rest of the project. ie. it works as expected in some functions but not in others.

I haven't been able to reliably reproduce the issue using a small project but, I have provided one of the affected code blocks below as it is AFTER commenting out the Continue statement and applying a work around.

What's also interesting is that the expected behaviour of intellisense appears to replicate this. ie. The background colour of the corresponding loop associated with the Continue statement is changed when the expected behaviour works but, does not change when the expected behaviour does not work.

If the Continue statement is moved outside of the Select block then it works as expected.

Due to the nature of this project I cannot zip it up and attach it here.

B4X:
        Do While Not(IsValid)
            Wait For (mainDialog.ShowCustom(pnlPrefs, obOK, "", obCancel)) Complete (dlgResult As Int)
            
            Select    dlgResult
                Case DialogResponse.POSITIVE
                    ' Validate the accepted values
                    IsValid = True
                    Dim tDistance As Int
                    Try
                        tDistance = txtPrefAutoCollectionDistance.Text
                        If tDistance < Starter.MinDistance Or tDistance > Starter.MaxDistance Then
                            ' The range is outside the min/max - let the user know
                            UI.ShowToastMessage(obInvalidDistance, True, UI.TOAST_ERROR, UI.ICON_ERROR)
                            IsValid = False
                            ' Continue ' <<<<<<<< not working as expected
                        End If
                    Catch
                        Log(LastException)
                    End Try
                    
                    Dim tTime As Int
                    If IsValid Then
                        Try
                            tTime = txtPrefAutoCollectionTime.Text
                            If tTime < Starter.MinTime Or tTime > Starter.MaxTime Then
                                ' The range is outside of the min/max - let the user know
                                UI.ShowToastMessage(obInvalidTime, True, UI.TOAST_ERROR, UI.ICON_ERROR)
                                IsValid = False
                            End If
                        Catch
                            Log(LastException)
                        End Try
                    End If
                    
                    If IsValid Then
                        ' Save the new values
                        Starter.AppConfig.Put(Starter.obAutoCollectionDistance, tDistance)
                        Starter.AppConfig.Put(Starter.obAutoCollectionTime, tTime)
                        
                        ' all is good - make the user feel all warm and fuzzy
                        UI.ShowToastMessage(obUpdatedPreferences, True, UI.TOAST_SUCCESS, UI.ICON_TICK)
                    End If
                Case Else
                    ' Cancellation from the user is Valid - nothing is updated
                    IsValid = True
            End Select
        Loop
 

drgottjr

Expert
Licensed User
Longtime User
go with exit (to get out of the select/case)
https://www.b4x.com/android/help/core.html#keywords_exit

you might also want to consider setting a flag within a particular case block before exiting. an old trick. then refer to that flag once you're back into the loop. if the loop sees the flag set, you could "continue" (if applicable) or "exit" from the loop (were some facet of the particular case block satisfied). don't forget to reset the flag before entering the select/case.
 

Ed Brown

Active Member
Licensed User
Longtime User
Hi @drgottjr , thanks for replying. Using 'exit' is not the solution as all iterations of the loop need to be processed. Using 'exit' will exit the loop before all iterations have been done.
 

drgottjr

Expert
Licensed User
Longtime User
Hi @drgottjr , thanks for replying. Using 'exit' is not the solution as all iterations of the loop need to be processed. Using 'exit' will exit the loop before all iterations have been done.
yeah; i thought about it later. it was old C-think. anyway, i stand by the flag
 
Top