Intro to Programming
Jezreel Crisostomo, LPT
South Philippine Adventist College
Computer Programming?
What is programming?
The process of writing, testing, and maintaining
the source code of computer programs.
Telling the computer what to do
Why learn to program?
Develops logic and problem-solving skills
Improves attention to detail
It’s fun!
But I’m Not a Geek!
Until
recently, programming required
knowledge of computer language syntax
Why Learn Programming?
Computers have become commonplace in our modern life
Understanding the strengths and limitations of computers requires some understanding of programming
Programming can be loads of fun!
It is an intellectually engaging activity that allows you to express yourself through remarkably beautiful creations
Programming develops valuable problem-solving skills, especially ones that pertain to analysis, design and
implementation
Programmers are in great demand!
Ubiquitous
Astronomy
Computing
Smaller,
Faster,
Cheaper
Sensors
Gene
Sequencing
and
Biotechnology
Hardware Basics
To be a successful programmer, you need to
know some details of how computers work
For instance, understanding the basics of
hardware will help you analyze the performance
(or efficiency) of any of your programs
Will the data of your program fit in memory?
If not, how would that impact the performance of your program?
Is your program CPU-bound or IO-Bound?
If CPU-bound, how powerful is your CPU?
If IO-bound, how big is your disk or network bandwidth?
Functional View of a Computer
Output
CPU Devices
Input
Devices
Main Seconda
Memory ry
Memory
Functional View of a Computer
• The secondary memory is where your saved program and data reside
• It is a non-volatile storage
• I.e., when the power is turned off,
your program and data will NOT be lost E.g., Hard Disk
Seconda
ry
Memory
Functional View of a Computer
• The main memory is much faster (but more expensive) than the
secondary one, however, it is volatile
• Your program and data are copied from secondary memory to main
memory for efficiency reasons
E.g., Random Access Memory (RAM)
Main Seconda
Memory ry
Memory
Functional View of a Computer
• The Central Processing Unit (CPU) is the “brain” of the computer
• It can at least perform:
• Arithmetic operations CPU
(e.g., adding 2 numbers)
• Logical operations
(e.g., test if 2 numbers
are equal) Main Seconda
Memory ry
Memory
• It can directly access
information stored in main memory but not in secondary memory
Functional View of a Computer
E.g., Monitor
E.g., Keyboard Output
and mouse CPU Devices
Input
Devices
Main Seconda
Memory ry
Memory
Functional View of a Computer
Humans interact with
computers via Input and
Output (IO) devices E.g., Monitor
E.g., Keyboard Output
and mouse Information from Input devices Devices
are processed by the CPU
Input
and may be shuffled off to the
Devices
main or secondary memory
When information need to be
displayed, the CPU sends
them to one or more Output
devices
Programming Languages
A program is just a sequence of instructions
telling the computer what to do
Obviously, we need to provide these instructions
in a language that computers can understand
We refer to this kind of a language as a programming language
Python, Java, C and C++ are examples of programming languages
Every structure in a programming language has
an exact form (i.e., syntax) and a precise
meaning (i.e., semantic)
Machine Languages
Python, Java, C, and C++ are, indeed, examples of high-
level languages
Strictly speaking, computer hardware can only
understand a very low-level language known as machine
language
If you want a computer to add two numbers, the
Load the
instructions number
that the from
CPUmemory location
will carry out2001 into be
might the CPU
Load the
something this: from memory location 2002 into the CPUA Lot of
likenumber
Add the two numbers in the CPU Work!
Store the result into location 2003
High-Level to Low-Level
Languages
In a high-level language like Python, the addition
of two numbers can be expressed more
naturally:
c=a+b Much Easier!
But, we need a way to translate the high-level
language into a machine language that a
computer can execute
To this end, high-level language can either be
compiled or interpreted
Compiling a High-Level
Language
A compiler is a complex software that takes a
program written in a high-level language and
translates it into an equivalent program in the
machine
Source language of some computer
Machine
Code Compiler
Code
(Program)
Running Output
Inputs
Program s
Interpreting a High-Level
Language
An interpreter is a software that analyzes and executes
the source code instruction-by-instruction (on-the-fly) as
necessary
Source
Code
(Program) Computer
Output
Running An
s
Interpreter
Inputs
E.g., Python is an interpreted language
Compiling vs. Interpreting
Compiling is a static (i.e., pre-execution), one-shot translation
Once a program is compiled, it may be run over and over again
without further need for the compiler or the source code
Interpreting is dynamic (i.e., happens during execution)
The interpreter and the source code are needed every time the
program runs
Compiled programs tend to be faster, while interpreted ones
lend themselves to a more flexible programming environments
(they can be developed and run interactively)
Note on Portability
The translation process highlights another advantage that high-level
languages have over machine languages, namely, portability
A program for an Intel-based machine will not run on an IBM-based
machine since each computer type has its own machine language
On the other hand, a program written in a high-level language (say,
a Python program) can be run on many different kinds of computers
as long as there is a suitable compiler or interpreter
Python programs are said to be portable!
Anyone Can Program!
New tools, like Scratch, allow ANYONE to
create computer programs without learning
complicated syntax.
Programming with Scratch
Open Scratch from either the desktop icon or
Start Menu
Summary
A computer is a universal information-processing machine, which
can carry out any process that can be described in sufficient detail
A description of the sequence of steps for solving a particular
problem is called an algorithm
Algorithms can be turned into software (programs) that determine
what the hardware (physical machine) can and does accomplish
The process of creating software is called programming
Summary
A basic functional view of a computer system comprises
a central processing unit (CPU), a main memory, a
secondary memory, and input and output devices
The CPU is the brain of the computer that performs
simple arithmetic and logical operations
Information that the CPU acts on (data and programs)
are stored in main memory (e.g., RAM), while more
permanent information are stored in secondary memory
(e.g., disk)
Summary
Programs are written using a formal notation known as a
programming language
There are many different languages, but all share the property
of having a precise syntax (form) and semantics (meaning)
Computer hardware only understands a very low-level
language known as machine language
Programs are usually written using human-oriented high-level
languages such as Python
Summary
A high-level language must either be compiled or
interpreted in order for the computer to understand it
High-level languages are more portable than machine
languages
Python is an interpreted language
One way to learn about Python is to use an interactive
shell for experimentation
Homework
Make sure you have the latest version of
Scratch installed on your Tablet PC
Finish working through the Scratch Getting
Started Guide
Feel free to experiment and explore!
Scratch Syntax: Part 1
Statements
In programming, a statement is simply a
directive that tells the computer to do
something. Think of it as a command or an
instruction.
In Scratch, any block whose label reads like
a command is a statement.
Boolean Expressions
Sometimes, you only want a statement to be
executed under certain conditions. Such
conditions are defined in terms of Boolean
expressions.
In programming, a Boolean expression is
an expression that is either true or false. In
Scratch, any block shaped like an elongated
diamond is a Boolean expression.
Boolean Expressions
One such block is:
After all, it is either true that the mouse button
is down or it is false.
Another such block is:
After all, it is either true that some number is
less than another number or it is false.
With Boolean expressions can we construct
conditions.
Conditions
In programming, a condition is something
that must be true in order for something to
happen.
A condition is thus said to "evaluate to true"
or "evaluate to false."
In Scratch, any block whose label says "if,"
"when," or "until" is a sort of conditional
construct.
Conditions
IfConstruct: Instruct a sprite to say hello
only if, say, the user has depressed the
mouse button:
If-Else Construct: Instruct a sprite to say
hello or goodbye, depending on whether the
user has depressed the mouse button:
Conditions
When Construct: Instruct a sprite to do
something when an event occurs:
When Construct: Instruct a sprite to wait to
do something until a condition is true:
Loops
In programming, a loop can induce multiple
executions of statements.
In Scratch, any block whose label begins with
"forever" or "repeat" is a looping construct.
Follow Me
Follow Me applet
Homework
Complete the Cat Walk Project
You may wish to use the Scratch Reference
Guide and the Programming Concepts
Handout as resources
Completed projects need to be submitted via
Interact by the due date.
Scratch Syntax: Part 2
Algebra-Geometry
Variables
In programming, a variable is a placeholder
for some value, much like x and y are popular
variables in algebra.
In Scratch, variables are represented with
blocks shaped like elongated circles,
uniquely labeled by you.
Threads
In programming, a thread is like a mini-
program within a program that can execute at
the same time as other threads.
In Scratch, any block whose label begins with
"when" essentially demarks the start of a
thread.
Events
An event is a signal from one thread to
another.
Blocks whose labels begin with "broadcast"
signal events whereas blocks whose labels
begin with "when" handle events.
Pac Man and Pong
Games like Pac Man A Variable could be
use Threads added to Pong
Homework
Complete the Tweak a Game Project
Remember that the video tutorials can be
very helpful
Completed projects need to be submitted via
Interact by the due date. Don’t forget to
include your brief written summary!
Scratch Syntax:
Your First Game
Adding a Level
Levels
can be added using threads and
events.
Copter Game Flight Code
Copter Game—One Level
Homework
Complete the Your First Game Project
You may wish to use the Copter Game and
Levels program as resources
Completed projects need to be submitted via
Interact by the due date.