Computer Science 207 Introduction to...

Post on 18-Apr-2020

3 views 0 download

Transcript of Computer Science 207 Introduction to...

Computer Science 207

Introduction to Programming

Iowa State University

Lecture 2

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 1 / 24

What is a program?

A sequence of instructions

Each instruction is very basic.

Language to communicate with a computer—programming language

Java is a programming language

A programming language has Syntax and Semantics

Program—Recipe

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 2 / 24

A Toy Example

Toy Computer with Basic Instructions:

draw line – Draws a 5 inch line

turn right

turn left

erase line – Erases 3 inches of a previously drawn line

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 3 / 24

Toy Example

A program to draw a 5 × 5 square

draw line

turn right

draw line

turn right

draw line

turn right

draw line

How about a 10 × 10 Square?

How about a 9 × 9 square?

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 4 / 24

A Recipe in English

Basic Intsructions

All arithmetic operations—addition, division, multiplication, and division

A program to check if 123456 is even number or not

Divide 123456 by 2

If the reminder is zero, then it is even. Otherwise, it is not even.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 5 / 24

A Recipe in English

Basic Intsructions

All arithmetic operations—addition, division, multiplication, and division

A program to check if 123456 is even number or not

Divide 123456 by 2

If the reminder is zero, then it is even. Otherwise, it is not even.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 5 / 24

A Recipe in English

Basic Intsructions

All arithmetic operations—addition, division, multiplication, and division

A program to check if 123456 is even number or not

Divide 123456 by 2

If the reminder is zero, then it is even. Otherwise, it is not even.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 5 / 24

Computational Thinking

A program to check if 13589 is even number or not

A program to check if 278453 is even number or not

A program to check if a given number is even number or not

Input: given number

Divide given number by 2

If the reminder is zero, then given number is even. Otherwise, it is

not even.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 6 / 24

Computational Thinking

A program to check if 13589 is even number or not

A program to check if 278453 is even number or not

A program to check if a given number is even number or not

Input: given number

Divide given number by 2

If the reminder is zero, then given number is even. Otherwise, it is

not even.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 6 / 24

Computational Thinking

A program to check if 13589 is even number or not

A program to check if 278453 is even number or not

A program to check if a given number is even number or not

Input: given number

Divide given number by 2

If the reminder is zero, then given number is even. Otherwise, it is

not even.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 6 / 24

Computational Thinking

A program to check if 13589 is even number or not

A program to check if 278453 is even number or not

A program to check if a given number is even number or not

Input: given number

Divide given number by 2

If the reminder is zero, then given number is even. Otherwise, it is

not even.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 6 / 24

A program to check to check if 246891 is a prime number or not?

Divide 246891 by 2. Is the reminder zero? If so, not a prime.

Divide 246891 by 3. Is the reminder zero? If so, not a prime.

Divide 246891 by 4. Is the reminder zero? If so, not a prime.

··Divide 246891 by 246890. Is the reminder zero? If so, not a prime.

If the reminder is never zero, then its a prime number.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 7 / 24

A program to check to check if 246891 is a prime number or not?

Divide 246891 by 2. Is the reminder zero? If so, not a prime.

Divide 246891 by 3. Is the reminder zero? If so, not a prime.

Divide 246891 by 4. Is the reminder zero? If so, not a prime.

··Divide 246891 by 246890. Is the reminder zero? If so, not a prime.

If the reminder is never zero, then its a prime number.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 7 / 24

Generalization

A program to check to check if a given umber is a prime number or not?

Divide given number by 2. Is the reminder zero? If so, not a prime.

Divide given number by 3. Is the reminder zero? If so, not a prime.

Divide given number by 4. Is the reminder zero? If so, not a prime.

··Divide given number by givennumber − 1. Is the reminder zero? If so,

not a prime.

If the reminder is never zero, then its a prime number.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 8 / 24

Generalization

A program to check to check if a given umber is a prime number or not?

Divide given number by 2. Is the reminder zero? If so, not a prime.

