Task 6: Looping
STEP It Up
STEP It Down
Your Turn - MenuLoop.BAS
Computer programs function by doing the same things over and over again at very high speed. This looping behavior enables the user to continue to use the program until the loop is stopped by either the program or the user. In the gaming world the faster a game program can loop the better. Gamers like this because it enables their games to have high frame rates known to gamers as the FPS (frames per second).
Remember the program we created in Task 5 that used GOTO to create a loop that counted from 1 to 24?
There is a much better and preferred method to create a loop that does the same thing.
The FOR...NEXT statement is used to create controlled loops. A controlled loop is one where you explicitly know the start and end values of the loop. FOR...NEXT statements can count forward or backward in any increment you wish. The default behavior for a FOR...NEXT loop is to count forward by 1 (a STEP value of 1) until the end value has been reached.
FOR Count% = 1 to 24
The integer variable Count% is used to hold the value of the FOR...NEXT loop. 1 TO 24 indicates the start and end values of the loop counter. The first time the loop executes Count% will contain the value of 1, the second loop execution the value of 2, and so on. This continues to happen until Count% finally reaches the end value of 24. This line controls when the loop ends:
Only when the NEXT statement sees that Count% equals the ending value specified will it end the loop. All code contained within the code block between the FOR and NEXT statements is executed over and over again while the loop is active.
STEP It Up
The STEP statement may be used in conjunction with the FOR...NEXT statement to change the default direction of the count. Save the previous example as ForNext.BAS before typing in the next program and executing it.
The new statement CLS is used to clear the screen. It clears all text and graphics currently displayed.
The example program above uses two FOR...NEXT loops, one inside the other, creating what is called a nested loop. The outer loop, Cstep%, counts from 1 to 4 while the inner loop, Count%, counts from 0 to 12 with a STEP value based on the current value of Cstep%. It just so happens that counting from 0 to 12 by any of the Cstep% values allows the loop to end on the number 12 each time because 12 is evenly divisible by the numbers 1 through 4. However, what would happen if we use a STEP value that mathematically can't reach the ending number? If the next STEP value equals or is greater than the ending loop value, the loop is considered complete. Make the following modifications to the above code to see this in action.
Change line 15 to read:
PRINT "Counting from 1 to 12 by steps of"; Cstep%
Change line 17 to read:
FOR Count% = 1 to 12 STEP Cstep%
Now, execute the program again.
As you can see STEP will only reach the value of 12 by counting in steps of 1. Counting in steps of 2, 3, or 4 can't mathematically reach the number 12. This is important to remember when writing your programs. Save this program as StepUp.BAS before continuing on.
STEP It Down
STEP can also be assigned a negative value to allow FOR...NEXT loops to count in reverse. Type in the following code and then execute it to see this in action.
Yes, I know, a pretty crude representation of a rocket launch. We'll revisit this code when graphics are introduced and spruce it up a bit.
There are three new commands in the example program above: SLEEP, _DELAY, and SYSTEM.
SLEEP puts the computer into a continuous loop for a given number of seconds which in effect stops program execution for that amount of time. If you do not specify the number of seconds to SLEEP the program will pause until the user presses a key. Also, the user can over-ride the SLEEP time at any point by pressing a key.
_DELAY (yes, the underscore is part of the command, more on that later) performs the same action as SLEEP but gives the programmer finer control over the timing. SLEEP will only work with whole seconds while _DELAY allows the programmer to specify the timing duration down to the millisecond, or thousandths of a second. _DELAY .001 would make the program pause for one one thousandth of a second. In the program above _DELAY .1 forces the program to pause for 100 milliseconds or one tenth of a second.
SYSTEM forces the text window to close immediately once the program has finished. No "Press any key to continue" message is displayed. The program simply ends, the window is closed, and the operating system is given back control.
Go ahead and save this program as Liftoff.BAS before continuing.
Another type of looping construct offered in programming is a conditional loop. Conditional loops continue looping until a certain condition is met. There are a few different methods of creating conditional loops in QB64. The first one we'll discuss is the DO...LOOP UNTIL statement. Type in the following code and then execute it.
The block of code contained in between the DO and LOOP UNTIL statements is looped until the LOOP UNTIL condition becomes true. Only when Count% contains the value of 24 does the loop exit. It's important to note that the code block with this form of DO...LOOP will always be executed at least one time since the conditional check isn't made until after the code block has been executed.
Here's a little sneak peek of things to come with a graphics program that uses a DO...LOOP construct that becomes true when the user presses the escape (ESC) key. Save the previous example as DoLoop.BAS before typing in the next example and executing it.
As you can see in the example code above there are a number of commands that have not been covered yet. Instead of explaining each new command in every piece of example code encountered it will be left up to you to investigate the new commands that are not covered immediately. Future tasks will eventually get to the commands not discussed yet. An alphabetical listing of QB64 commands can be found here.
Save this program as Circle.BAS so we can revisit it in a later task.
The DO...LOOP statement has a couple of variations that can be used.
DO WHILE <condition is true>
.. <code block>
The code block in this variation may never get executed because the condition is checked before the code block is entered. The condition must be true before the code block is entered.
DO UNTIL <condition is true>
.. <code block>
Again, the code block may never get executed because of the condition being tested before it. The condition must be false however before the code block is entered.
.. <code block>
LOOP WHILE <condition is true>
This variation continues to loop as long as the condition is true. The code block will get executed at least one time since the conditional check is done after the code block.
No DO...LOOP variation is considered better than another. The one you decide to use will depend on your program requirements at the time.
Another method of implementing a conditional loop is with the use of a WHILE...WEND statement. Type in the following program and execute it.
The code block in between a DO...LOOP structure must be executed at least once before the loop's condition is checked. However, a WHILE...WEND structure allows the programmer to skip a code block completely because the condition is checked before the loop is entered. If the user enters a number below 101 the loop will execute at least once.
WHILE Count% < = 100
As long as Count% is less than or equal to ( <= ) 100 the code block in the loop will be executed. If the user were to enter 110 the WHILE statement's condition would be false resulting in the code block never being executed.
The following relational operators can be used to check for conditions: = (equal to), < (less than), > (greater than), <> (not equal to), <= (less than or equal to) and >= (greater than or equal to).
Create a program that combines the conversion utilities described in Task 4 into a menu driven program. Figures 1 and 2 below show what the output of your program should look like.
Figure 1 - Fahrenheit to Celsius
Figure 2 - Inches to Millimeters
- Invalid menu entries should be ignored.
- When the user enters 3 to end the program the program should immediately terminate back to the operating system.
- The screen should be cleared before the menu is displayed.
- You'll need the following commands to complete this task: DIM, DO...LOOP, CLS, PRINT, IF...THEN, INPUT, SLEEP, and SYSTEM
- Save the program as MenuLoop.BAS when finished.
Click here to see the solution.
New commands introduced in this task:
DO...LOOP (and variations)
New concepts introduced in this task:
Commands learned in Task 5:
SELECT CASE...END SELECT
colon ( : )
Concepts learned in Task 5:
order of operations
Commands learned in Task 4:
REM or '
Concepts learned in Task 4:
operators ( +, -, *, / )
Commands learned in Task 3:
Concepts learned in Task 3: