Do Loops

        One of the things that you will find your self doing while programming is performing some operation or function, more than once, or many times. The easiest way to do this is not to type in the same code 50 times, there is a way that you can perform the same code over and over again. This is called a 'loop'.
        There are 3 main types of loops in QBasic, the first one that I'm going to talk about is the DO LOOP. The basic syntax of a DO LOOP has 4 different forms, but they are all related. The first is called a 'pre-test' loop, this is called that because in a loop you execute the block of code until some condition is met, similar to an IF statement. The test is evaluated first as the name pre-test suggests Here is an example of a pre-test loop:
        X% = 0
        DO WHILE X% < 10
          X% = X% + 1
          PRINT X%
        LOOP

Output:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10

        As you can see this loop repeated itself until the condition is met. If for some reason your loop does not have a condition that can be or will ever be met like 1 = 2, or if there is no condition at all (which is legal for this type of loop) then the loop will continue to loop forever.

        The second type of loop is also a pre-test loop, the difference with this one is that instead of looping WHILE a condition is true, it loops UNTIL a condition is true. Here is an example:
        X% = 0
        DO UNTIL X% = 10
          X% = X% + 1
          PRINT X%
        LOOP

Output:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10

        As you can see from this example, the result of the loop is the same, but the condition is different. This is due to the UNTIL statement instead of the WHILE
        The next type of loop to discuss (which is still a DO-LOOP) is what is know as a post-test loop. I'm sure you can guess what exaclty that means from the previous examples. Essentially a post-test loop does the condition test last instead of first.
The first example is a loop WHILE a condition is true, like the first example above, and the second example loops UNTIL a condition is met. Here are the examples:
Example 1:
        X% = 0
        DO
          X% = X% + 1
          PRINT X%
        LOOP WHILE X% < 10

Output:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10

Example 2
        X% = 0
        DO X% = 10
          X% = X% + 1
          PRINT X%
        LOOP UNTIL X% = 10

Output:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10

        As you can see all of these loops are the same, with the same output, what is different is where the conditions are and how they are put. It is important to understand the different use of WHILE and UNTIL, as it will come in handy as you progress with this language, and any language in general.

    Back         Tutorial Part 1         Next