Using GOTO and For Loops in Quick Basic 64-Bit for Windows

In summary: The code inside the loop will run a fixed number of times, no matter what. So if you need to do something based on a condition, use a WHILE...WEND or DO...LOOP.You're missing my point, when the language provides a variety of looping constructs the more experienced programmer will choose the one most appropriate for the task he or she is trying to accomplish. So when counting index is needed you would choose the for loop but when you must loop an indeterminate number of times then a while loop is more appropriate.
  • #1
alionalizoti
16
0
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!
 
Technology news on Phys.org
  • #2
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:
i=0

do while i<1 

    {program body}

    x$=inkey$
    if x$="E" then i=1

loop

or more compactly:

Code:
do while inkey$<>'E'

    {program body}
   
loop

The reasoning is that there is an expectation by people reading the code of counting in a for loop with i being the index value whereas in a do-while loop there is an expectation of looping until some condition is met in this case the user typing an E.
 
Last edited:
  • Like
Likes Modulo2pi
  • #3
Ok, this do ... loop may work, but not with every program body
i.e.,
1) randomizing: and you know why, it takes too long for the randomizing command to execute
and meanwhile do ... loop is simply lost or vanished due to lack of timing.
2) files or large files, reading or writing: it takes too long for these too.
3) etc!

Also, for is the traditional loop of Basics, this is why i find it literally useful
 
  • #4
alionalizoti said:
Ok, this do ... loop may work, but not with every program body
i.e.,
1) randomizing: and you know why, it takes too long for the randomizing command to execute
and meanwhile do ... loop is simply lost or vanished due to lack of timing.
2) files or large files, reading or writing: it takes too long for these too.
3) etc!

Also, for is the traditional loop of Basics, this is why i find it literally useful

You're missing my point, when the language provides a variety of looping constructs the more experienced programmer will choose the one most appropriate for the task he or she is trying to accomplish. So when counting index is needed you would choose the for loop but when you must loop an indeterminate number of times then a while loop is more appropriate.

A similar case exists for case selection, when the cases are known then a switch case construct is more appropriate than a sequence of if conditions.

The analogy here is that I can fix a lot things with a hammer but when it's more appropriate I use a screwdriver to tighten screws in ie I don't hammer them into place.
 
  • Like
Likes Modulo2pi
  • #5
alionalizoti said:
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
The code above is not as clear at should be for the following reasons.
1. What you're calling "program body" is usually called the loop body, for obvious reasons.

2. Since the last line of your code is next, I infer that the loop body actually contains the line you wrote as {program body} as well as all the lines of code after it up to the line with next. If my guess is correct, it's misleading to have "program body" in there if it doesn't actually represent all of the code in the loop body.

3. As already mentioned, don't confuse the letter 'o' with the number 0.

4. Your code accepts the character 'E' but not the character 'e'. A user who wants to exit might not know that 'E' will work but 'e' won't.

One more thing. Instead of using bold to display code, use the [code] and [/code] BB tags around your example. These tags preserve whatever indentation you have used.
 
  • #6
FOR...NEXT loops are used when you need a specific amount of loops, such as reading through an indexed array (previously mentioned); where you need a set # of iterations. Using it in place of a WHILE...WEND or DO...LOOP is very bad practice.

FOR...NEXT aren't conditional loops either (meaning you can't exit on a condition unless you add logic in the loop), unlike DO and WHILE loops that have a few means of using conditionals to escape the loop, which in your case would be the proper way to execute your code :

Code:
 While ucase$(inkey$) <> ucase$("E")
     ' your code here
 Wend
 
  • #7
alionalizoti said:
Ok, this do ... loop may work, but not with every program body
i.e.,
1) randomizing: and you know why, it takes too long for the randomizing command to execute
and meanwhile do ... loop is simply lost or vanished due to lack of timing.
2) files or large files, reading or writing: it takes too long for these too.
3) etc!

Also, for is the traditional loop of Basics, this is why i find it literally useful

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:
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:
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:
NLINES=100     ## read in no more than 100 lines of input
for i=1 to NLINES
    if end_of_file(myfile) break
    line[i]=read(myfile)
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
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
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
Why are you insisting on using a for next loop and setting the variable to zero? Use another loop.
 
  • Like
Likes 1 person
  • #11
If I had a hammer...

 
Last edited by a moderator:
  • #12
jedishrfu said:
If I had a hammer...
Lol :) I feel ya...
 
  • #13
For someone who likes the tradition of for next loops, it seems VERY odd to have the next line: i = 0
(!
 

1. What is a GOTO statement in Quick Basic 64-Bit for Windows?

A GOTO statement in Quick Basic 64-Bit for Windows is a control flow statement that allows the program to jump to a specific line of code, bypassing any code in between. It is generally not recommended to use GOTO statements as they can make the code difficult to read and maintain.

2. How do I use a GOTO statement in my Quick Basic 64-Bit for Windows program?

To use a GOTO statement, you must first assign a line number to the code you want to jump to. Then, use the GOTO keyword followed by the line number to jump to that specific line of code. For example, "GOTO 100" will jump to line 100 in your program.

3. What is a For Loop in Quick Basic 64-Bit for Windows?

A For Loop is a control flow statement that allows a set of code to be executed repeatedly for a specified number of times. It is commonly used for iterating over a list of values or performing a specific task a certain number of times.

4. How do I use a For Loop in my Quick Basic 64-Bit for Windows program?

To use a For Loop, you must first declare a variable to act as the counter for the loop. Then, use the FOR keyword followed by the variable name and the starting and ending values for the loop. Inside the loop, you can perform any desired actions. For example, "FOR i = 1 TO 10" will execute the code inside the loop 10 times, with the value of i increasing by 1 each time.

5. Can I use both GOTO statements and For Loops in the same Quick Basic 64-Bit for Windows program?

Yes, you can use both GOTO statements and For Loops in the same program. However, it is important to use them carefully and avoid creating complex and confusing code. It is generally recommended to use For Loops instead of GOTO statements for repetitive tasks.

Similar threads

  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
754
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Computing and Technology
2
Replies
37
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Special and General Relativity
Replies
2
Views
651
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top