Source Program Divisions

From CometWiki

(Difference between revisions)
Jump to: navigation, search
(New page: == Source Program Divisions == Internet Basic source programs are arranged into three functional divisions. '''Data Division''' The Data Division defines the data variables to be used ...)
 
(5 intermediate revisions not shown)
Line 28: Line 28:
Here's a sample program (note that this program does not contain any instructions in the I/O Format Division).  
Here's a sample program (note that this program does not contain any instructions in the I/O Format Division).  
-
! Data Division
+
  ! Data Division
-
!
+
    LENGTH 1 & LOCAL Choice$,Answer$
-
LENGTH 6.2
+
    LENGTH 6.2 & LOCAL Fahrenheit,Celsius
-
LOCAL Fahrenheit,Celsius
+
  ! I/O Format Division
   !
   !
-
LENGTH 1
 
-
LOCAL Choice$,Answer$
 
   !
   !
-
! I/O Format Division
+
  ! Procedure Division
-
!
+
  !    
-
!
+
-
! P rocedure Division
+
-
!
+
   Top: Print (CS)
   Top: Print (CS)
  !
  !
-
Print "This program converts between temperature scales." , @(0,3)
+
    Print "This program converts between temperature scales." , @(0,3)
  !
  !
-
Print "Enter 1 to convert from Fahrenheit to Celsius, or" , @(0,5)
+
    Print "Enter 1 to convert from Fahrenheit to Celsius, or" , @(0,5)
-
Print "enter 2 to convert from Celsius to Fahrenheit."    , @(0,6)
+
    Print "enter 2 to convert from Celsius to Fahrenheit."    , @(0,6)
  !
  !
-
Choice:
+
  Choice:
-
Print "Your choice?" , @(0,8)
+
    Print "Your choice?" , @(0,8)
-
Input @(14,8),Choice$
+
    Input @(14,8),Choice$
   !
   !
-
SELECT CASE Choice$
+
    SELECT CASE Choice$
   !
   !
-
  CASE ""
+
    CASE ""
-
    STOP
+
    STOP
   !
   !
-
  CASE "1"
+
    CASE "1"
-
    Print "Enter Fahrenheit temperature:" , @(0,10)
+
    Print "Enter Fahrenheit temperature:" , @(0,10)
-
    Input @(30,10),Fahrenheit
+
    Input @(30,10),Fahrenheit
-
    Celsius = (Fahrenheit - 32) / 1.8
+
    Celsius = (Fahrenheit - 32) / 1.8
-
    Print "Celsius temperature is:",@(0,12) ; Celsius,@(30,12)
+
    Print "Celsius temperature is:",@(0,12) ; Celsius,@(30,12)
   !
   !
-
  CASE "2"
+
    CASE "2"
-
    Print "Enter Celsius temperature:" , @(0,10)
+
    Print "Enter Celsius temperature:" , @(0,10)
-
    Input @(30,10),Celsius
+
    Input @(30,10),Celsius
-
    Fahrenheit = Celsius*1.8 + 32
+
    Fahrenheit = Celsius*1.8 + 32
-
    Print "Fahrenheit temperature is:",@(0,12) ; Fahrenheit,@(30,12)
+
    Print "Fahrenheit temperature is:",@(0,12) ; Fahrenheit,@(30,12)
   !
   !
-
  CASE ELSE
+
    CASE ELSE
-
    Print "Please enter 1 or 2." , @(0,10)
+
    Print "Please enter 1 or 2." , @(0,10)
-
    GOTO Choice
+
    GOTO Choice
   !
   !
-
ENDSELECT
+
    ENDSELECT
   !
   !
-
Print "Would you like to perform another conversion? (Y/N)" , @(0,14)
+
    Print "Would you like to perform another conversion? (Y/N)" , @(0,14)
-
Input @(52,14),Answer$
+
    Input @(52,14),Answer$
   !
   !
-
Answer$ = UCASE(Answer$)
+
    Answer$ = UCASE(Answer$)
   !
   !
-
IF Answer$ = "Y" THEN
+
    IF Answer$ = "Y" THEN
-
  GOTO Top
+
    GOTO Top
-
ELSE
+
    ELSE
-
  STOP
+
    STOP
-
ENDIF
+
    ENDIF
   !
   !
     END
     END

Latest revision as of 08:16, 31 May 2009

Source Program Divisions

Internet Basic source programs are arranged into three functional divisions.

Data Division The Data Division defines the data variables to be used in the program.

I/O Format Division The I/O Format Division establishes the exact formats and device control codes for all input/output aspects of the program.

Procedure Division The Procedure Division, contains the executable statements of the program, including data initialization and assignment, input and output statements, program flow control, decision making and looping, and exception handling.

So, a typical Internet Basic source program looks like this:

Data Division
.
.
I/O Format Division
.
.
Procedure Division
.
.

Note: Even though the Data Division and I/O Format Division are shown as separate sections of a source program, they may be combined and intermixed. That is, format statements may be used in the Data Division and vice versa. The only requirement is that data elements used in format statements must be defined prior to their use. Other than that, it is sometimes more convenient to think of the Data Division and I/O Format Division as one section of the source program, rather than two.

Here's a sample program (note that this program does not contain any instructions in the I/O Format Division).

 ! Data Division
   LENGTH 1 & LOCAL Choice$,Answer$
   LENGTH 6.2 & LOCAL Fahrenheit,Celsius
 ! I/O Format Division
 !
 !
 ! Procedure Division
 !      
 Top:	Print (CS)
!
    Print "This program converts between temperature scales." , @(0,3)
!
    Print "Enter 1 to convert from Fahrenheit to Celsius, or" , @(0,5)
    Print "enter 2 to convert from Celsius to Fahrenheit."    , @(0,6)
!
 Choice:
    Print "Your choice?" , @(0,8)
    Input @(14,8),Choice$
 !
    SELECT CASE Choice$
 !
    CASE ""
    STOP
 !
    CASE "1"
    Print "Enter Fahrenheit temperature:" , @(0,10)
    Input @(30,10),Fahrenheit
    Celsius = (Fahrenheit - 32) / 1.8
    Print "Celsius temperature is:",@(0,12) ; Celsius,@(30,12)
 !
    CASE "2"
    Print "Enter Celsius temperature:" , @(0,10)
    Input @(30,10),Celsius
    Fahrenheit = Celsius*1.8 + 32
    Print "Fahrenheit temperature is:",@(0,12) ; Fahrenheit,@(30,12)
 !
    CASE ELSE
    Print "Please enter 1 or 2." , @(0,10)
    GOTO Choice
  !
    ENDSELECT
 !
    Print "Would you like to perform another conversion? (Y/N)" , @(0,14)
    Input @(52,14),Answer$
  !
    Answer$ = UCASE(Answer$)
  !
    IF Answer$ = "Y" THEN
    GOTO Top
    ELSE
    STOP
    ENDIF
  !
    END
Personal tools