While... Until

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

While... Until

Post by Michael Vogel »

Just posted some source code lines like this:

Code: Select all

While l
   l-1
   If PeekA(@a+l)=c
      Break
   EndIf
Wend
Wouldn't it be nice to have a possibility for creating such codes... ?

Code: Select all

While l
   l-1
Until PeekA(@a+l)=c
Wouldn't it generate faster code as well?
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: While... Until

Post by STARGÅTE »

Both codes makes no sense, because you can't get clear statement:

Code: Select all

While l
   l-1
Until PeekA(@a+l)=c
; What happened to be here?
was l = 0 or was PeekA(@a+l) = c.
So you have to add here more querys...
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: While... Until

Post by MachineCode »

What's wrong with just doing:

Code: Select all

Repeat
   l-1
Until PeekA(@a+l)=c Or l=0
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: While... Until

Post by Shield »

They do make sense...if you don't care about the reason.
It's the same with switch statements from other languages (use multiple case with only one break).

However, it's not really necessary because it is still limited to two conditions instead of just one.
It's like saying we need another if statement because it only allows for one condition to be checked
instead of simply using two if statements.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: While... Until

Post by STARGÅTE »

if l = 0 at begin, it crashes.
So:

Code: Select all

Repeat
   l-1
Until l <= 0 Or PeekA(@a+l)=c
If l < 0 , PeekA(@a+l)=c will be ignore

Code: Select all

If #True Or CreateImage(1, 4, 4) ; CreateImage not execute!
	Debug IsImage(1)
EndIf
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: While... Until

Post by Michael Vogel »

STARGÅTE wrote:Both codes makes no sense [...] So you have to add here more querys...
The code was just a simple example for finding the position of a char in a string:

Code: Select all

a.s="C:\abc\def\x.yz"
result=Len(a.s)

While result
	result-1
	;***
	If PeekA(@a+result)='\'
		Break
	EndIf
Wend

Debug Left(a,result)
I know, that the code can be transformed into traditional statements but remember that usually there are many more statements (***) in a loop which would have to be executed in your examples.

I am relatively sure, that Fred doesn't want to change the traditional loop behaviour, as it would mean a lot of additional job for changing the whole system (syntax checks etc) and could irritate some users as well – just for a small speed improvement within some loops.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: While... Until

Post by Demivec »

I also don't think there is any advantage to what you've requested (i.e. a 'While/Until' loop).
Michael Vogel wrote:I know, that the code can be transformed into traditional statements but remember that usually there are many more statements (***) in a loop which would have to be executed in your examples.
I think the traditional statements make it clear what is taking place. For instance, for your coding example I would use:

Code: Select all

a.s="C:\abc\def\x.yz"
Result=Len(a.s)

If Result
  Repeat
    Result-1
    ;***
  Until PeekA(@a+Result)='\' Or Result = 0
EndIf

Debug Left(a,Result)
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: While... Until

Post by Michael Vogel »

Demivec wrote:I think the traditional statements make it clear what is taking place.
In the meantime, I found out, why I did the request - I don't like the 'Break' command (and I do everything to avoid lines like 'Break 2') :lol:
The previous example it was to easy for you :wink: - and you could avoid breaks and the additional "if Result=0" won't need a lot of exection time – but would you have seen such a simple 'conservative' solution also here that fast?

Code: Select all

a.s="C:\abc\def\x.yz"

Result=0
While PeekA(@a+Result)<>'.'
	Result+1
Until Result=Len(a) 

Debug Left(a,Result)
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: While... Until

Post by Little John »

Code: Select all

a.s = "C:\abc\def\x.yz"

Result = 1
While Result <> Len(a) And PeekA(@a+Result-1) <> '.'
   Result + 1
Wend

Debug Left(a, Result)
Regards, Little John
Puffolino
User
User
Posts: 49
Joined: Thu Jan 05, 2012 12:27 am

Re: While... Until

Post by Puffolino »

Shouldn't be Result<Len(a) ?

However, Break is never needed :wink:
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: While... Until

Post by Little John »

Puffolino wrote:Shouldn't be Result<Len(a) ?
No.
Puffolino
User
User
Posts: 49
Joined: Thu Jan 05, 2012 12:27 am

Re: While... Until

Post by Puffolino »

Little John wrote:
Puffolino wrote:Shouldn't be Result<Len(a) ?
No.
a.s=""

Result=1
:
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: While... Until

Post by Little John »

Puffolino wrote:
Little John wrote:
Puffolino wrote:Shouldn't be Result<Len(a) ?
No.
a.s=""

Result=1
:
Yes, and Michael's pseudo code would give the same result.
So my code is a correct "translation" of his pseudo code.
Puffolino
User
User
Posts: 49
Joined: Thu Jan 05, 2012 12:27 am

Re: While... Until

Post by Puffolino »

Little John wrote:
Puffolino wrote:
Little John wrote:
Puffolino wrote:Shouldn't be Result<Len(a) ?
No.
a.s=""

Result=1
:
Yes, and Michael's pseudo code would give the same result.
So my code is a correct "translation" of his pseudo code.
:D
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: While... Until

Post by Little John »

Puffolino wrote: :D
:?:
Post Reply