Extra BASIC Practice Helpful Hints Text Version |
Slide 1 Extra BASIC Practice Helpful hints for: READ/DATA PRINT USING Writing Programs
Slide 2 INPUT - PROCESSING - OUTPUT READ/DATA statements take the place of LET statements used to assign values in the INPUT section.
A one-to-one correspondence is needed between the READ and DATA statements. The number of variables in the READ statement need to match the number of values in the DATA statement. The type of variable (numeric or string) in the READ statement must match the type of value in the DATA statement. DATA statements should be located at the end of the program just above the END statement.
Slide 3 PRINT USING PRINT USING statements allow the format of the output to be controlled. PRINT USING statements have three parts:
In the format string,
Number values can be formatted to print with commas, dollar signs, and percent signs. Columns of numbers can be aligned on the decimal point. Also, the precision of numbers is controlled.
Slide 4 The PLANETS Program Let's look at a sample program. Go to lesson and look at the code for the PLANETS.BAS program. At first glance, the program appears to be long! REM The Planets
Slide 5 The Sections Take a closer look! Do you see how the code in each section repeats? If we delete all the code for planets 2 through 8, the program is much shorter! REM The Planets
Slide 6 Hints for Writing a Program The next slides will break down the steps for writing the PLANETS program. Notice the focus is only on the first planet. Writing a "short" program is easier than a "long" one. It is much easier to debug! When the "short" program runs properly for the first planet, adding the code for the other planets 2 through 8 is a snap. Keep reading to learn how! REM The Planets
Slide 7 Starting a Program: Step 1 Start by setting up the READ/DATA statements for the first planet. Do the number of variables in the READ statement match the number of values in the DATA statements? Are the variables and values the same type? (numeric or string) Save and RUN the program to make sure it works. (You will only see a blank screen since there are no INPUT or PRINT statements.) REM The Planets
Slide 8 Processing: Step 2 Now add the calculations only for the first planet. Again, save and run your program. (The screen will be blank.) Advanced Programmers: If you want, you can add a quick PRINT statement to check your calculations. Be sure to delete it after you've checked the values (PRINT radius1, circ1). REM The Planets
Slide 9 Output: Step 3 Time to add the PRINT statement for the column headings and the PRINT USING for the data in the table. See the next screen for instructions for adding a PRINT USING statement. Save and run your program. Remember to focus only on the first planet! REM The Planets
Slide 10 Hints for PRINT USING Feel overwhelmed by PRINT USING statements? Let's look at them piece by piece. A PRINT USING statement has 3 parts: the key words PRINT USING, the format string and the variable list: PRINT USING "format string"; variable list When writing a PRINT USING statement, start by listing the variables to print and leave the format string blank: PRINT USING " "; planet1$; radius1; circ1
Slide 11 Hints for PRINT USING cont. Now fill in the format string. Look at the variable list. Ask yourself what type the first variable (planet1$) is. It is a string. That means we will use a pair of backslashes to show the computer where to print the planet's name: PRINT USING "\ \"; planet1$; radius1; circ1 Now look at the next variable (radius1). It is of type numeric. That number should be printed with commas: PRINT USING "\ \ ##,###"; planet1$; radius1; circ1 The next variable is circ1. It is of type numeric. The circumference should be printed with commas and 2 decimal places: PRINT USING "\ \ ##,### #,###.##"; planet1$; radius1; circ1
Slide 12 Adding Other Planets Now that you've written the program for the first planet, you can easily add the code for planets 2 through 9. Go to the next slide to learn how to copy and paste. REM The Planets
Slide 13 Copy and Paste Yes, you can copy and paste in the QBASIC editor! Use your mouse to click and drag to highlight a row or if the row scrolls off the screen, click at the beginning of the row, press Shift and the down arrow. Now go to the edit menu and choose copy. Next, click where you want to paste. Go to the edit menu and choose paste. REM The Planets
Slide 14 Overwrite Mode Now we need to change the 1s to 2s, 3s, 4s, etc. You can delete the 1's and type the new numbers or switch to overwrite mode. Press the insert key on your keyboard (notice your cursor is a large box instead of a thin line), click on a 1 you need to change to a 2 and press 2. The 1 changed to a 2 without having to press the delete key. Be sure to switch back to insert mode by pressing the insert key again. REM The Planets |