Programming: Concepts for New Programmers
Total Page:16
File Type:pdf, Size:1020Kb
Programming: Concepts for new programmers How to Use this User Guide This handbook accompanies the taught sessions for the course. Each section contains a brief overview of a topic for your reference and then one or more exercises. Exercises are arranged as follows: A title and brief overview of the tasks to be carried out; A numbered set of tasks, together with a brief description of each; Some exercises, particularly those within the same section, assume that you have completed earlier exercises. If you have any problems with the text or the exercises, please ask the teacher or one of the demonstrators for help. Many of the exercises used here are designed to be worked through in pairs or small groups of students, to encourage discussion about the topics covered. Software Used None Files Used None Revision Information Version Date Author Changes made 1.0 Sept 2006 Dave Baker Created 1.1 May 2007 Dave Baker Minor typo corrections Rename of Chapter 6, with change of focus 1.1a Nov 2007 Dave Baker Correction of typos in Ex 6 1.2 Dec 2007 Dave Baker Revision of some exercises 1.3 April 2011 Dave Baker Revision/removal of some exercises 1.3a Oct 2011 Dave Baker Minor corrections 1.4 Oct 2012 Dave Baker Minor updates 1.5 Sep 2013 Dave Baker Revision of some exercises 1.6 Feb 2014 Dave Baker Added section on code re- use 1.7 Jun 2014 Dave Baker Added section on lists 1.8 Oct 2016 Duncan Young Updates to University names and language descriptions in Section 7 Acknowledgements Many of the ideas in this module have been talked through with colleagues and friends, in particular Jonathan Gregory formally of Abingdon College of Further Education. Thanks to Peet Morris of the Computing Laboratory (ComLab) at Oxford University for his useful suggestions and for keeping me on the straight and narrow in terms of the underlying theory of computer programming and computer languages. Copyright This document is made available under a Creative Commons Attribution- NonCommercial-ShareAlike CC BY-NC-SA licence by Dave Baker who asserts his right to be identified as the author. Note that some images used in the document and presentations are copyright of their owners and may be subject to different copyright conditions. Where possible IT Learning Centre iv this has been noted in the text. If an error in attribution/copyright has been made, contact the author who will be pleased to make the necessary corrections. v IT Learning Centre Contents 1 Introduction ............................................................................. 1 1.1. What you should already know ........................................................... 1 1.2. What you will learn ............................................................................. 1 2 What is a Program? .................................................................. 2 2.1. Where do we find programs? ............................................................. 3 2.2. Where do programs come from? ....................................................... 3 2.3. So, what’s so difficult about programming? ...................................... 6 2.3.1. Types of program errors ....................................................................... 6 2.3.2. The program development process...................................................... 7 3 What is a computer language? ................................................. 9 3.1. Computer language structure ........................................................... 11 3.2. Algorithms ........................................................................................ 11 3.3. Pseudocode ....................................................................................... 12 3.4. Pseudocode and comments .............................................................. 13 4 Controlling how a program behaves ....................................... 17 4.1. Sequence ............................................................................................ 17 4.2. Selection ............................................................................................ 17 4.2.1. Nested selection ................................................................................. 18 4.2.2. Deeply nested selection ..................................................................... 18 4.3. Repetition..........................................................................................19 4.3.1. Pre-determined repetition...................................................................19 4.3.2. Conditional repetition .........................................................................19 5 Data ........................................................................................ 23 5.1. What is a variable? ........................................................................... 23 5.2. What is a constant? .......................................................................... 24 5.3. The types of data .............................................................................. 25 5.4. Operators ......................................................................................... 27 5.5. Data structures ................................................................................. 28 5.5.1. Arrays.................................................................................................. 28 5.5.2. Lists .................................................................................................... 30 6 The ‘shape’ of programs ......................................................... 32 6.1. Functions .......................................................................................... 32 6.1.1. Argument (or Parameter) passing ...................................................... 34 6.1.2. Re-use of code .................................................................................... 34 IT Learning Centre vi 6.2. Objects ............................................................................................. 36 6.2.1. Encapsulation: Data and Behaviour .................................................. 37 7 What next? ............................................................................. 38 7.1. A hands on introduction using Alice ................................................ 38 7.2. Playing with SCRATCH ....................................................................... 38 7.3. An example of a Java program ........................................................ 38 7.4. Choosing a computer language to learn ........................................... 41 7.5. C ......................................................................................................... 41 7.6. C++ ................................................................................................... 42 7.7. Java ................................................................................................... 42 7.8. JavaScript......................................................................................... 43 7.9. MatLab ............................................................................................. 43 7.10. Perl ................................................................................................. 44 7.11. PHP ................................................................................................. 44 7.12. Python ............................................................................................. 45 7.13. C# .................................................................................................... 45 7.14. Objective-C ..................................................................................... 46 7.15. Swift ................................................................................................ 46 7.16. Visual Basic ..................................................................................... 46 7.17. Ruby ................................................................................................ 47 7.18. R ...................................................................................................... 47 7.19. Wolfram .......................................................................................... 47 7.20. Haskell ........................................................................................... 47 8 Glossary ................................................................................. 48 vii IT Learning Centre Exercises Exercise 1 What is a program? ............................................................... 2 Exercise 2 Simulating a program ........................................................... 4 Exercise 3 Writing a program ................................................................ 5 Exercise 4 An algorithm – Bubble sort ................................................. 14 Exercise 5 Creating your own algorithm – The Median ....................... 15 Exercise 6 Efficient use of control structures ....................................... 21 Exercise 7 Identifying data types ......................................................... 27 Exercise 8 Using a data structure (arrays) .......................................... 29 Exercise 9 Using functions ................................................................... 35 IT Learning Centre viii Programming: Concepts for new programmers PG002 1 IT Learning Centre Programming: Concepts for new programmers PG002 1 Introduction Welcome to this introductory programming module. This booklet accompanies the module delivered by the University