Lecture 1 - Introduction

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 52

Fundamentals of

Programming
CS-112 By Dr. Mir Yasir Umair
Associate Professor, MCS, NUST
• Computers
Computer Systems
o Devices for performing computations at high speeds with great accuracy
o A machine that can be programmed to manipulate symbols. Can perform complex & repetitive procedures quickly,
precisely and reliably. Can quickly store and retrieve large amounts of data.

• Program
o A set of instructions for a computer to follow, written in specific programming language

FOP by DR MIR YASIR UMAIR


Computers and Computer Languages
• Computers are everywhere
o how many computers do you own?

• Computers are useful because they run programs


o program is simply a set of instructions to complete some task
o how many different programs do you use in a day?

3
Definitions
• program: A set of instructions that are to be carried out by a computer.

• program execution: The act of carrying out the instructions contained


in a program.
o this is done by feeding the instructions to the CPU

• programming language: A systematic set of rules used to describe


computations, generally in a format that is readable and editable by
humans.
o in this class we use Java

4
Computer Systems
• Hardware (HW)
oActual physical machines (equipment) that make up the
computer
• Software (SW)
oA collection of programs used by a computer
oA set of instructions provided by the programmer that the
computer follows.
oProgram instructions have to be stored in main memory
before they can be executed.

FOP by DR MIR YASIR UMAIR


Computer Systems
• Computer Categories:
o Microcomputers (Personal computers / PCs)
• Used by a single person
o Workstations:
• Largest microcomputers
o Minicomputers:
• Can be used by many people simultaneously by using several terminals connected to the same
CPU
o Main frame computers:
• Faster & larger than minicomputers
o Super computers:
• Most powerful mainframe computers
• Of which category is you computer at home??

FOP by DR MIR YASIR UMAIR


Hardware (HW): Organization
• Computer HW usually consists of:
o CPU
o Main memory (RAM & ROM)
o I/O Devices
o Secondary Memory
• CPU & main memory are the heart of the computer
• Usually the CPU, main memory and secondary memory are housed in a single cabinet

I/O Devices
CPU

Secondary Memory

Main Memory

FOP by DR MIR YASIR UMAIR


Hardware (HW):
Central Processing Unit (CPU)
• Executes programs
• Performs calculations
o Arithmetic
• Add, subtract, divide, multiply, … etc.
o Logical
• Compare, test for true/false
• Controls & coordinates the other parts of the computer.
• The CPU controls just about everything else by executing instructions
o stores data in memory
o retrieves data from memory
o performs computations
o sends and gets data from other devices

FOP by DR MIR YASIR UMAIR


Hardware (HW): Memory
• Main (Primary) Memory:
o Ordered sequence and specific number of memory locations (Bytes, words) that
have unique addresses indicating their relative positions
o Fast, expensive, short term memory
o Holds intermediate results and serves as “scratch paper”
o Needed to carry out program instructions
o Types:
• RAM: Random Access Memory
(vs. Sequential Access Memory)
• Volatile (i.e. contents disappear when the computer is switched off)
• Writable (except where forbidden by the software)
• ROM: Read Only Memory
• Non-volatile
• Also usually random access

FOP by DR MIR YASIR UMAIR


Hardware (HW): Memory
• Secondary (auxiliary) memory
o Used for keeping a permanent records of information
o Holds programs and data between jobs
o Keeps data or program files for later use
o Slower, cheaper, long-term memory
o Common forms
• Diskettes, magnetic tapes, hard disk, CD-ROM’s, DVD
o Some types are removable

FOP by DR MIR YASIR UMAIR


Hardware (HW)
• I/O (Input/Output) devices
o Allow the user to communicate with the computer.
o A single computer could be connected to more than one input or output device.
o Examples:
• Input: Keyboard, mouse, scanner, voice
• Output: Screen, printer, voice

FOP by DR MIR YASIR UMAIR


Hardware Devices ( Examples)
• Input:
o Keyboard
o Mouse, Touchpad, Joystick
o Pen & tablet, Perforated cards
• Output:
o Printer
o Screen (monitor)
o Teletype
• Input & Output:
o Auxiliary Devices:
• disks (hard disk, floppies, DVDs/CDs, USB keys, Flash memories)
• tape drives
o Touch screen
o Network cards (intranet, internet)

FOP by DR MIR YASIR UMAIR


Hardware Devices (3)
• Bus: A group of wires through which the other devices communicate.

I/O devices are controlled by drivers, which are very specialized kinds of programs

FOP by DR MIR YASIR UMAIR


Hardware Trends
• Every year or two computer power approximately doubles
o Memory size (RAM)
• Memory used to execute programs
o Secondary storage (permanent storage)
• E.g. disk storage, used to to hold programs and data over time
o Processor speeds
• Speed at which computers execute their programs

FOP by DR MIR YASIR UMAIR


Hardware/Software Trends
• Applications:
o Rapidly increasing hardware power allows applications to get bigger and more complex

• Costs
o Hardware costs dropping
o Software development costs rising
• Software development complexity
• Programmer salaries
• Cost of slipping schedules
o Unanticipated interactions in complex systems
o Unpredictability of software development times

FOP by DR MIR YASIR UMAIR


Hardware/Software Architectures
• Mainframe Era
o 1940’s-70’s: mainframe computer, minicomputers
o Environments:
• Batch environments, batch processing
o Files are basis for I/O: fixed formats, minimal device I/O
o Error recovery
o Lack of timing constraints
• Interactive environments
o Terminal and file I/O:
o Interactive error handling
o Faster performance

FOP by DR MIR YASIR UMAIR


