Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Cool Loops

  1. Sep 8, 2014 #1
    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. jcsd
  3. Sep 8, 2014 #2

    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):


    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: Sep 8, 2014
  4. Sep 9, 2014 #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
     
  5. Sep 9, 2014 #4

    jedishrfu

    Staff: Mentor

    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.
     
  6. Sep 9, 2014 #5

    Mark44

    Staff: Mentor

    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.
     
  7. Sep 9, 2014 #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 (Text):

     While ucase$(inkey$) <> ucase$("E")
         ' your code here
     Wend
     
     
  8. Sep 9, 2014 #7

    jedishrfu

    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
        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.
     
  9. Sep 10, 2014 #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!
     
  10. Sep 10, 2014 #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!
     
  11. Sep 10, 2014 #10
    Why are you insisting on using a for next loop and setting the variable to zero? Use another loop.
     
  12. Sep 10, 2014 #11

    jedishrfu

    Staff: Mentor

    If I had a hammer...

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

    Lol :) I feel ya...
     
  14. Oct 12, 2014 #13
    For someone who likes the tradition of for next loops, it seems VERY odd to have the next line: i = 0






    (!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Cool Loops
  1. Problem in loops (Replies: 14)

  2. For loop in c (Replies: 3)

  3. Freal Loops? (Replies: 1)

  4. Loop in c (Replies: 1)

Loading...