Page 1 of 1

While... Until

Posted: Fri Dec 28, 2012 10:15 am
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?

Re: While... Until

Posted: Fri Dec 28, 2012 1:58 pm
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...

Re: While... Until

Posted: Fri Dec 28, 2012 2:05 pm
by MachineCode
What's wrong with just doing:

Code: Select all

Repeat
   l-1
Until PeekA(@a+l)=c Or l=0

Re: While... Until

Posted: Fri Dec 28, 2012 2:12 pm
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.

Re: While... Until

Posted: Fri Dec 28, 2012 2:16 pm
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

Re: While... Until

Posted: Fri Dec 28, 2012 2:41 pm
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.

Re: While... Until

Posted: Fri Dec 28, 2012 4:33 pm
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)

Re: While... Until

Posted: Fri Dec 28, 2012 5:59 pm
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)

Re: While... Until

Posted: Fri Dec 28, 2012 6:20 pm
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

Re: While... Until

Posted: Fri Dec 28, 2012 6:59 pm
by Puffolino
Shouldn't be Result<Len(a) ?

However, Break is never needed :wink:

Re: While... Until

Posted: Fri Dec 28, 2012 7:08 pm
by Little John
Puffolino wrote:Shouldn't be Result<Len(a) ?
No.

Re: While... Until

Posted: Fri Dec 28, 2012 7:10 pm
by Puffolino
Little John wrote:
Puffolino wrote:Shouldn't be Result<Len(a) ?
No.
a.s=""

Result=1
:

Re: While... Until

Posted: Fri Dec 28, 2012 7:14 pm
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.

Re: While... Until

Posted: Fri Dec 28, 2012 7:17 pm
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

Re: While... Until

Posted: Fri Dec 28, 2012 7:18 pm
by Little John
Puffolino wrote: :D
:?: