CSP1150
Programming Principles
Lecture 1: Introduction to Programming
This Lecture
• Establishing a context
– Approach to the unit
– What is programming?
– Program implementation and source code
• Program design
– Pseudocode and flowcharts
• A brief history of programming languages
• Getting started
– Introduction to Python
– A simple program
– Variables, comments and concatenation
Textbook
• Starting Out with Python, 3rd Edition
– Tony Gaddis
• Reading the chapter(s) is required
– Read the indicated chapter(s) before class
• This week covers the following textbook chapter(s):
– Chapter 1 – Introduction to Computers and Programming
• Entire chapter
– Chapter 2 – Input, Processing, and Output
• Entire chapter
– Appendix A and B
Establishing a Context
Approach to the Unit
• This unit aims to teach you introductory programming
– It does not aim to teach you “programming in Python”
• The language you happen to use when learning to program
is not the focus or goal, it is simply the tool you are using
– When you learn to drive, you learn to drive a car – not a dark
red 1997 Ford Festiva Trio
• We will focus on the core concepts of programming
– What they entail
– Why they are necessary, important or desirable
– How (and why) their implementations differ between languages
– Most of the examples, and your lab work, will just happen to
use the Python programming language
Approach to the Unit
• While Python is the programming language we focus on,
this unit will discuss and demonstrate other languages
– This is to keep you focused upon the concepts being covered,
rather than their specific implementation in Python
• Code samples written in Python will look like this:
# print "Hello world!" to the screen Python
print('Hello world!')
• Code samples written in other languages will look like this:
// print "Hello world!" to the screen Java
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
What is Programming?
• Let us first consider “What is a computer?”
• A computer is a device with a few basic components:
– A processor (CPU) which can execute basic instructions
– Memory to store data and instructions that are in use
– Storage for ongoing retention of data and programs
– Input and output devices to communicate/interact with users
• Computers exist to be useful; To do things for us
– e.g. Automation of a manual process, or making something
possible that would otherwise be impossible or unfeasible
– Computers need to be told exactly how to do things
– This must be written in a way that a computer can understand
i.e. they must follow a pre-defined set of rules
Operating Systems
• Operating systems such as Windows, Mac OS or Linux sit
between the hardware and the programs on a computer
• Operating Systems are responsible for:
– Interfacing with the hardware of a computer and making it
available for programs to interact with
– Managing the storage and organisation of data and programs
– Allocating and scheduling the usage of the computer’s
processor, memory, etc, so that everything can run smoothly
– Managing users and security
• This provides a layer of abstraction, so that programs can
interact with hardware and resources in a consistent way
– Hence programming languages / programmers do not need to
implement things “from the ground up” every time
What is Programming?
• So, what is programming? Programming is problem solving
– Programs are written in order to fulfil a need / solve a problem
– A programming language is the tool used to create a solution
By writing a program using a programming language,
you can make a computer do something useful
Why Learn Programming?
• Learning to program is about developing cognitive
processes - i.e. ways of thinking - not memorising syntax
– Programming teaches you to think about a problem and
design a solution that adheres to an established set of rules
– Programming teaches you to think in a precise and
methodical manner when approaching a problem
– Programming teaches you to think of solutions that are
appropriate, elegant, modular and well-structured
Programming teaches you to think
Program Implementation
• A computer’s processor can only understand a set of very
basic instructions, such as simple arithmetic operations
– This is known as machine code, and it is not feasible to write
long or complex code directly in machine code
B801000000 Machine Code
BF02000000
01F8
• To make this easier, assembly language was developed
– Assembly language replaces the numeric instruction codes of
machine code with short descriptive words, e.g. “add”
– Each instruction corresponds to a machine code instruction,
so it is still very low level and unfeasible for complex code
mov eax, 0x1 Assembly
mov edi, 0x2
add eax, edi
Program Implementation
• In order to write complex programs in a way that is easy to
read and write as well as being processor-independent, we
use high-level languages
– These are the programming languages you hear about / use:
Python, C, Java, PHP, C++, JavaScript, etc
– Instructions in high-level languages are “statements”, and
usually translate to multiple machine code instructions
– These languages are much easier for people to write code in,
as well as reading and editing that code at a later stage
• The code of a program written in a high-level language is
known as the “source code” of the program
– The computer’s processor still only understands machine
code; the source code must be translated into machine code
Program Implementation
• There are two main approaches for translating high-level
source code into machine code:
– Compilation: The entirety of the source code is translated
into machine code and an executable file of it is produced
• This is a relatively slow process, however the resulting file runs
quickly as it is not being translated on the spot, and the compiler
can spend time to further optimise the resulting file
• Some compiled languages include C and C++
– Interpretation: An interpreter translates and executes each
source code statement one at a time as the program is run
• Slower to run as it is “doing it live”, but fewer steps involved
• Some interpreted languages include Python, PHP and JavaScript
• Some languages, e.g. Java & .NET languages, use a hybrid
method – compiling to “bytecode” and interpreting that
Source Code
• A program’s source code consists of a list of statements
written in a programming language
• Every statement must adhere to the syntax of the language
– The “words” that the language recognises
– The “phrasing” required to specify values
– The ways of arranging these into valid “sentences”
• Any errors in your syntax will prevent the code from running
prunt('Hello world!') Python
Traceback (most recent call last):
File "C:\Python\Scripts\test.py", line 1, in <module> Error location
prunt('Hello world!') Erroneous statement
NameError: name 'prunt' is not defined Type of error
– Do not fear error messages; read them and learn from them
Source Code
• When program is run, it runs the statements in the source
code from start to end (unless it encounters an error)
– Some statements choose between different sections of code
to run based upon the result of a conditional check (Selection)
– Some statements repeatedly run a section of code until a
certain condition is met (Iteration)
– Some statements request input from users or data from other
sources, often processing it using selection and iteration
– Some statements define sections of code to be used at other
points in the program where it is needed (Functions)
• Many programs are “event-driven”, meaning that they wait
for events to occur (e.g. the user clicking on something)
– When an event occurs, it triggers some statements to be run
Walking Exercise
• Goal: To navigate from one end of the room to the other
– As an English-speaking human, this can be achieved simply
by telling the person “Go over there”… Easy!
• We will be instructing a robot (played by a volunteer), who
can only understand the following statements:
– FORWARD( num )
• Walk forward num steps (assume 1 metre steps)
– TURN( LEFT | RIGHT )
• Turn 90 degrees left or right
• The robot must be given all of their statements in advance
– Bonus points for walking like a robot or making robot noises
Walking Exercise
• RoboTest.robo
FORWARD(1) Robo
TURN(LEFT)
FORWARD(1)
TURN(LEFT)
FORWARD(1)
TURN(LEFT)
FORWARD(1)
TURN(LEFT)
• EndOfRoom.robo
Robo
Program Design
Program Design
• Writing syntactically correct code is difficult and can distract
you from the problem that you are trying to solve
– Hence it is useful to be able to plan out the logic and structure
of your solution prior to commencing the actual programming
– This is the concept of program design
• Pseudocode and flowcharts are two ways of representing
the steps and statements that a program will implement
• Pseudocode and flowcharts…
– are valuable design tools that experienced programmers use
– are designed to be read by people, not parsed by a computer
– follow conventions of programming, e.g. selection & iteration
– often omit minor, insignificant and assumed statements
Program Design
• Pseudocode does not have defined standards, rules or
syntax that need to be adhered to
– There is no single “correct” way to write pseudocode
– To emphasise this, different conventions have been used in
each of the following pseudocode examples
• Flowcharts are a more visual approach to program design,
using symbols with specific meanings associated with them
– They are “stricter” than pseudocode, but this is a necessity in
order to produce flowcharts that anyone can read
• Always remember that the goal of these design tools is to
illustrate and communicate your design in a clear way
Flowchart Symbols
• This table summarises the main symbols used in flowcharts
– Note that flowcharts are used to illustrate algorithms to solve
problems in many fields, not just programming – very useful!
Symbol Meaning
Flow Line (indicates direction and connection between things)
Processing (indicates a basic operation, step or action)
Terminator (indicates start or end of a flowchart)
Input/Output (indicates input or output)
Decision (indicates a choice with true/false outcomes)
Predefined Process/Function (indicates a group of statements
that perform a task – may be detailed in separate flowchart)
Pseudocode and Flowchart Examples
• Let’s look at some examples of pseudocode, flowcharts,
and corresponding source code
Get value
Prompt the user for a value Pseudocode
Multiply the value by 2 Result = value * 2
Show the value on the screen
Display result
value = input('Enter a value: ') Python
result = int(value) * 2
print(result)
Pseudocode and Flowchart Examples
IF length of name > 15 THEN Pseudocode
PRINT name length error message
ELSE
PRINT name saved message
Name
length >
15?
True False
Display name Display name
length error saved message
if (name.length() > 15) Java
{
System.out.println("Error: Name too long.");
}
else
{
System.out.println("Your name has been saved.");
}
Pseudocode and Flowchart Examples
Get user details from Pseudocode
database
get user details from database
while (rows remaining in result set)
{
Row
get next row of data
remaining?
print name from row
}
True False
Get next row of
results
Display name
PHP
from row
$results = $db->query("SELECT * FROM users");
while ($row = $results->fetch_assoc())
{
echo $row['first_name'];
}
Software Engineering & Program Design
• Software Engineering describes the process of designing,
developing, testing and maintaining a piece of software
– Numerous different approaches to this are possible
– There are numerous other tools and techniques that are
involved in the design (and development) of software
• Some are only relevant in certain types/scales of software
• Some design tools and techniques in this process include…
– Program specifications (documenting the scope/functionality)
– User interface design (mock-ups of visual elements)
– Software architecture design (high-level logistics of software)
You should know exactly what you are building, and how,
before you start writing any code
Writing Good Code…
xkcd.com
A Brief History of
Programming Languages
Early High-Level Languages
• Fortran is generally credited as the first high-level language
– Originally released in 1957, with subsequent versions
released since then (it’s still being released and used)
– Introduced many of the core concepts and conventions of
programming that we now take for granted
– Designed for scientific applications
• In the 1960s, computers were beginning to see significant
usage in business, which had different needs than science
– COBOL was a language designed to meet these needs,
aiming for accessibility and English-like syntax
– While not much COBOL is being written these days, many
businesses are still running COBOL code in various systems
Programming Languages, 1960s – 1980s
• Many other significant languages released over the years:
– BASIC (1963) emerged as a teaching language, and lives on
as Visual Basic .NET these days
– PL/I (1964) tried to take the best aspects of all languages and
invented some new ones in an attempt to be able to do it all
– Pascal (1971) also designed for teaching, led to Turbo Pascal
– C (1972) was designed for systems programming (e.g. OSs)
was very expressive and powerful – also packaged in UNIX
– Ada (1974) developed for US DoD to standardise the software
used in embedded systems. Introduced some new concepts
– Smalltalk-80 (1980) was first fully Object Oriented language,
expanding on features that appeared in SIMULA and Ada
– C++ (1983) evolved from C, incorporating OO from Smalltalk
Programming Languages, 1990s and Beyond
• Some of the more modern languages include…
– Python (1991), general purpose language emphasising
readability and expressiveness. Supports OO and other styles
– Java (1995), OO, designed to be platform independent
– PHP (1995), designed for web development (server-side)
– JavaScript (1995), designed for web development (client-side)
– .NET (2002) is a framework allowing for interoperability
between many different languages (C#, VB.NET, ASP.NET…)
• And more (including some popular and/or significant ones)!
– This isn’t a topic worth examining in further detail at this point
Programming Language Design Focus
• In the 1950s and 1960s languages focused upon writability
– How easy and effective it is to write code in the language
– Why? Few people writing code, not much reuse or
maintenance, time-consuming and costly to write and run
• By the 1970s, languages started emphasising readability
– How easy it is to read code in the language
– Why? More people (not just scientists) coding, larger and
more complex code in use, lots of maintenance and updating
• Striking a balance between these is important in a language
– Other factors include reliability (particularly for mission critical
code) and cost (influenced by all other factors – time and
effort needed to learn, train, write, read, maintain, etc.)