• Personal Computers Era
o 1978: the Apple II ran BASIC
• Educational use
o 1981: IBM released the first PC
o 1984: Macintosh
o Window environments:
• OO models are ideal (Smalltalk)
• Must interact with many I/O devices (file I/O is less important)
o Embedded systems
• Error handling
• Real-time response
• Distributed systems with concurrently running tasks

FOP by DR MIR YASIR UMAIR


• Networking Era:
o LANs (Local Area Networks):
client-server model
• Airline reservations, banking
o Internet
• 70’s: ARPANET: telnet, FTP, SMTP protocols
• late 80’s: HTML and HTTP added
o Issues and Effects:
• Static web pages with URLs for access
URL = Uniform Resource Locator
• Dynamic web pages for e-commerce (Perl, JAVA, etc.)
• Security
• Performance (multiple clients)
o Offloading work to client

FOP by DR MIR YASIR UMAIR


Hardware/Software Concepts
• Computers manipulate instructions and data
o Represented in similar ways
o Used in different ways
o Representation is binary (digital hardware is binary)
• Numbers vs. symbols
o Computers represent everything as numbers
o But numbers can represent symbols
o Can perform “symbolic” computation
• Beginning of Artificial Intelligence

FOP by DR MIR YASIR UMAIR


HW/SW Concepts: Data
· What is it?
- Numbers, characters, images, or other method of recording
- Can be assessed by a human or (especially) input into a computer, stored and processed there, or transmitted
on some digital channel.
- Nearly always represent data in binary.
- Has no meaning on its own.
- When interpreted by data processing system it takes on meaning and becomes information.
· Storage
o Setting of individual bits to specific values, destroying its previous contents
· Retrieval
o Copying the contents of a particular memory cell to another storage area.
o Original data remains unchanged

FOP by DR MIR YASIR UMAIR


HW/SW Concepts: Representation
• Digit / Bit
o Smallest unit of information/storage, sufficient to hold one bit
o Can take one of two values
(true/false, 1/0, or yes/no)
o Corresponds to an input/output being on or off
• Byte
o Smallest addressable unit of storage
o Usually 8 bits
o Typically holds one character
o Can represent 256 different values

FOP by DR MIR YASIR UMAIR


HW/SW Concepts: Representation
• Word
o Fundamental unit of storage in a computer
o Word size is one of its chief distinguishing characteristics of a computer
o Typical size in modern computers: 32 bits (4 bytes) or 64 bits (8 bytes)
o An instruction is usually one or more words long
o A word can be used to hold a whole number of characters

FOP by DR MIR YASIR UMAIR


Software Categories
Software Categories
• System SW
o Programs written for computer systems
• Compilers, operating systems, …
• Application SW
o Programs written for computer users
• Word-processors, spreadsheets, & other application packages

FOP by DR MIR YASIR UMAIR


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


FOP by DR MIR YASIR UMAIR
Operating System (OS)
· Provides several essential services:
o Loading & running application programs
o Allocating memory & processor time
o Providing input & output facilities
o Managing files of information

FOP by DR MIR YASIR UMAIR


Programs
• Programs are written in programming languages
o PL = programming language
o 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
o A special purpose and limited language
o A set of rules and symbols used to construct a computer program
o A language used to interact with the computer

FOP by DR MIR YASIR UMAIR


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

FOP by DR MIR YASIR UMAIR


Machine Language
• The representation of a computer program which is actually read and understood by
the computer.
o A program in machine code consists of a sequence of machine instructions.
• Instructions:
o Machine instructions are in binary code
o 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

FOP by DR MIR YASIR UMAIR


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

FOP by DR MIR YASIR UMAIR


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

FOP by DR MIR YASIR UMAIR


Syntax & Semantics
• Syntax:
o The structure of strings in some language. A language's syntax is described by a
grammar.
o 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:
o The meaning of the language

FOP by DR MIR YASIR UMAIR


Syntax & Grammars
• Syntax descriptions for a PL are themselves written in a formal language.
o 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.

FOP by DR MIR YASIR UMAIR


Compilers & Programs
• Compiler
o A program that converts another program from some source language (or high-level programming language / HLL) to machine
language (object code).
o Some compilers output assembly language which is then converted to machine language by a separate assembler.
o Is distinguished from an assembler by the fact that each input statement, in general, correspond to more than one machine
instruction.

FOP by DR MIR YASIR UMAIR


Compilation into Assembly L
Source Assembly
Program Compiler Language

Assembly Assembler Machine


Language Language

FOP by DR MIR YASIR UMAIR


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.

FOP by DR MIR YASIR UMAIR


An Important Realization
• Computers are stupid.

• Computers can’t read minds.

• Computers seldom make mistakes.

• If the computer is not doing what we want, it’s because WE made a mistake.
• Users are not always very intelligent

37
A Simple C++ Program
• Preprocess directives are not
statements (no semi-colon ;)

• Extra space in < >

• Case Sensitive
o Main vs main

38
Listing 1.2

• Output?

39
Listing 1.3

• Output?

40
Output?

41
C++ Program

Development Cycle

42
Programming Style and
Documentation
• Appropriate Comments
• Comment Styles
• Proper Indentation and Spacing

43
Errors?

44
Programming Errors
• Syntax Errors

• Runtime Errors

• Logic Errors

45
Programming Errors
• Syntax Errors
;

46
Programming Errors
• Syntax Errors

• Runtime Errors

47
Programming Errors
• Syntax Errors

• Runtime Errors

• Logic Errors

48
Common Errors

Missing Braces, Semicolons, Quotation Marks and Misspelling Names


49
Work…

50
. Homework
• Think of 3 tasks you have encountered in your studies that you would
like to write a program for.
• Deadline.. Next class

FOP by DR MIR YASIR UMAIR


Thanks
Questions ???

FOP by DR MIR YASIR UMAIR

You might also like