Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Zlatko Vid on December 01, 2024, 06:19:34 PM

Title: Do/Loop While ?
Post by: Zlatko Vid on December 01, 2024, 06:19:34 PM
Hello Charles..others

does o2 support

Do
...
...
...
Loop While expr

?
I mean i always use Do / End Do
or
While / Wend

or can i replace

Do /Loop While with...

Do loop ? and how  ?
Title: Re: Do/Loop While ?
Post by: Charles Pegge on December 01, 2024, 07:02:20 PM
Yes,

uses console
int i
do
  i++
  print i cr
'OPTIONS:
'loop while i<10
'loop when i<10
'loop until i>9
'loop if i<10
loop if not i>9
wait

Similar option exist for exit and continue
Title: Re: Do/Loop While ?
Post by: Zlatko Vid on December 02, 2024, 08:00:12 AM
or can i use just this :

int i
do
  i++
  print i cr
   if i=10 : exit do : end if
end do
Title: Re: Do/Loop While ?
Post by: Pierre Bellisle on December 02, 2024, 09:40:35 AM
Hi Aurel,
While waiting for Charles answer,
on my side, one thing I like is to be able to
put dual conditions, one in the do line and also one in the loop line.
This make the code more compact and often easier to read.

// KickSwitch -64 -r
// KickEnd

include once "console.inc"
include once "windows\msvcrt.inc"

%BCRYPT_USE_SYSTEM_PREFERRED_RNG = 2
! BCryptGenRandom lib "bcrypt.dll"(sys hAlgorithm, sys pbBuffer, dword cbBuffer, dword dwFlags) as long
'_____________________________________________________________________________

SetConsoleTitle("loop")
long index = 0
byte RandomByte

printl
printl "do until RandomByte > 245"

do until RandomByte > 245
  BCryptGenRandom(0, @RandomByte, 1, %BCRYPT_USE_SYSTEM_PREFERRED_RNG)
  printl "  index = " index ", RandomByte = " RandomByte
  index ++
loop while index <= 5

printl "loop while index <= 5"
printl
printl : printl "Press a key or click to end"
waitkey

end
'_____________________________________________________________________________
'
Title: Re: Do/Loop While ?
Post by: Charles Pegge on December 02, 2024, 03:28:36 PM
The compiler has to break all these looping expressions into a simple  do .. loop  containing one or more conditionals, containing an exit/continue instruction.

They are all luxury items for hand-coding. If you use an AI agent to produce the code, you might find it simpler to eliminate all while and for constructs.
Title: Re: Do/Loop While ?
Post by: Zlatko Vid on December 02, 2024, 05:47:13 PM
Hi Pierre....Charles

my intention is to avoid Loop While in this loop construct
because really i don't use in any of my programs Do/Loop While
i prefer Do/End Do and While/Wend

in first place i was asking because this is used in many C or Java programs
and i am doing some translation