Thursday, March 14, 2013

Input and Output

Input and Output

In a program to input or output data it may be necessary to supply information about:

1 The input or output device to be used. (In BASIC on a microcomputer the input is usually assumed to be from the keyboard and output to the screen. However, the programmer can specify other devices if required.)

2 The format of the data on the input or output medium-i.e. the way the data is to be setout. (In BASIC output format is mainly controlled within PRINT statements.

e.g. 100 PRINT TAB(10); "HELLO THERE";Name1$,"AND";Name2$ might print:


The Tab(10) would cause HELLO THERE to start after column 10. The semicolon causes the next item to be printed without the cursor moving. The comma causes the next item to be printed in the next 'zone' of the screen. The screen is divided up into a number of equal zones of about 10 to 20 columns each depending on the computer.)

3 The variables being used to store the data being input or output. In BASIC input these appear in a list separated by commas, for example,



A rogue value or data terminator is a special value put at the end of a list of data items SO that the end of the list can be detected. The computer has to check every item read to see if it is the rogue value.

The rogue value:

1 Has to have a value that cannot be mistaken for real data.

2 Is in the same format as the rest of the data, so that it will be read without producing an

Example of a rogue value

The following is part of a BASIC program. Here the rogue value is ***.

130 REM /// Convert heights to centimetres ///

140 PRINT "For each one type in name,feet,inches. Type '***,0,0' to finish"

150 INPUT names$,feet,ins

160 IF name$="***" THEN GOTO 500: REM /// CONTINUE REST OF PROGRAM ///

170 PRINT name$;" has height ";(feet*12+ ins)*2.54;" cms "

180 GOTO 150

Control Statements

Programmers need to be able to use the 3 main types of component of algorithms:

1 Sequence.

2 Repetition.

3 Selection.


Each of the statements in a group is carried out once and in the order given.

In most high level languages statements are normally executed in sequence unless the programmer does something to change that. (See Fig 3.)

Input and Output 2

Fig 3 Flowchart showing statements executed in sequence   Fig4 Flowchart showing how an IF . THEN. statement is executed

In a language such as BASIC the programmer can transfer control out of the sequence to another part of the program.

Examples of BASIC statements which transfer control

1 300 GOTO 500

2 250 GOSUB 1000

3 110 IF A > 3 THEN GOTO 200

Statements containing GOTO are easy to use. However, programs which contain them are often difficult to understand. It is good programming practice to avoid using GOTO if possible.


A choice is made between two or more different options. Only one of the options is carried out.

1 IF .. THEN

Test the condition after the IF. If the condition is TRUE then carry out the statements). If it is FALSE ignore the statement(s) and carryon. (See Fig 4.)

