Free Pascal and Lazarus Programming Textbook
Total Page:16
File Type:pdf, Size:1020Kb
This page deliberately left blank. In the series: ALT Linux library Free Pascal and Lazarus Programming Textbook E. R. Alekseev O. V. Chesnokova T. V. Kucher Moscow ALT Linux; DMK-Press Publishers 2010 i UDC 004.432 BBK 22.1 A47 Alekseev E.R., Chesnokova O.V., Kucher T.V. A47 Free Pascal and Lazarus: A Programming Textbook / E. R. Alekseev, O. V. Chesnokova, T. V. Kucher M.: ALTLinux; Publishing house DMK-Press, 2010. 440 p.: illustrated.(ALT Linux library). ISBN 978-5-94074-611-9 Free Pascal is a free implementation of the Pascal programming language that is compatible with Borland Pascal and Object Pascal / Delphi, but with additional features. The Free Pascal compiler is a free cross-platform product implemented on Linux and Windows, and other operating systems. This book is a textbook on algorithms and programming, using Free Pascal. The reader will also be introduced to the principles of creating graphical user interface applications with Lazarus. Each topic is accompanied by 25 exercise problems, which will make this textbook useful not only for those studying programming independently, but also for teachers in the education system. The book’s website is: http://books.altlinux.ru/freepascal/ This textbook is intended for teachers and students of junior colleges and universities, and the wider audience of readers who may be interested in programming. UDC 004.432 BBK 22.1 This book is available from: The <<Alt Linux>> company: (495) 662-3883. E-mail: [email protected] Internet store: http://shop.altlinux.ru From the publishers <<Alians-kniga>>: Wholesale purchases: (495) 258-91-94, 258-91-95. E-mail: [email protected] Internet store: http://www.alians-kniga.ru/ The material in this book is distributed under the GNU FDL License. The book contains the following text on the first page after the cover: <<In the series: ALT Linux Library>>. The title is: <<Free Pascal and Lazarus: A Textbook on Programming>>. The book does not contain invariant sections. The authors of the sections are shown in the titles of the respective sections. ALT Linux is a trademark of ALT Linux. Linux is a trademark of Linus Torvalds. Other names in this textbook may be trademarks of their respective owners. ISBN 978-5-94074-611-9 © Alekseev E.R., Chesnokova O.V., Kucher T.V., 2010 © ALT Linux, 2010 ii Table of Contents From the Editors.....................................................................................................................1 About free software for education.................................................................................1 The “ALT Linux Library” Series.........................................................................................2 Introduction............................................................................................................................3 Chapter 1. Development Tools............................................................................................5 1.1 The Program Development Process........................................................................5 1.2 The Free Pascal Programming Environment..........................................................6 1.2.1 The Free Pascal Text Editor.....................................................................................9 1.2.2 Running a Program and Viewing Results...........................................................11 1.3 The Geany Development Environment.................................................................11 1.4 The Lazarus RAD Environment...............................................................................14 1.4.1 Installing Lazarus on Linux...................................................................................14 1.4.2 Installing Lazarus on Windows............................................................................15 1.4.3 The Lazarus Environment.....................................................................................17 1.4.4 The Lazarus Main Menu........................................................................................18 1.4.5 The Application Form............................................................................................22 1.4.6 The Lazarus Source Editor....................................................................................22 1.4.7 The Component Palette........................................................................................28 1.4.8 The Object Inspector.............................................................................................29 1.4.9 Your First Lazarus Program..................................................................................30 1.4.10 A Useful Program.................................................................................................38 1.4.11 A Lazarus IDE Console Application....................................................................43 1.4.12 Data Input / Output Statements........................................................................46 Chapter 2. Introduction to Free Pascal.............................................................................49 2.1 Lazarus Project Structure........................................................................................49 2.2 The Console Application Structure........................................................................50 2.3 Language Elements..................................................................................................52 2.4 Free Pascal Data Types............................................................................................52 2.4.1 The Character Type................................................................................................53 2.4.2 The Integer Types..................................................................................................53 2.4.3 The Real Type..........................................................................................................54 iii 2.4.4 The DateTime Type................................................................................................55 2.4.5 The Boolean Type...................................................................................................55 2.4.6 Creating New Types...............................................................................................55 2.4.7 The Enumeration Type..........................................................................................56 2.4.8 The Subrange Type................................................................................................56 2.4.9 Structured Types....................................................................................................57 2.4.10 Pointers.................................................................................................................59 2.5 Operators and Expressions.....................................................................................60 2.5.1 Arithmetic Operators.............................................................................................62 2.5.2 Relational Operators..............................................................................................64 2.5.3 Boolean Operators.................................................................................................64 2.5.4 Pointer Operators..................................................................................................64 2.6 Standard Mathematical Functions.........................................................................65 2.7 Exercises.....................................................................................................................75 Chapter 3. Flow Control......................................................................................................79 3.1 Principal Algorithm Constructs...............................................................................79 3.2 The Assignment Statement.....................................................................................81 3.3 The Compound Statement......................................................................................81 3.4 Selection Structures.................................................................................................81 3.4.1 The If..Then..Else Selection Structure..................................................................81 3.4.2 The Case Selection Structure................................................................................98 3.4.3 Error Handling and Displaying Messages........................................................101 3.5 Repetition Statements...........................................................................................105 3.5.1 The while..do Statement.....................................................................................105 3.5.2 The repeat..until Statement................................................................................107 3.5.3 The for..do Statement.........................................................................................108 3.5.4 Control Transfer Statements..............................................................................110 3.5.5 Solving Problems Using Loops...........................................................................111 3.5.6 Entering Data Using an InputBox......................................................................126 3.6 Exercises..................................................................................................................135