Why does this code always returns zero ?

Just starting out? Need help? Post your questions and find answers here.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Why does this code always returns zero ?

Post by IdeasVacuum »

Well, my native language is English and I understood the context of the comment perfectly well, the English is perfectly correct. If you remove a portion of any sentence, it is then likely to lose it's original meaning - that is what you did.

Look at the difference:

"Programming without EnableExplicit is not a mistake, but it is extremely unwise."
Advice that is delivered in a kind and friendly manner.

"Programming without EnableExplicit is not a mistake"
A Statement.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Why does this code always returns zero ?

Post by Little John »

IdeasVacuum wrote:Well, my native language is English and I understood the context of the comment perfectly well
I'm glad that we've talked about it.
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Why does this code always returns zero ?

Post by mk-soft »

English is not my language and since DEEPL.COM exists my english has become even worse. :mrgreen:

Unfortunately there is no "Do : Break : EndDo" in Purebasic
With "Goto" it is not a good solution that it can come with loops to problems.
Most of the time I solve it with "Repeat : Break : Until #True", which doesn't always look clean, because you might think it is a loop.

Code: Select all

Procedure foo()
  
  Repeat ; Do
    
    Debug "Loop i"
    For i = 1 To 10
      If i = 5
        Debug "Do not more"
        Break 2
      EndIf
    Next
    
    Debug "Do more"
    
  Until #True ; EndDo
  
  Debug "Exit"
  
EndProcedure

foo()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Why does this code always returns zero ?

Post by eck49 »

Drifting off topic, but...

Anyone remember Gosub (followed by a line number), from some early flavours of BASIC long, long ago?
Before the days of named Procedures, this was how sub-routines were done.
GoSub executed the target code and when it was finished (Return statement, I think) it carried on with the next line of code.
And there was no Break or Continue, just GoTo.

Yes, GoTo is very powerful, and therefore should be used very carefully.

But one place I find it makes for clearer code is where there is a small forest of nested loops and deep inside I find a condition which means I want to abort the whole lot. Rather than set up a series of Break and Test at every level a simple GoTo looks cleaner to me, especially if there is not much more than a cascade of Next statements between the GoTo and its label. But I have a rule that for each label there can only be one GoTo. And, in practice they are very rare indeed.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Why does this code always returns zero ?

Post by mk-soft »

The "goto" function should be used with care. You must also not jump out of a procedure, as this leads to a stack problem.
Syntax

Goto <label>
Description
This command is used to transfer the program directly to the labels position. Be cautious when using this function, as incorrect use could cause a program to crash...

Note: To exit a loop safely, you always must use Break instead of Goto, and never use it inside a Select/EndSelect block (Unless you have the ability to manage the stack yourself, correctly.)
You can also get out of nested loops with the "break" function.

Code: Select all

For k=0 To 10
  Counter = 0
  Repeat
    If k=5
      Break 2 ; Will exit directly from the Repeat/Until and For/Next loops
    EndIf
    Counter+1
  Until Counter > 1
  Debug k
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Why does this code always returns zero ?

Post by Saki »

Yes Goto, an little example
In principle, you can run this until the CPU falls out of the socket.

Code: Select all

If result
      search_again:
      delay_1+Delay_internal(0, 1000) ; Use internal timer 0
      If delay_1>delay
        StopDrawing()
        ProcedureReturn 0
      EndIf    
      test_color=Random($FFFFFE, 1) ; Ignore 0 and $FFFFFF
      For i=y To yy
        For ii=x To xx
          If test_color=Point(ii, i)
            Goto search_again 
          EndIf
        Next ii
      Next i
      StopDrawing()
      ProcedureReturn test_color
    Else
      ProcedureReturn -17
EndIf
地球上の平和
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Why does this code always returns zero ?

Post by TI-994A »

eck49 wrote:Anyone remember Gosub (followed by a line number), from some early flavours of BASIC long, long ago?
Surely do! An early introduction to modular programming.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Why does this code always returns zero ?

Post by eck49 »

@Saki
Breaks one of my old rules: Goto always goes forward, ie lower down the code. Going backwards implies either a potential loop or badly organised code. As, I think, was your point.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply