Program Control
Understanding what reserved words and keywords exist is vital to learning any programming language. Next, however, is learning how to use them and when. We call this write the Program Control, think of it as an executive summary. In this chapter, we will address the Program, Data, Looping, Logic and a little on Objects. The Program is the product as a whole, defining the goal and then through code implementing the functionality to achieve the goal.- Each Program must have a "Begin" and "End.".
- When the Program is executed, the "Begin" and "End." are executed, and all code after the "End." is ignored.
- The code in the "Begin" and "End." determines what your program does.
- No matter how much code you have written, if the "Begin" and "End." do nothing, your program does nothing.
program WorkFlow; var Choice:Longint; procedure Tell; begin With Session do Begin ClrScr; WriteLn('Official Presidential Control Panel'); Writeln(''); WriteLn('1. Start the coffee maker.'); WriteLn('2. Start World War III.'); WriteLn('3. Start the electric pencil sharpener.'); End; end; function Choose:Longint; var Choice:Longint; begin repeat Session.Write('Enter 1,2 or 3: '); Choice:=StrToIntDef(Session.Ask('Enter 1,2 or 3',' ',False,False),-1); If (Choice<1) or (Choice>3) then Choice:=-1; If Choice=-1 then Session.WriteLn('Entry Error. Type a number in the range of 1-3.'); until (Choice>-1); Result:=Choice; // Forgetting to set your function result will break everything! end; procedure Act(Choice:Longint); begin Session.Write('You chose '+IntToStr(Choice)+', so I am starting '); Case Choice Of 1:Session.WriteLn('the coffee maker...'); 2:Session.WriteLn('World War III...'); 3:Session.WriteLn('the pencil sharpener...'); end; end; (* Here the main program block starts *) begin tell; choice:=choose; act(choice); end.
The above workflow
There are three methods called by the Begin and End. section:- it calls tell, displays to the user the acceptable choices.
- then it calls choose, accepts the user's input entered at the keyboard.
- lastly calls act, takes an action depending upon the user's choice.
- First clears the screen.
- then it displays a title and 3 menu choices.
- One decision is made based upon the value entered.
- Another decision is to display an error message if required.
- The last decision is to repeat the loop or exit based upon the value entered.
Basically, the act must choose between three alternatives, using a number to control the choice. Instead of having three if-then comparisons, we elected to use the case command to make the decision of what should be displayed.
This program structure has several purposes:
- The program shows its structure clearly in the program listing.
- Different actions are placed in different methods.
- This helps the programmer under the program.
- Because the methods are separate, each one can be modified or replaced very easily.
- The order of the actions can be easily changed.
- You may easily substitute methods with alternatives or optimized replacements.
- Because the methods are independent of each other and the program, each method may be exported to other programs and reused there. (Explained in more detail soon).
Variable declarations
A variable is an identifier that marks a value that can change. A variable declaration is a list of identifiers that designate new variables and their types. The type given for the variable(s) can be a type identifier previously declared in a type declaration part in the same block, in an enclosing block, it can also be a new type definition.
When an identifier is specified within the identifier list of a variable declaration, that identifier is a variable identifier for the block in which the declaration occurs. The variable can then be referred to throughout the block unless the identifier is redeclared in an enclosed block. Redeclaration creates a new variable using the same identifier, without affecting the value of the original variable.
Typed Constants
Typed constants can be compared to initialized variables -- variables whose values are defined on entry to their block. Unlike an untyped constant, the declaration of a typed constant specifies both the type and the value of the constant.
Conditional Compilation
To make your job easier, Modern Pascal conditional compilation. This means that you can now decide what portions of your program to compile based on options or defined symbols. The conditional directives are similar in format to the compiler directives you're accustomed to; in other words, they take the format{$directive arg}
where the directive is the directive (such as DEFINE, IFDEF, and so on), and arg is the argument if any. Note that there must be a separator (blank, tab) between directive and arg.
Directive | Description |
{$DEFINE symbol} | Defines symbol for other directives |
{$UNDEF symbol} | Removes definition of symbol |
{$IFDEF symbol} | Compiles following code if symbol is defined |
{$IFNDEF symbol} | Compiles following code if symbol is not defined |
{$IFOPT opt } | Compiles following code if directive opt is enabled |
{$IFOPT opt-} | Compiles following code if directive opt is disabled |
{$ELSE} | Compiles following code if previous IFDEF/IFNDEF/IFOPT is not True |
{$ENDIF} | Marks end of IFDEF/IFNDEF/IFOPT or ELSE section |
The DEFINE and UNDEF directives
The IFDEF and IFNDEF directives test to see if a given symbol is defined. These symbols are defined using the DEFINE directive and undefined UNDEF directives.To define a symbol, insert this directive into your program: {$DEFINE symbol} where symbol follows the usual rules for identifiers as far as length, characters allowed, and other specifications. For example, you might write:
{$DEFINE debug}This defines the symbol debug for the remainder of the module being compiled, or until this statement is encountered:
{$UNDEF debug}As you might guess, UNDEF "undefines" a symbol. If the symbol isn't defined, UNDEF has no effect.
Predefined Symbols
Directive | Description |
MODERNPASCAL | Used to detect it is Modern Pascal running your code |
MPC | Abbreviation for Modern Pascal is running your code |
CODERUNNER | Used to detect your code is running in Modern Pascal's CODERUNNER |
CELERITY | Used to detect your code is running in Modern Pascal's CELERITY |
LINUX | Used to detect your code is running on Linux Operating System |
MACOSX | Used to detect your code is running on MAC OS X Operating System |
FREEBSD | Used to detect your code is running on FREEBSD Operating System |
BSD | Abbreviation for FREEBSD Operating System |
SUNOS | Used to detect your code is running on SUN Operating System |
WINDOWS | Used to detect your code is running on Microsoft Windows Operating System |
UNIX | Used to detect your code is running on a NON-WINDOWS Operating System |
WIN32 | Used to detect your code is running on Windows 32bit Operating System |
WIN64 | Used to detect your code is running on Windows 64bit Operating System |
CPU32 | Used to detect your code is running on a 32bit CPU |
CPU64 | Used to detect your code is running on a 64bit CPU |
CPUARM | Used to detect your code is running on an ARM CPU |
I386 | Used to detect your code is running on an INTEL 32bit CPU |
X86_64 | Used to detect your code is running on an INTEL 64bit CPU |
POWERPC | Used to detect your code is running on a POWERPC CPU |
POWERPC64 | Used to detect your code is running on a 64bit POWERPC CPU |
M68K | Used to detect your code is running on a Motorola 68,000 CPU |
SPARC | Used to detect your code is running on a SPARC CPU |
SPARC64 | Used to detect your code is running on a 64bit SPARC CPU |
BIGENDIAN | Used to detect your CPU uses BIG ENDIAN memory format |
LITTLEENDIAN | Used to detect your CPU uses LITTLE ENDIAN memory format |
The IFxxx, ELSE, and ENDIF symbols
The idea behind conditional directives is that you want to select different source code to be compiled if a particular symbol is (or is not) defined or if a particular option is (or is not) enabled. The general format follows:{$IFxxx} source code {$ENDIF}where IFxxx is IFDEF, IFNDEF, or IFOPT, followed by the appropriate argument, and source code is any amount of Pascal source code, {$I ...} include file, etc. If the expression in the IFxxx directive is True, then the source code is compiled; otherwise, it is ignored as if it had been commented out of your program.
Often you have alternate chunks of source code. If the expression is True, you want one chunk compiled, and if it's False, you want the other one compiled. Modern Pascal lets you do this with the $ELSE directive:
{$IFxxx} source code A {$ELSE} source code B {$ENDIF}If the expression in IFxxx is True, source code A is compiled; otherwise, source code B is compiled. Note that all IFxxx directives must be completed within the same source file, which means they can't start in one source file and end in another. As IFxxx directive can encompass an {$I ...} include file option.
{$IFxxx} {$I file1.pas} {$ELSE} {$I file2.pas} {$ENDIF}That way, you can select alternate include files based on some condition.
You can also nest IFxxx..ENDIF constructs so that you can have something like this:
{$IFDEF WINDOWS} {$IFDEF WIN32} {$I Windows_32bit_Code.inc} {$ELSE} {$I Windows_64bit_Code.inc} {$ENDIF} {$ENDIF}
The IFOPT directive
You might want to include or exclude code, depending upon which compiler options (range checking, I/O checking, and so on) have been selected. Modern Pascal lets you do that with the IFOPT directive, which takes two forms:{$IFOPT R } source code {$ENDIF}and
{$IFOPT R-} source code {$ENDIF}where R is one of the compiler options. With the first form, the following code is compiled if the compiler option is currently enabled; with the second, the code is compiled if the option is currently disabled.