2 IF ... THEN .. ELSE ... -carry out one o{two possible actions.

Test the condition after the IF. If the condition is TRUE carry out the statement(s) after the THEN. If the condition is FALSE carry out the statement(s) after the ELSE. (See Fig 5.)

Examples of IF ... THEN ... statements in BASIC

1 150 IF Al=A2 THEN Comparison$="EQUAL"

2 1000 IF age> 100 OR name$="LAST" THEN PRINT TOTAL

Example of IF ... THEN ... ELSE ... in COMAL

In COMAL an IF statement taking up more than one line ends with an ENDIF: 100 IF age >= 17

110 THEN PRINT "You may apply for a provisional licence"

120 ELSE

130 PRINT "Sorry, you are still too young to drive"


Input and Output  3

Fig5 Flowchart showing how an IF. . THEN. . ELSE. . statement is executed


This is a statement to carry out one of a number of possible actions.

The facilities for selecting one of a number of alternatives differs from one language to

another. In COMAL the statements are:

CASE (Expression) OF

WHEN (List1 of expressions) (Action 1)

WHEN (List2 of expressions) (Action 2)

etc .


The expressions in the lists after WHEN all give possible values of the expression after CASE. If the value chosen is in List! then the computer will take Action1, etc.

Usually the expression after CASE is just a variable and the expressions after the WHENs are just values of it.

The OTHERWISE is not necessary. It is used if there are some cases not covered in the other lists.

Example of CASE statement in COMAL

The example is part of a program to find how many letters, words and sentences there are in a piece of text. Each character is checked in turn.

500 CASE character$ OF

510 WHEN" "

520 words: = words +1

530 WHEN "."

540 sentences: = sentences +1

550 WHEN "A","B","C","D","E","F","G","H","I","J"."K"."L"."M"."N"."O"."P","Q","R","S"."T"."U"."V"."W"."X"."Y","Z",.

560 letters : = letters + l


580 oddments: = oddments + 1



In repetition a statement or a sequence of statements is repeated..

A loop is a sequence of statements which is repeated until some condition is met.

In a language such as BASIC the programmer can write loops using GOTO and IF statements.

Example of a loop in BASIC

100 REM H **** Program to print out 10 numbers and their squares *****

110 LET I=0

120 LET I=I+1

130 PRINT "The square of ";1;" is ";I*I

140 IF I<=10THENGOTo 120


Most languages have special looping structures built in. In many languages there are three main types of looping structure:

1 The FOR loop-Repetition a given number of times. (See Fig 6.) 2 The REPEAT ... UNTIL loop- Repetition until a condition is met.

The loop is always carried out at least once. This is because the test to see whether it should finish is at the end of the loop. (See Fig 7.)

3 The WHILE loop-Repetition while a condition is true.

Here the condition is tested at the beginning. So if it. is not true to start with then the loop will not be executed at all. (See Fig 8.)

Examples of a FOR loop

1 The following BASIC program performs the same task as the first loop example (above). 100 FOR 1~1 TO 10

110 PRINT "The square of ";1;" is ";1"1

120 NEXT I

2 The previous program (example 1) would also work in COMAL (using: = in the FOR statement). COMAL also allows a FOR loop which only repeats one statement to be done without a NEXT. A DO is placed between the FOR statement and the repeated statement. The previous example becomes:

100 FOR' : = 1 TO 10 00 PRINT "The square of ";1;" is ";1'"

3 Loops are often used with arrays (see Unit 1104). The following BASIC example is a module of a program. The array MARK has been dimensioned and had values read into it earlier in the program. This module finds the highest mark in the array and prints it out.

500 LET highest=0

510 FOR 1=1 TO numval

520 If mark(l) < highest THEN GOTO 540 530 highest = mark(l)

540 NEXT I

560 PRINT "the highest mark is "; highest


highest is highest mark so far numval is number of marks

Input and Output 5

Fig 6 Flowchart showing the order in which a FOR loop is executed

Example of a REPEAT ... UNTIL loop in COMAL

100 REM *** Selecting from a menu-correct options are 1 to 7 *** 110 PRINT "Type ";


130 PRINT "a whole number from 1 to 7, please";

140 INPUT Option

150 UNTIL Option>0 and Option<8 AND INT(Option) = Option


1 INT is a function . INT(X) produces the whole number below X. The condition in the UNTIL statement will only be fulfilled if OPTION is a whole number between 1 and 7 inclusive. The loop will be repeated until this happens.

2 This example would also be correct in some versions of BASIC.

Example of a WHILE loop in COMAL

100 // To find the total of a set of marks //

110 Total: ==0

200 PRINT "Type in the marks, pressing RETURN after each one"

210 PRINT "After the last mark enter -1"

220 INPUT "First mark ": Mark

240 WHILE Mark> = 0 DO

250 Total: = Total + Mark

260 INPUT "Next mark ": Mark


280 PRINT "Total mark is "; Total


1 In COMAL a WHILE loop is ended with an ENDWHILE.

2 -1 is a rogue value for the input data .

Input and Output 

Fig7 Flowchart showing the order in which a REPEAT ... UNTIL loop is executed

Fig8 Flowchart showing the order in which a WHILE loop is executed.