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.

READ contains the variables
DATA contains the values

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:

PRINT USING "format string"; variable list

In the format string,

A pair of backslashes (\ \) are used to show where and how to print string values.
A set of number signs (or pound signs) (#) are used to show where and how to print numeric values.

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
REM Penny Programmer
REM 02/20/2001
'
REM Input Data
READ planet1$, diameter1
READ planet2$, diameter2
READ planet3$, diameter3
READ planet4$, diameter4
READ planet5$, diameter5
READ planet6$, diameter6
READ planet7$, diameter7
READ planet8$, diameter8
'
REM Calculate Radius
radius1 = diameter1 / 2
radius2 = diameter2 / 2
radius3 = diameter3 / 2
radius4 = diameter4 / 2
radius5 = diameter5 / 2
radius6 = diameter6 / 2
radius7 = diameter7 / 2
radius8 = diameter8 / 2
'
REM Calculate Circumference
CONST pi = 3.14
circ1 = 2 * pi * radius1
circ2 = 2 * pi * radius2
circ3 = 2 * pi * radius3
circ4 = 2 * pi * radius4
circ5 = 2 * pi * radius5
circ6 = 2 * pi * radius6
circ7 = 2 * pi * radius7
circ8 = 2 * pi * radius8
'
REM Output
COLOR 2, 4
CLS
PRINT TAB(15); "The Planets"
PRINT "Planet"; TAB(15); "Radius (miles)"; TAB(35); "Circumference (miles)"
PRINT USING "\ \ ##,### ###,###.##"; planet1$; radius1; circ1
PRINT USING "\ \ ##,### ###,###.##"; planet2$; radius2; circ2
PRINT USING "\ \ ##,### ###,###.##"; planet3$; radius3; circ3
PRINT USING "\ \ ##,### ###,###.##"; planet4$; radius4; circ4
PRINT USING "\ \ ##,### ###,###.##"; planet5$; radius5; circ5
PRINT USING "\ \ ##,### ###,###.##"; planet6$; radius6; circ6
PRINT USING "\ \ ##,### ###,###.##"; planet7$; radius7; circ7
PRINT USING "\ \ ##,### ###,###.##"; planet8$; radius8; circ8
'
REM Data Statements
DATA Mercury,3032
DATA Venus,7521
DATA Earth,7926
DATA Mars,4213
DATA Jupiter,88732
DATA Saturn,74975
DATA Uranus,31763
DATA Neptune,30603
'
END

 

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
REM Penny Programmer
REM 02/20/2001
'
REM Input Data
READ planet1$, diameter1
'
REM Calculate Radius
radius1 = diameter1 / 2
'
REM Calculate Circumference
CONST pi = 3.14
circ1 = 2 * pi * radius1
'
REM Output
COLOR 2, 4
CLS
PRINT TAB(15); "The Planets"
PRINT "Planet"; TAB(15); "Radius (miles)"; TAB(35);
"Circumference (miles)"
PRINT USING "\ \ ##,### ###,###.##"; planet1$; radius1; circ1
'
REM Data Statements
DATA Mercury,3032
'
END

 

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
REM Penny Programmer
REM 02/20/2001
'
REM Input Data
READ planet1$, diameter1
'
REM Calculate Radius
radius1 = diameter1 / 2
'
REM Calculate Circumference
CONST pi = 3.14
circ1 = 2 * pi * radius1
'
REM Output
COLOR 2, 4
CLS
PRINT TAB(15); "The Planets"
PRINT "Planet"; TAB(15); "Radius (miles)"; TAB(35); "Circumference (miles)"
PRINT USING "\ \ ##,### ###,###.##"; planet1$; radius1; circ1
'
REM Data Statements
DATA Mercury,3032
'
END

 

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
REM Penny Programmer
REM 02/20/2001
'
REM Input Data
READ planet1$, diameter1

REM Data Statements
DATA Mercury,3032
'
END

 

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
REM Penny Programmer
REM 02/20/2001
CLS
'
REM Input Data
READ planet1$, diameter1

REM Calculate Radius
radius1 = diameter1 / 2
'
REM Calculate Circumference
CONST pi = 3.14
circ1 = 2 * pi * radius1

REM Data Statements
DATA Mercury,3032
'
END

 

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
REM Penny Programmer
REM 02/20/2001
CLS
'
REM Input Data
READ planet1$, diameter1

REM Calculate Radius
radius1 = diameter1 / 2
'
REM Calculate Circumference
CONST pi = 3.14
circ1 = 2 * pi * radius1

REM Output
COLOR 2, 4
CLS
PRINT TAB(15); "The Planets"
PRINT "Planet"; TAB(15); "Radius
(miles)"; TAB(35); "Circumference
(miles)"
PRINT USING "\ \ ##,###
###, ###.##"; planet1$; radius1; circ1
'
REM Data Statements
DATA Mercury,3032
'
END

 

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
REM Penny Programmer
REM 02/20/2001
CLS
'
REM Input Data
READ planet1$, diameter1

REM Calculate Radius
radius1 = diameter1 / 2
'
REM Calculate Circumference
CONST pi = 3.14
circ1 = 2 * pi * radius1

REM Output
COLOR 2, 4
CLS
PRINT TAB(15); "The Planets"
PRINT "Planet"; TAB(15); "Radius
(miles)"; TAB(35); "Circumference
(miles)"
PRINT USING "\ \ ##,### ###,###.##";
planet1$; radius1; circ1
'
REM Data Statements
DATA Mercury,3032
'
END

 

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
REM Penny Programmer
REM 02/20/2001
'
REM Input Data
READ planet1$, diameter1
READ planet2$, diameter2
READ planet3$, diameter3
READ planet4$, diameter4
READ planet5$, diameter5
READ planet6$, diameter6
READ planet7$, diameter7
READ planet8$, diameter8

 

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
REM Penny Programmer
REM 02/20/2001
'
REM Input Data
READ planet1$, diameter1
READ planet2$, diameter2
READ planet3$, diameter3
READ planet4$, diameter4
READ planet5$, diameter5
READ planet6$, diameter6
READ planet7$, diameter7
READ planet8$, diameter8
'


previous