Page 2 of 3

Posted: Sun Sep 29, 2002 11:39 am
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by Justin

If the idea is this:

For a=1 To 999
messagerequester("",str(a),0)
if a=5 : a=999 : endif ;trying to break
messagerequester("","loop complete",0)
Next

messagerequester("","loop exit",0)

That is not a break, 'loop complete' is executed after forcing a=999, the loop is always finished,
Yes, but as shown in the complete example by MrVain, you stick an If round the part you are doing as the main loop and only do that if you have not already forced an end of loop.
a break transfers control inmediately to the statement following the next, endselect or whatever. Everything between the break and the next is not executed, the only way is using a goto.
There is very rarely only one way of doing something.
The break is extensively used in the windows message loop, look at any c code.
Yes, but in C the only clean way to get out of a switch statement is to use break. Otherwise the program just runs through all the code inside the "case XXXX:" parts.

Besides, just because it is used in C and Windows, who is to say it is the correct way to code?


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)

Posted: Sun Sep 29, 2002 11:58 am
by BackupUser
Restored from previous forum. Originally posted by PB.

> That is not a break, 'loop complete' is executed after forcing a=999

Try this amended example of your code routine above. However, I do
agree that a "Break" command would definitely make it all easier, and
with less code. :)

Code: Select all

For a=1 To 999
  MessageRequester("",Str(a),0)
  If a=5 : a=1000 : EndIf
  If a<1000
    MessageRequester("","loop complete",0)
  EndIf
Next
MessageRequester("","loop exit",0)

PB - Registered PureBasic Coder

Posted: Sun Sep 29, 2002 3:10 pm
by BackupUser
Restored from previous forum. Originally posted by Justin.

If the options are the suggested workarounds or the goto i prefer to use the goto.

The msg loop was just an example, because PB uses it a lot too(event loop), and we have to use the fakeendselect.

"Besides, just because it is used in C and Windows, who is to say it is the correct way to code?"

I don't get this, so it's no correct to have a break command?

Anyways, I just was a bit surprised that a basic language did not have that commands when most languages have it, we'll use the workarounds, that's all.

Posted: Sun Sep 29, 2002 6:00 pm
by BackupUser
Restored from previous forum. Originally posted by fred.

It's correct to have a break command and I will had it to exit cleanly from loop.

Fred - AlphaSND

Posted: Sun Sep 29, 2002 6:57 pm
by BackupUser
Restored from previous forum. Originally posted by Berikco.
Originally posted by fred

It's correct to have a break command and I will add it to exit cleanly from loop.

Fred - AlphaSND
Thanks Fred, i whas also waiting for this

Regards,

Berikco

http://www.benny.zeb.be

Posted: Sun Sep 29, 2002 7:00 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.
The msg loop was just an example, because PB uses it a lot too(event loop), and we have to use the fakeendselect.
What is a fakeendselect?
"Besides, just because it is used in C and Windows, who is to say it is the correct way to code?"

I don't get this, so it's no correct to have a break command?
No, I'm saying PureBasic shouldn't ape other systems for no good reason.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)

Posted: Sun Sep 29, 2002 8:29 pm
by BackupUser
Restored from previous forum. Originally posted by Justin.

Thank you Fred, that will be an useful command.

tinman, the fakeendselect is explained in the select/endselect part in the help file (basic keywords), it's handy function too.

Posted: Mon Sep 30, 2002 6:03 am
by BackupUser
Restored from previous forum. Originally posted by plouf.
What is a fakeendselect?
used to make an virutal endselect if you exit a select (with goto)
and did not return back
but AFAIK it is absolute anymore

Christos

Posted: Mon Sep 30, 2002 10:13 am
by BackupUser
Restored from previous forum. Originally posted by fweil.

Just to give up my mind about breaks and goto :

- it is better to not to use such methods for a clear coding but computing is based on loops and exceptions. No language can solve any problem (including natural language) without exceptions.

The first low level break / goto method is the If / Else / Endif block.

You will never code without jumping somewhere after doing something.

So I just mean it is not correct to say good programmers should never use !

This becomes especially true when optimizing an application program.

But you are right somewhere to try to not to use it.

KRgrds

Francois Weil
14, rue Douer
F64100 Bayonne

Posted: Mon Sep 30, 2002 10:17 pm
by BackupUser
Restored from previous forum. Originally posted by Berikco.

My idea also,

I never use goto, always try another construction.
But i think compiled, almost all loops look the same. (atleast in my previous language it whas)

I used it in the past, like this...

10 Print "Hello World"
20 Goto 10




Regards,

Berikco

http://www.benny.zeb.be

Posted: Tue Oct 01, 2002 3:37 am
by BackupUser
Restored from previous forum. Originally posted by Danilo.

>> I think a good coder dont need any Goto nor FakeReturn! :wink:)
>
> It's not considered a good programming practice these days to use
> Goto/FakeReturn to exit a loop. The functionality is there for
> compatibility reasons, but is best avoided in the first place.

The thing with "GOTO" is what we learned as little kids.
Everybody said we shouldnt use it because "IT" produces
"spaghetti code".
Well, its not "GOTO" that makes the spaghetti code, its the
little kid who used it, because it jumps around everywhere
in the code so that it is hard to follow the code.

Every professional programmer knows that GOTO isnt
a bad thing at all.
For me, a GOTO is the same like a unconditional Jump
in Assembly: JMP label

And without the JMP instructions, your computer
wouldnt even boot.
After the POST the BIOS loads the bootsector and
JUMPs to it, so it can continue...

If you start a .EXE, the loader in windows loads
this file, makes some things (importing DLLs etc..)
and the JUMPs to the entry point.

Without JMP/GOTO, you couldnt use your computer.

And know what: the BREAK command is also a JMP only,
so if you are a "good programmer" (good boy) then dont use it !! :)

And if you write a parser for example and it is deeply
within a loop (recursive procedure calls) and finds
a syntax error, its not needed to go back through all the
previous calls.
You simply abort the whole thing, jump out of it
and display the error message.

Said that, you should know yourself when GOTO
can be very useful and much faster.
You dont need it very often, but its not a
bad coding style if you know what you are doing.


Sometimes you have to think yourself...
dont believe what others say!

cya,
...Danilo

(registered PureBasic user)

Posted: Tue Oct 01, 2002 11:32 am
by BackupUser
Restored from previous forum. Originally posted by fred.

The problem about GOTO is it's only a jump. So it doesn't care about stack problem which can be really painful in a Select/Case for example. That's why you have to use FakeEndSelect. And Break willn't need an additionnal label :)

Fred - AlphaSND

Posted: Wed Oct 02, 2002 2:16 pm
by BackupUser
Restored from previous forum. Originally posted by flim.

I want break and continue command too

Posted: Wed Oct 02, 2002 6:25 pm
by BackupUser
Restored from previous forum. Originally posted by Danilo.

"continue" jumps back in the loop ??

cya,
...Danilo

(registered PureBasic user)

Posted: Wed Oct 02, 2002 6:30 pm
by BackupUser
Restored from previous forum. Originally posted by Berikco.

Some languages used break and continue for debugging...maybe this is what flim means?

Regards,

Berikco

http://www.benny.zeb.be