Programming in Turbo Pascal 45 the Seven Basic Elements of Programming
Total Page:16
File Type:pdf, Size:1020Kb
BORLAND Turbo Pascal® User's Guide Version 5.0 Borland International 1800 Green Hills Road P.O. Box 660001 Scotts Valley, CA 95066-0001 This manual was produced with Sprin~ The Professional Word Processor All Borland products are trademarks or registered trademarks of Borland International, Inc. Other brand and product names are trademarks or registered trademarks of their respective holders. CopyrightG 1983, 1989 Borland International Printed in the U.s.A. 1098 14 Table of Contents Introduction 1 Understanding 5.0 . .. 2 Integrated Environment and Command-Line Compilers. .. 2 Stand-Alone Debugging Support . .. .. .. .. .. .. .. .. .. .. 3 Separate Compilation ........................................... 3 Programs and Units ............................................ 3 Compile, Make, and Build ... .. 3 Pick File list . .. 4 File Extensions .................... ~ . .. 4 About This Manual . .. 5 Using Turbo Pascal . .. 5 Appendixes ................................................... 6 Hardware and Software Requirements .............................. 7 Typography ..................................................... 7 How to Contact Borland .......................................... 8 Part 1 Using Turbo Pascal Chapter 1 Getting Started 11 Before You Begin . .. 11 What's On Your Disks ........................................... 12 Installing Turbo Pascal On Your System ............................ 15 Hard Disk Installation ......................................... 15 Installation on a Floppy-Drive System. .. 18 Special Notes ................................................. 20 Choosing Between Two Compilers ................................ 20 Using This Manual .............................................. 21 Chapter 2 Beginning Turbo Pascal 23 Using the Integrated Environment. .. 23 Online Help .................................................. 23 UsingTHELP ............................................... 24 Windows and Menus .......................................... 25 Using Hot Keys ............................................... 27 Loading Turbo Pascal. .. 31 CreatingYourFirstProgram ...................................... 31 Analyzing Your First Program .................................. 32 Saving Your First Program. .. 32 Compiling Your First Program .................................. 33 Running Your First Program ..... .. 34 Checking the Files You've Created ............................... 35 Stepping Up: Your Second'Program . .. 35 Debugging Your Program ...................................... 36 Using the Watch Window .................................... 37 Fixing Your Second Program . .. 38 Programming Pizazz: Your Third Program. .. 39 The Turbo Pascal Compiler .. .. 41 So, What's a Compiler Anyway? .............................. 41 What Gets Compiled? . .. 42 Where's the Code? .......................................... 42 Compile, Make, and Build .. .. 43 Chapter 3 Programming in Turbo Pascal 45 The Seven Basic Elements of Programming ......................... 46 Data ......................................................... 47 Data Types ................................................. 47 Integer Data Types ........................................ 47 Real Data Types . .. 48 Character and String Data Types ............................ 49 Boolean Data Type ........................................ 51 Pointer Data Type ......................................... 52 Identifiers .................................................. 53 Opera tors .................................................... 53 Assignment Operators ....................................... 54 Unary and Binary Arithmetic Operators . .. 54 Bitwise Operators ........................................... 55 Relational Operators ......................................... 55 LogicalOperators ........................................... 56 Address Operators .......................................... 57 Set Operators ............................................... 57 String Operators ............................................ 57 Output ...................................................... 57 The Writeln Procedure ....................................... 57 Input ........................................................ 59 Conditional Statements ........................................ 59 The If Statement. .. 59 The Case Statement. .. 60 Loops ........................................................ 61 The While Loop . .. 61 The Repeat.. Until Loop ...................................... 62 The For Loop ........'....................................... 63 Procedures and Functions ...................................... 64 Program Structure. .. 65 Procedure and Function Structure .............................. 65 11 Sample Program ............................................ 66 Program Comments ......................................... 67 Chapter 4 Units and Related Mysteries 69 What's a Unit, Anyway? ......................................... 69 A Unit's Struchlre .................. .. 70 Interface Section ............................................ 71 Implementation Section ...................................... 71 Initializa tion Section . .. 72 How Are Units Used? ........................................... 73 Referencing Unit Declarations .................................. 74 Implementation Section Uses Clause ............................. 76 Circular Unit References ..................................... 77 Sharing Other Declarations ................................... 78 TURBO.TPL . .. 79 Writing Your Own Units ......................................... 81 Compiling a Unit. .. 82 An Example .................................................. 82 Units and Large Programs . .. 83 Units as Overlays ............................................. 84 1PUMOVER . .. 84 Chapter 5 Project Management 87 Program Organization ............................................ 87 Initialization .................................................. 88 The Build and Make Options ..................................... 89 The Make Option ............................................. 90 The Build Option. .. 90 The Stand-Alone Make Utility .................................... 91 A Quick Example ............................................. 91 Creating a Makefile .......................................... 92 Using MAKE ............................................... 93 Conditional Compilation . .. .. .. .. .. .. .. .. 93 The DEFINE and UNDEF Directives ............................. 94 Defining at the Command Line. .. .. .. .. .. .. 95 Defining in the Integrated Environment ........................ 95 Predefined Symbols ......................................... 95 The VERSO Symbol ........................................ 95 The MSDOS and CPU86 Symbols. .. 96 The CPU87 Symbol . .. 96 The IFxxx, ELSE, and ENDIF Symbols. .. 96 The IFDEF and IFNDEF Directives. .. 97 The IFOPT Directive ......................................... 98 Optimizing Code. .. 99 iii Chapter 6 Debugging Your Turbo Pascal Programs 101 Compile-Time Errors ........................................... 102 Run-Time Errors ............................................... 102 Logic Errors ................................................... 103 Turbo Pascal's Integrated Debugger .............................. 103 A Quick Debugging Example .................................. 107 Why Use the Debugger? . .. 109 Tracing ................................................... 110 Go to Cursor. .. 110 Breaking .................................................. 110 Watching .................................................. 110 Evaluating ................................................ 111 Modifying . .. 111 Navigating ................................................ 111 Preparing to Use the Debugger ................................. 111 Your Display .......................................... ',' ...... 112 Starting a Debugging Session ................................ 114 Restarting a Debugging Session .............................. 114 Ending a Debugging Session . .. 115 Stepping Through Your Program ............................... 115 Using Breakpoints ............................................ 119 Using Ctrl-Break ........................................... 120 Watching Values ............................................. 121 Scope and Qualification ..................................... 123 Types of Watch Expressions .................................. 126 Format Specifiers . .. 127 Typecasting ............................................. 130 Expressions ............................................. 131 Editing the Watch Window .................................. 133 Evaluating and Modifying.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 134 Modification Issues ......................................... 136 Navigation .................................................. 137 The Call Stack ............................................. 137 Finding Procedures and Functions ............................ 139 General Issues ................................................. 140 How to Write Programs for Debugging ......................... 141 IvIemory Issues .............................................. Recursive Routines .......................................... 143 Where Debugging Won't Go .................................. 144 Common Pitfalls ............................................ 145 Error-Handling ............................................... 145 Input/Output Error-Checking ................................ 146 Range-Checking ...........................................