Custom Search
 
  

 
Sample Problem Using Control Statements

Now lets examine a program which contains an example of each type of control statement discussed: GOTO, IF-THEN, and ON-GOTO. In this example we will be converting temperatures from Fahrenheit to Celsius and vice versa. Suppose we want to write a program to solve this prob-lem, and to warn us if any temperature we convert to Celsius is over 1000 Celsius. The first step in solving the problem is to formally define the problem to be sure we know exactly what problem we want to solve, and what its inputs and outputs are to be. The PROBLEM DEFINITION could be written as shown in figure 4-1.

The procedure outlined in the programming flowchart and coded program (figure 4-2) will solve our problem.

Study the flowchart and the program. To help, the statement numbers have been placed on the flowchart above each corresponding flowchart symbol. Line 10 will print a message prompting you to input the temperature you want converted. The program will then ask which conversion you want; you respond with 1 for F to C, or 2 for C to F. Line 60 will evaluate your response to determine whether to branch to line 90 or line 150 (conditional branch). 

If you entered anything other than a 1 or 2 at line 50, youll get a message and the program will branch back to line 50 because of the GOTO 50 in line 80. If you entered a 1, control will be transferred from line 60 to line 90, where the value you input at line 20 will be converted to a Celsius value. Line 100 will take this value and check it to determine if it is greater than 100. If it is, control is transferred to line 130 where the value for C and a message is printed. Line 140 is executed next. It is a GOTO statement (unconditional branch) which transfers control to line 170. At this point you are given the option to terminate the program.

Lets assume you enter a Y (line 180), the program will branch back to line 10. Now you may enter another value to be converted. This time enter a 2 at line 50. The ON-GOTO statement, line 60, will transfer control to line 150, which will convert the value entered at line 20 into a Fahrenheit value, and print the value of F and a message. Once this is done, line 170 will again be executed.

PROBLEM DEFINITION

TEMPERATURE CONVERSIONThis program is to convert temperatures from Fahrenheit (F) to Celsius (C) and from Celsius to Fahrenheit. The formulas to do this are: C = (F 32)*5/9 and F = 9/5*C + 32. The program is also to print a warning beside any Celsius value greater than 100 degrees Celsius. The program is to contain a loop to allow for repeated executions to process more input.

INPUT: The input for this program is to be either a Fahrenheit or Celsius value which is to be input as variable X. Another variable, T, will be used to indicate the type conversion you wish to do; Fahrenheit to Celsius or Celsius to Fahrenheit. String variable Q$ is to be used to specify whether there is more input or not. All values for these variables will be entered into the system via the terminal at the time of program execution.

OUTPUT: For each temperature entered, print it and its converted value with a caption to indicate whether it is Fahrenheit or Celsius and for any Celsius value greater than 100 degrees, print the word "CAUTION" beside it.

Figure 4-1.Temperature Conversion Problem Definition.

Figure 4-2.Temperature Conversion Flowchart and Program.

Now take some values you know the Fahrenheit and Celsius equivalent of, and trace them through the program. See what paths would be taken and determine if the program is correct. This procedure, discussed in Chapter 1, is known as "desk checking." You might start with 32 degrees Fahrenheit. Trace it through the program to be sure you get 0 degrees Celsius. Next take 212 degrees Fahrenheit; did you get 100 degrees Celsius? Try 100 degrees Celsius; did you get 212 degrees Fahrenheit? If you take 213 degrees Fahrenheit, you should get 100.555 degrees Celsius and the message, CAUTION; did you? Once you have tested with known values to test the various program paths, you can be reasonably sure that the program will run and produce accurate results.

SUMMARY

The READ and DATA statements can be used to introduce data into a program. READ statements cause data to be read from a data list specified in a DATA statement and assigned the variable names specified in the READ statement. When coding DATA statements, be sure the values in the DATA statements are in the same order as the variable names specified by the READ statements.

Data may also be introduced into a program at the time the program is executed by using the INPUT statement. The INPUT statement specifies one or more variable names; each must be separated by a comma. It is good programming practice to precede INPUT statements with a PRINT statement to print a message to remind you what data is to be input and in what order. Data introduced into the program using the INPUT statement is not retained by the program after the program has been executed.

Transfer of control statements may be used in a program when you do not want to execute statements in sequence, or when you want to execute agroup of statements repeatedly, forming a loop. The unconditional transfer of control statement, GOTO, is used when you always want to alter the normal sequence, either to bypass some statements or to branch back to the beginning of a loop. The conditional transfer of control statements, IF-THEN and ON-GOTO are used when you only want to change the normal sequence of execution if given conditions exist. These can be used to control a loop or to branch to selected statements.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business