computer science 207 introduction to...

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

Upload: others

Post on 18-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

Computer Science 207

Introduction to Programming

Iowa State University

Lecture 2

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

Page 2: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 3: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 4: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 5: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 6: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 7: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 8: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 9: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 10: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 11: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 12: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 13: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 14: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 15: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 16: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 17: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 18: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 19: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

Early Days of Computers

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

Page 20: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

Special purpose Computational devices

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

Page 21: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 22: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 23: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 24: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 25: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 26: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 27: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 28: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 29: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 30: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 31: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 32: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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

Page 33: Computer Science 207 Introduction to Programmingweb.cs.iastate.edu/~pavan/207/S12/lectures/Jan11.pdf · (Iowa State University) Computer Science 207 Introduction to Programming Lecture

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