Divide given number by 3. Is the reminder zero? If so, not a prime.

Divide given number by 4. Is the reminder zero? If so, not a prime.

··Divide given number by givennumber − 1. Is the reminder zero? If so,

not a prime.

If the reminder is never zero, then its a prime number.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 8 / 24

A Terse Program

Set n = 2

Repeat the following as long as n is less than given number

Divide given number by n. If the reminder is zero, then declare that the

given number is not a prime and STOP.

Increment the value if n by 1.

Declare that the given number is a prime.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 9 / 24

Why Computer Science?

Car Science?

Telephone Science?

Claculator Science?

What sets Computers part from other machines?

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 10 / 24

Why Computer Science?

Car Science?

Telephone Science?

Claculator Science?

What sets Computers part from other machines?

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 10 / 24

Early Days of Computers

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 11 / 24

Special purpose Computational devices

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 12 / 24

General Purpose Computers

A machine with the capability to read a program (sequence of

instructions), and perform those instructions.

Change the program: Functionality of the machine changes.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 13 / 24

General Purpose Computers

A machine with the capability to read a program (sequence of

instructions), and perform those instructions.

Change the program: Functionality of the machine changes.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 13 / 24

John von Nuemann Computer

“Who Got Einstein’s Office? Eccentricity and Genius at the Institute

for Advanced Study“ by Regis

“Alan Turing: The Enigma“ by Hodges and Hofstadter

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 14 / 24

Computer Architecture

Computer has four components

Input Devices.

Examples?

Output Devices.

Examples?

Memory - Work Space.

Examples?

CPU - Central Processing Unit.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 15 / 24

Program Execution

All computations are done by the CPU.

Programs and Data are stored in Memory.

CPU fetches instructions and data from main memory.

Executes the instruction on the data.

Stores the result in the memory.

Input/Output devices: Interfaces between User and the Computer.

Allow communication between the user and the computer.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 16 / 24

CPU

Two components main components of CPU:

ALU (Arithmetic Logic Unit)

Registers - temporary data storage. data registers, program counter

registers.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 17 / 24

Memory

In Main Memory, Data is stored as “bit” sequence.

Memory contains devices called: Flip-flops

Think as on-off switch

on - 1

off -0

Data is stored as binary string/sequence.

Computers operate in base 2.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 18 / 24

Bits and Bytes

Date is stored in Binary Form: 0 and 1 are called bits.

8 bits is a byte.

Kilobyte (KB) is 210 = 1024 bytes. Or 8192 bits.

Megabyte (MB) is 220 = 1, 048, 576 bytes.

Gigabyte (GB) is 230 = 1, 073, 741, 824 bytes.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 19 / 24

Machine Language

CPU fetches instructions from main memory and executes them.

Contents of the main memory are binary sequences.

instructions should be in binary format.

CPU can “understand” only instructions that are in binary format.

CPU associates a meaning to binary sequences:

add - 0010

Subtract - 0111

compare - 1000

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 20 / 24

A machine language instruction:

0010 0001 0010 0011

Add contents of registers r1 and r2 and store the result in register r3

0111 0010 0011 0001

Subtract contents of registers r1 form contents of register r2 and store the

result in register r3.

Disadvantages ?

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 21 / 24

High Level Language

Computers understand Machine Language.

Tedious to write programs in machine Language.

Write programs in High Language (somewhat similar to english).

Convert into Machine Language

Converting from HLL to ML—compiling or interpreting

Examples of HLL: PASCAL, C, C++, JAVA, BASIC, FORTRAN, LISP,

PROLOG

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 22 / 24

Syntax and Semantics

Language has syntax and semantics.

English language:

Rules that govern the sentence formation—Grammar or Syntax.

Meaning of words/sentences—Semantics.

Learning a language: learn Syntax and Semantics.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 23 / 24

Java is a High Level Programming Language:

It also has Syntax and Semantics.

(Iowa State University) Computer Science 207 Introduction to Programming Lecture 2 24 / 24