Cool Loops

1. Sep 8, 2014

alionalizoti

I am working with Quick Basic, 64 Bit for Windows, which is quite good indeed, but i met again the same usual difficulties to use [do ... loop] for efficient loops.

So I decided to better use [goto], as anyone else!
But I also found very useful the classical for loop.

Here are some useful examples.

for i = o to 1

{ program body }

i = 0
x$= inkey$
if x$= chr$(27) then i=1
next

or we can use

for i = o to 1

{ program body }

i = 0
x$= inkey$
if x$= "E" then i=1 next or we can use for i = o to 1 { program body } i = 0 x$ = inkey$if x$ = "E" then
y$= "" input "enter wish: ", y$
if y$= "exit" then i = 1 end if next if this has been useful, please make a sound and i shall have some extra points on my account from the good lord up there, trying to fix my lost points! 2. Sep 8, 2014 jedishrfu Staff: Mentor Is there a question on loops here? I noticed your for statement is using an 'o' and not a 0. Also it helps to block your code using [ code ] tags. You have a nice set of examples of for loop usage. However, it would be better to use do-while loops in this case: Code (Text): i=0 do while i<1 {program body} x$=inkey$if x$="E" then i=1

loop

or more compactly:

Code (Text):

Wend

7. Sep 9, 2014

Staff: Mentor

I don't understand what you mean in case #1 and why a for...loop would be better here. Using the
random number generator function is totally unrelated to a discussion about looping.

As an example someone might use a for loop to iterate through a deck of cards and then use the random number generator to select a card in the list and swap the current card with the randomly selected card as a means to shuffle the deck and you might loop this procedure n times to improve the shuffling of the deck.

Code (Text):

for i=1 to 3    $# shuffle the deck three times for j=1 to 52 k=get_random_number(between 1 and 52)$# psuedo code

$# swap the j and k card tempcard = card[j] card[j]=card[k] card[k]=tempcard next j next i With respect to case #2, most programmers would use a while loop if the were reading in the file line by line searching or aggregating for something: Code (Text): do while end_of_file(file)<0$ read a line from file

loop

but if you were reading in N lines of a file to store in an array then:

Code (Text):

NLINES=100     ## read in no more than 100 lines of input
for i=1 to NLINES
if end_of_file(myfile) break
next

CAVEAT: My examples are PSEUDO code not actual Quick Basic code just to give you an idea of how someone might handle the cases you described.

For what you're working perhaps for loops make more sense but I thought you should know how most programmers would handle things.

8. Sep 10, 2014

alionalizoti

of course, i agree

for i = 0 to 1
i = 0

{ loop program body - not standard - and specific }

x$= inkey$
if x$= chr$(27) 'esc key'
next

looks better!

9. Sep 10, 2014

alionalizoti

cool loop

of course, i agree

for i = 0 to 1
i = 0

{ loop program body - not standard - and specific - i.e. }

locate 1, 1
print 'cool loop - never ending'

x$= inkey$
if x$= chr$(27) then i = 1 'esc key'
next

looks better!

10. Sep 10, 2014

elusiveshame

Why are you insisting on using a for next loop and setting the variable to zero? Use another loop.

11. Sep 10, 2014

Staff: Mentor

Last edited by a moderator: Sep 25, 2014
12. Sep 10, 2014

elusiveshame

Lol :) I feel ya...

13. Oct 12, 2014

Modulo2pi

For someone who likes the tradition of for next loops, it seems VERY odd to have the next line: i = 0

(!