UNIHIVE

Study Hub UNIPORT CSC 180.1

CSC 180.1 · Introduction to Computer Science and BASIC Programming

Input and output statements

INPUT, READ and DATA, RESTORE, PRINT with commas and semicolons, and simple formatting.

Our notes

2 min read

Getting data into a program

INPUT

Asks the user to type data while the program runs.
10 INPUT "ENTER YOUR SCORE"; S
Good for interactive programs; the user must type values in the right order.

READ and DATA

Values are stored in the program itself in DATA lines and read in order.
10 READ A, B, C
20 PRINT A + B + C
30 DATA 4, 7, 10
40 END
Output: 21. DATA lines can be anywhere in the program. Reading more items than the DATA holds gives an "Out of DATA" error.

RESTORE

Sends the pointer back to the first DATA item so the data can be read again.

Output: PRINT

  • PRINT "HELLO" prints text.
  • PRINT A prints a value (positive numbers often have a leading space).
  • PRINT alone prints a blank line.
  • Comma (,) moves to the next print zone (columns about 14 characters wide).
  • Semicolon (;) prints the next item immediately after.
  • A comma or semicolon at the end of a line keeps the cursor on that line for the next PRINT.

Worked example

10 LET N$ = "ADA"
20 LET S = 72
30 PRINT "NAME:"; N$, "SCORE:"; S
Prints NAME:ADA in the first zone and SCORE: 72 in the next.

Formatting output (QBasic and many dialects)

PRINT USING "###.##"; X prints X with two decimal places, e.g. 3.14159 as " 3.14". TAB(n) moves to column n.

A complete example: simple interest

10 REM SIMPLE INTEREST
20 INPUT "PRINCIPAL, RATE, TIME"; P, R, T
30 LET I = P * R * T / 100
40 PRINT "INTEREST ="; I
50 PRINT "AMOUNT ="; P + I
60 END
For P = 50,000 (naira), R = 10, T = 2: I = 10,000; amount 60,000.

Choosing INPUT or READ/DATA

INPUTREAD/DATA
data typed while runningdata written into the program
interactive, changes each rungood for fixed tables and testing
slower for large datafast; no typing at each run

Exams usually ask

  • Distinguish INPUT from READ/DATA, with examples.
  • Give the output of PRINT statements with commas and semicolons.
  • Write a program that reads data and prints a calculated result.

Common mistake

Putting more variables in READ than values in DATA (or values in the wrong order). Match them one to one.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 180.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.