0% found this document useful (0 votes)
15 views

ICS 2102-Lecture 2 Programming Concepts

Uploaded by

naffarm74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

ICS 2102-Lecture 2 Programming Concepts

Uploaded by

naffarm74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

ICS 2102-Introduction

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!

• The process of creating software is called programming, and it is the


focus of this course

• Virtually, anyone can learn how to program computers


• It requires only some grit!
Software Categories

• 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

• Programming can be loads of fun! Ubiquitous


Astronomy
• It is an intellectually engaging activity that allows you to express yourself
Computing

through remarkably beautiful creations


Smaller, Faster,
Cheaper Sensors

• Programming develops Gene


valuable problem-solving skills, especially
ones that pertainSequencing
to analysis,
and
Biotechnology
design and implementation
Why Learn Programming?
• Computers have become commonplace in our modern life
• Applying ideas in different fields requires programming

• Programming can be loads of fun!


• It is an intellectually engaging activity that allows you to express yourself
through remarkably beautiful constructs and structures

• Programming develops valuable problem-solving skills, especially


ones that pertain to analysis, design and implementation

• Programmers are in great demand!


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 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

• It can at least perform:


• Arithmetic operations CPU
(e.g., adding 2 numbers)
• Logical operations
(e.g., test if 2 numbers
are equal) Main Secondary
Memory 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 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

 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)
Computer Languages
– Machine Language
• Uses binary code
• Machine-dependent
• Not portable
• Assembly Language
• Uses mnemonics
• Machine-dependent
• Not usually portable
• High-Level Language (HLL)
• Uses English-like language
• Machine independent
• Portable (but must be compiled for different platforms)
• Examples: Pascal, C, C++, Java, Fortran, . . .
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 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!

• 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 language of some computer

Source Code Machine


Compiler
(Program) Code

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

• 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!
A Layered View of the Computer
Application Programs
Word-Processors, Spreadsheets,
Database Software, IDEs,
etc…
System Software
Compilers, Interpreters,Preprocessors, etc.
Operating System, Device Drivers

Machine with all its hardware


Operating System (OS)
· Provides several essential services:
• Loading & running application programs
• Allocating memory & processor time
• Providing input & output facilities
• Managing files of information
Programs
• Programs are written in programming languages
• PL = programming language
• Pieces of the same program can be written in different PLs
• Languages closer to the machine can be more efficient
• As long as they agree on how to communicate
• A PL is
• A special purpose and limited language
• A set of rules and symbols used to construct a computer
program
• A language used to interact with the computer
Machine Language
• The representation of a computer program which is actually
read and understood by the computer.
• A program in machine code consists of a sequence of machine
instructions.
• Instructions:
• Machine instructions are in binary code
• Instructions specify operations and memory cells involved in the
operation
Example:
Operation Address

0010 0000 0000 0100

0100 0000 0000 0101

0011 0000 0000 0110


Assembly Language
• A symbolic representation of the machine language of a
specific processor.
• Is converted to machine code by an assembler.
• Usually, each line of assembly code produces one machine
instruction (One-to-one correspondence).
• Programming in assembly language is slow and error-prone but
is more efficient in terms of hardware performance.
• Mnemonic representation of the instructions and data
• Example:
Load Price
Add Tax
Store Cost
High-level language
• A programming language which use statements consisting of English-like
keywords such as "FOR", "PRINT" or “IF“, ... etc.
• Each statement corresponds to several machine language instructions (one-to-
many correspondence).
• Much easier to program than in assembly language.
• Data are referenced using descriptive names
• Operations can be described using familiar symbols
• Example:
Cost := Price + Tax
Syntax & Semantics
• Syntax:
• The structure of strings in some language. A language's syntax is described by
a grammar.
• Examples:
• Binary number
<binary_number> = <bit> | <bit> <binary_number>
<bit> =0|1
• Identifier
<identifier> = <letter> {<letter> | <digit> }
<letter> =a|b|...|z
<digit =0|1|...|9
• Semantics:
• The meaning of the language
Syntax & Grammars
• Syntax descriptions for a PL are themselves written in a formal
language.
• E.g. Backus-Naur Form (BNF)
• The formal language is not a PL but it can be implemented by a
compiler to enforce grammar restrictions.
• Some PLs look more like grammar descriptions than like instructions.
Compilers & Programs
• Compiler
• A program that converts another program from some source
language (or high-level programming language / HLL) to
machine language (object code).
• Some compilers output assembly language which is then
converted to machine language by a separate assembler.
• Is distinguished from an assembler by the fact that each input
statement, in general, correspond to more than one machine
instruction.
Compilation into Assembly
L
Source Assembly
Program Compiler Language

Assembly Assembler Machine


Language Language
Compilers & Programs
• Source program
• The form in which a computer program, written in some
formal programming language, is written by the
programmer.
• Can be compiled automatically into object code or
machine code or executed by an interpreter.
• Pascal source programs have extension ‘.pas’
Compilers & Programs
• Object program
• Output from the compiler
• Equivalent machine language translation of the source program
• Files usually have extension ‘.obj’

• 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

Input Target Program Output

• Compiler translates source into target (a machine language


program)
• Compiler goes away at execution time
• Compiler is itself a machine language program, presumably
created by compiling some other high-level program
• Machine language, when written in a format understood by
the OS is object code
Interpretation
Source
Program

Interpreter Output

Input

• The interpreter stays around during execution


• It reads and executes statements one at a time
Compilation vs. Interpretation
• Compilation:
• Syntax errors caught before running the program
• Better performance
• Decisions made once, at compile time
• Interpretation:
• Better diagnostics (error messages)
• More flexibility
• Supports late binding (delaying decisions about
program implementation until runtime)
• Can better cope with PLs where type and size of variables
depend on input
• Supports creation/modification of program code on the
fly (e.g. Lisp, Prolog)
Mixture of C & I
Source Intermediate
Program Translator Program

Intermediate
Program
VM Output
Input

• Many programming languages implement this

• Interpreter implements a Virtual Machine (VM).


JAVA
For portability:

Java Compiler

bytecode

ML Interpreter

For flexibility: Just In Time (JIT) compiler translates


bytecode into ML just before execution
Writing Python Commands
• Here is a sample interaction with the Python interpreter:
 print is a built-in function that >>> print("Hello")
allows displaying information Hello
on screen >>> print("Programming is fun!")
Programming is fun!
 When you call the print function, >>> print(3)
the parameters in the parentheses 3
tell the function what to print >>> print(2.3)
2.3
 There is only 1 parameter passed to
print here, which is a textual data (or what is referred to as a string)
Writing Python Commands
• Here is a sample interaction with the Python interpreter:
>>> print("Hello")
Hello
>>> print("Programming is fun!")
Another string parameter Programming is fun!
>>> print(3)
3
An integer (int for short) parameter
>>> print(2.3)
2.3
A float parameter

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

• 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
Next Lecture…
• Basic elements of Python programs (e.g., names, expressions, input
statements, output statements, assignment statements, etc.,)

You might also like