ICS 2102-Lecture 2 Programming Concepts
ICS 2102-Lecture 2 Programming Concepts
to programming
Lecture –Introduction to Computers and Programming
Today…
• Introduction (~70 minutes):
• Motivation
• Hardware basics
• High-level vs. low-level programming languages
• Compiling vs. interpreting high-level languages
• Writing very simple Python commands using the interactive mode
Computers and Programming
• A computer is just a machine (the hardware) for executing programs
(the software)
• Hence, the software rules the hardware!
• System SW
• Programs written for computer systems
• Compilers, operating systems, …
• Application SW
• Programs written for computer users
• Word-processors, spreadsheets, & other
application packages
Why Learn Programming?
• Computers have become commonplace in our modern life
• Understanding the strengths and limitations of computers requires some
understanding of programming
Output
CPU Devices
Input
Devices
Main Secondary
Memory 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
Secondary
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 Secondary
Memory Memory
Functional View of a Computer
• The Central Processing Unit (CPU) is the “brain” of the computer
E.g., Monitor
E.g., Keyboard Output
and mouse CPU Devices
Input
Devices
Main Secondary
Memory Memory
Functional View of a Computer
Humans interact with computers
E.g., Monitor
via Input and Output (IO) devices
E.g., Keyboard Output
and mouse Information from Input devices Devices
are processed by the CPU and
Input
may be shuffled off to the main or
Devices
secondary memory
• If you want a computer to add two numbers, the instructions that the
CPU will carry out might be something like this:
Load the number from memory location 2001 into the CPU
Load the number from memory location 2002 into the CPU A Lot of
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!
Running
Inputs Outputs
Program
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
Running An Outputs
Interpreter
Inputs
• Executable program
• Output from linker/loader
• Machine language program linked with necessary libraries &
other files
• Files usually have extension ‘.exe’
What is a Linker?
• A program that pulls other programs together so that they can run.
• Most programs are very large and consist of several modules.
• Even small programs use existing code provided by the programming
environment called libraries.
• The linker pulls everything together, makes sure that references to
other parts of the program (code) are resolved.
Running Programs
• Steps that the computer goes through to run a
program:
Memory
Machine language
program
(executable file)
Input Data Data entered CPU
during execution
Computed results
Program Output
Program Execution
• Steps taken by the CPU to run a program (instructions are in
machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
Program Errors
• Syntax Errors:
• Errors in grammar of the language
• Runtime error:
• When there are no syntax errors, but the program can’t
complete execution
• Divide by zero
• Invalid input data
• Logical errors:
• The program completes execution, but delivers incorrect
results
• Incorrect usage of parentheses
Compilation
Source Target
Program Compiler Program
Interpreter Output
Input
Intermediate
Program
VM Output
Input
Java Compiler
bytecode
ML Interpreter
How can we figure out in a program whether a value is an int, float, or string?
Summary
• A computer is a universal information-processing machine, which can
carry out any process that can be described in sufficient detail
• 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)