computer science 1 week 12. this week... qbasic for loops qbasic for loops computer history computer...

99
Computer Science 1 Week 12

Upload: ashley-barton

Post on 24-Dec-2015

236 views

Category:

Documents


1 download

TRANSCRIPT

Computer Science 1Week 12

This Week ...This Week ...

• QBasic For LoopsQBasic For Loops

• Computer HistoryComputer History 11stst – 3 – 3rdrd Generation computers Generation computers

• Website Project is Due!Website Project is Due!

QBasic QBasic For LoopsFor Loops

Looping a variable through a rangeLooping a variable through a range

For For StatementStatement

• LoopsLoops a block of statements a block of statements the block is executed numerous timesthe block is executed numerous times this allows you to create repetitionthis allows you to create repetition

• When do you use it?When do you use it? execute a block a number of timesexecute a block a number of times process data using the set of numbersprocess data using the set of numbers

• VeryVery versatile in QBasic Lite versatile in QBasic Lite

For For StatementStatement

• Uses a numeric variableUses a numeric variable starts with an initial valuestarts with an initial value changes its value for each loopchanges its value for each loop stops once its value is greater than end value stops once its value is greater than end value

• Semantics ...Semantics ... assigns the initial/next value to the variableassigns the initial/next value to the variable executes the block if the variable is not greater executes the block if the variable is not greater

than the end valuethan the end value repeats the processrepeats the process

Default: increment by one (+ 1) in each loop automatically

FOR variable = start TO end

Statements

NEXT

Basic Basic For For Statement Statement SyntaxSyntax

FOR x = 1 to 5

PRINT x

NEXT

Simple LoopSimple LoopExampleExample

1

2

3

4

5

Simple LoopSimple LoopExample OutputExample Output

FOR x = 1 to 5

PRINT x, x ^ 2

NEXT

Simple Loop 2Simple Loop 2ExampleExample

1 1

2 4

3 9

4 16

5 25

Simple Loop 2Simple Loop 2Example OutputExample Output

FOR x = -5 to -1

PRINT x, x ^ 2

NEXT

Negative ValuesNegative ValuesExampleExample

-5 25

-4 16

-3 9

-2 4

-1 1

Negative ValuesNegative ValuesExample OutputExample Output

FOR Quiz = 1 to 3

INPUT "Score: ", Score

LET Sum = Sum + Score

NEXT

PRINT "Average is"; Sum / 3

AverageAverageExampleExample

Score: 97

Score: 72

Score: 83

Average is 84

AverageAverageExample OutputExample Output

Inner LoopsInner Loops

• Like all QBasic blocks, you can Like all QBasic blocks, you can put loops within loopsput loops within loops

• The loop in the inner-most block The loop in the inner-most block is an is an inner loopinner loop

• The loop in the outer-most The loop in the outer-most block is an block is an outer loopouter loop

FOR x = 10 to 11

PRINT "Outer: "; x

FOR y = 1 to 2

PRINT " Inner: "; y

NEXT

NEXT

Inner LoopInner LoopExampleExample

Outer: 10

Inner: 1

Inner: 2

Outer: 11

Inner: 1

Inner: 2

Inner LoopInner LoopExample OutputExample Output

FOR x = 5 to 1

PRINT x

NEXT

Not-So-Simple LoopNot-So-Simple LoopExampleExample

Negative Range

Not-So-Simple LoopNot-So-Simple LoopExample OutputExample Output

Nothing!

Not-So-Simple LoopNot-So-Simple LoopWhy?Why?

• The The For ... NextFor ... Next Statement Statement starts at the first valuestarts at the first value continues continues untiluntil the value of the variable the value of the variable is greater is greater

thanthan the end value the end value• If the start value is greater than the end If the start value is greater than the end

value, the loop value, the loop nevernever executesexecutes

The The StepStep Clause Clause

• Normally, the Normally, the For ... NextFor ... Next Statement Statement increments using increments using +1+1

• The Step Clause allows you to change the The Step Clause allows you to change the increment valueincrement value

• This allows you to:This allows you to: create negative loopscreate negative loops create loops that jump by a specific valuecreate loops that jump by a specific value

FOR var = start TO end STEP inc

Statements

NEXT

Full Full For ... NextFor ... Next Statement SyntaxStatement Syntax The increment is

defined by the step here

FOR x = 1 TO 10 STEP 2

PRINT x

NEXT

Large StepLarge StepExampleExample The increment for

variable x in each loop is 2 here

1

3

5

7

9

Large Step Large Step Example OutputExample Output

FOR x = 5 TO 1 STEP -1

PRINT x, x ^ 2

NEXT

Negative StepNegative StepExampleExample

The increment for variable x in each loop is 1 here

5 25

4 16

3 9

2 4

1 1

Negative Step Negative Step Example OutputExample Output

FOR x = 1 TO 5 STEP 0

PRINT x, x ^ 2

NEXT

Zero StepZero StepExampleExample

RUNTIME ERROR:

Line 1 : Step value is zero

Zero Step Zero Step Example OutputExample Output

While Loop vs. While Loop vs. For LoopFor Loop

• For LoopFor Loop is a form of the is a form of the While LoopWhile Loop• As a result:As a result:

a a While Loop While Loop can implement any can implement any For LoopFor Loop the the For LoopFor Loop is used as a shortcut notation is used as a shortcut notation

x = 1

DO WHILE x <= 5

PRINT x LET x = x + 1 LOOP

While Loop vs. While Loop vs. For LoopFor Loop

FOR x = 1 TO 5

PRINT xNEXT

1

2

3

4

5

While Loop vs. While Loop vs. For Loop OutputFor Loop Output

1

2

3

4

5

QBasic LabQBasic Lab

For Loops – For Loops – 99 Bottles of Something99 Bottles of Something

Lab: 99 Bottles of Lab: 99 Bottles of SomethingSomething

• Overview:Overview: You will use QBasic to print the 99 You will use QBasic to print the 99

Bottles of Beer (Water) SongBottles of Beer (Water) Song Use another drink, water or juice Use another drink, water or juice

• Objectives:Objectives: Use a For Statement to print the songUse a For Statement to print the song Use an If Statement inside the loopUse an If Statement inside the loop

Remember ...Remember ...

• Turn your program Turn your program && your output your output to Lab10 in SacCTto Lab10 in SacCT

• You must do your own workYou must do your own work• If you do not turn in your program, If you do not turn in your program,

you will you will notnot get credit! get credit!

The First The First ComputerComputer

Computer HistoryComputer History

Punched CardsPunched Cards

• Invented by Invented by Joseph-Marie JacquardJoseph-Marie Jacquard

• Textile industryTextile industry control the flow of yarn in a loomcontrol the flow of yarn in a loom used to create patternsused to create patterns

• Adapted to Adapted to automatic pianosautomatic pianos computer datacomputer data

Charles BabbageCharles Babbage

• Known as theKnown as the“Father of Computers”“Father of Computers”

• Created several Created several calculatorscalculators

• Developed the first Developed the first computercomputer

The Difference EngineThe Difference Engine

• In the 1800’sIn the 1800’s log and trig tables were calculated manuallylog and trig tables were calculated manually Babbage knew machines could compute theseBabbage knew machines could compute these

• He designed the He designed the Difference EngineDifference Engine could compute logarithmic and trigonometric datacould compute logarithmic and trigonometric data first machine to be first machine to be automatic in actionautomatic in action it was not finished in his lifetimeit was not finished in his lifetime

The Difference Engine:The Difference Engine:continuedcontinued

• It was completed in March 2008 at the Science It was completed in March 2008 at the Science Museum, London 150 years after its original Museum, London 150 years after its original design, and is faithful to Babbage’s original design, and is faithful to Babbage’s original design (1847-49).design (1847-49).

• It is one of only two Babbage engines consisting It is one of only two Babbage engines consisting of 8,000 parts in bronze, cast iron and steel.of 8,000 parts in bronze, cast iron and steel.

• It weighs five tons and measures 11 feet long and It weighs five tons and measures 11 feet long and 7 feet tall. 7 feet tall.

Computer History Museum, San Jose (June, 2012)Computer History Museum, San Jose (June, 2012)

Computer History Museum, San Jose (June, 2012)Computer History Museum, San Jose (June, 2012)

The Analytical EngineThe Analytical Engine

• A A general-purpose general-purpose calculating machinecalculating machine• He began work in He began work in 18341834

he never finished ithe never finished it it has it has nevernever been built been built

• If built, it would have been one of theIf built, it would have been one of theWonders of the WorldWonders of the World

The Analytical EngineThe Analytical Engine

• Use punched cards to run calculationsUse punched cards to run calculations• Had all the attributes of a modern computerHad all the attributes of a modern computer

Programs – looping, branching – Programs – looping, branching – "Barrels""Barrels" Memory – Memory – "The Store""The Store" Arithmetic Logic Unit – Arithmetic Logic Unit – "The Mill""The Mill" Bus – Bus – "The Rack""The Rack"

The Store

The Rack

The Mill

Barrels

Ada LovelaceAda Lovelace

• MathematicianMathematician• Spoke several languagesSpoke several languages• University of Turin lecturerUniversity of Turin lecturer

During a nine-month period in 1842–43, Lovelace During a nine-month period in 1842–43, Lovelace translated Italian mathematician Luigi Menabrea's translated Italian mathematician Luigi Menabrea's memoir (to English) on Babbage's Analytical Engine. memoir (to English) on Babbage's Analytical Engine. With the article, she appended a set of notes that With the article, she appended a set of notes that included a method for calculating a sequence of included a method for calculating a sequence of Bernoulli numbers with the Engine.Bernoulli numbers with the Engine.

Her method is recognized as the worldHer method is recognized as the world’’s first computer s first computer program.program.

Ada LovelaceAda Lovelace

• She made She made extensiveextensive notes notes several volumes, in factseveral volumes, in fact designed several programsdesigned several programs this included how to calculate this included how to calculate

Bernoulli numbers Bernoulli numbers • World's first programmerWorld's first programmer

The 1890 The 1890 Census CrisisCensus Crisis

Computer HistoryComputer History

Census CrisisCensus Crisis

• The The United States Federal ConstitutionUnited States Federal Constitution population must be calculated - censuspopulation must be calculated - census this must be done every 10 yearsthis must be done every 10 years used in the House of Representativesused in the House of Representatives

• However, the U.S. populationHowever, the U.S. population had grown extremely large had grown extremely large the people could the people could notnot be counted in 10 years be counted in 10 years

Herman HollerithHerman Hollerith

• Developed:Developed: first automatic card-feed first automatic card-feed

mechanismmechanism enhanced card readingenhanced card reading the first key punch – 200 to the first key punch – 200 to

300 cards per hour300 cards per hour• Used Used electricityelectricity

Herman HollerithHerman Hollerith

• His systemHis system was used for was used for 18901890 census census only took 9 months!only took 9 months!

• Hollerith foundedHollerith founded Tabulating Machine CompanyTabulating Machine Company it later became it later became International International

Business MachinesBusiness Machines

Birth of Computer Birth of Computer ScienceScience

Computer HistoryComputer History

Alan TuringAlan Turing

• Mathematician, logician & Mathematician, logician & cryptographercryptographer

• "Father of Computer Science""Father of Computer Science" Highest award in Computer Highest award in Computer

Science is the Science is the Turing AwardTuring Award Developed Turing MachinesDeveloped Turing Machines

Alan Turing'sAlan Turing'sMajor WorksMajor Works

• Developed Turing MachinesDeveloped Turing Machines invented in invented in 19371937 logical model – not an actual logical model – not an actual

computercomputer proved programmingproved programming

• Turing TestTuring Test artificial Intelligenceartificial Intelligence no computer has yet passed itno computer has yet passed it

Computer Computer GenerationsGenerations

• Computers are historically classified by Computers are historically classified by their their generationgeneration

• Each generation ...Each generation ... marks a new, major, technologymarks a new, major, technology changes how computers are built and/or usedchanges how computers are built and/or used their are currently their are currently fourfour generations generations

11stst Generation Generation ComputersComputers

Computer HistoryComputer History

First Generation First Generation ComputersComputers

• 1946 to 19581946 to 1958• Used the Used the vacuum tubevacuum tube

they consumed a lot of powerthey consumed a lot of power they also tended to burn out quicklythey also tended to burn out quickly

• Programs written in machine languagePrograms written in machine language• Data Data

read with Punched Cardsread with Punched Cards stored with Magnetic Tapestored with Magnetic Tape

Vacuum TubesVacuum Tubes

Atanasoff-Berry Atanasoff-Berry ComputerComputer

• First electronic digital computerFirst electronic digital computer• DevelopmentDevelopment

John Atanasoff John Atanasoff && Clifford Berry Clifford Berry Iowa State UniversityIowa State University built from built from 1937 to 19421937 to 1942

• Speed: 60 HzSpeed: 60 Hz

Atanasoff-Berry Atanasoff-Berry ComputerComputer

• Features:Features: binary digits to represent all databinary digits to represent all data calculations using electronicscalculations using electronics memory is separate from CPUmemory is separate from CPU

• Rebuilt in Rebuilt in 19971997 cost of $350,000cost of $350,000 it worked!it worked!

ENIACENIAC

• EElectronic lectronic NNumerical umerical IIntegrator ntegrator AAnd nd CComputeromputer

• DevelopmentDevelopment John Eckert and John W. Mauchly John Eckert and John W. Mauchly U.S. Ballistics Research Laboratory U.S. Ballistics Research Laboratory Needed to fight World War II – then Cold WarNeeded to fight World War II – then Cold War Compute ballistic firing tablesCompute ballistic firing tables

ENIACENIAC

• Designed to be Designed to be Turing CompleteTuring Complete• Operational in February Operational in February 19461946• FeaturesFeatures

5 KHz (5000 Hz)5 KHz (5000 Hz) programmed by rewiring – programmed by rewiring – pre 1948pre 1948 based on decimal – not binarybased on decimal – not binary weighed 30 tons,18 feet high, 80 feet longweighed 30 tons,18 feet high, 80 feet long

ENIACENIAC

• A tube burned out once every 2 daysA tube burned out once every 2 days• Retired in Retired in 19551955

operational for only operational for only 9 years9 years estimated to have performed more estimated to have performed more

calculations than all of humanity had ever calculations than all of humanity had ever done priordone prior

Grace HopperGrace Hopper

• Admiral in the U.S. NavyAdmiral in the U.S. Navy• Worked on several projectsWorked on several projects

Mark II MainframeMark II Mainframe COBOL Programming LanguageCOBOL Programming Language CompilersCompilers Standardized software testingStandardized software testing

• Discovered the first Discovered the first "bug""bug"

September 9, 1945September 9, 1945

UNIVACUNIVAC

• UnivUniversal ersal AAutomatic utomatic CComputeromputer• Released in Released in 19511951• First commercial computerFirst commercial computer

43 were sold to government and industry43 were sold to government and industry used to tabulate the censusused to tabulate the census

UNIVACUNIVAC

• Some featuresSome features ran programs on punched cardsran programs on punched cards secondary storage: metal oxide tapesecondary storage: metal oxide tape clock: clock: 2.25 MHz2.25 MHz

• Predicted the 1952 electionPredicted the 1952 election Eisenhower over StevensonEisenhower over Stevenson news agencies held the results - news agencies held the results - great publicitygreat publicity

IBM RAMAC 305IBM RAMAC 305

• RRandom andom AAccess ccess MMethod of ethod of AAccounting ccounting and and CControlontrol

• Released in Released in 19561956• First computer to use a hard driveFirst computer to use a hard drive

based on record technologybased on record technology could store 5 million 7-bit characterscould store 5 million 7-bit characters

22ndnd Generation Generation ComputersComputers

Computer HistoryComputer History

Second Generation Second Generation ComputersComputers

• 1959 to 19641959 to 1964• Used Used TransistorsTransistors

more reliable than vacuum tubesmore reliable than vacuum tubes required less powerrequired less power

• Compilers were developedCompilers were developed• Disk Storage was developedDisk Storage was developed

DEC DEC PDP-1PDP-1

• PProgrammed rogrammed DData ata PProcessor-rocessor-11• Digital Equipment Corporation (DEC)Digital Equipment Corporation (DEC)• Released Released 19601960• FeaturesFeatures

read data from taperead data from tape used a cathode ray tube (T.V.) to display dataused a cathode ray tube (T.V.) to display data

• The first hacker computerThe first hacker computer• World's first digital video game: Space WarWorld's first digital video game: Space War

33rdrd Generation Generation ComputersComputers

Computer HistoryComputer History

Third Generation Third Generation ComputersComputers

• 1965 to 19701965 to 1970• Used integrated circuitsUsed integrated circuits

increased speedincreased speed smaller sizesmaller size lower costlower cost even less electricityeven less electricity

• Birth of the minicomputerBirth of the minicomputer

IBM 360IBM 360

• Most expensive computer project everMost expensive computer project ever the "5 billion dollar gamble"the "5 billion dollar gamble" this is over 30 billion by today's dollarsthis is over 30 billion by today's dollars

• Released Released 19641964• BreakthroughBreakthrough

architecture and implementation are differentarchitecture and implementation are different microcode technologymicrocode technology

IBM 360IBM 360

• It was the It was the firstfirst platform computer platform computer allall 360 computers would be compatible 360 computers would be compatible initial models: 30, 40, 50, 60, 62, and 70 initial models: 30, 40, 50, 60, 62, and 70 peripherals could be interchangedperipherals could be interchanged

• Huge successHuge success companies could buy the best modelcompanies could buy the best model companies could upgrade their systems latercompanies could upgrade their systems later

DEC PDP-8DEC PDP-8

• First successful commercial First successful commercial minicomputerminicomputer released in released in 19651965 it cost less than $20,000it cost less than $20,000

• FeaturesFeatures up to 32k of RAMup to 32k of RAM 1 MHz – varied by model1 MHz – varied by model multiple versions availablemultiple versions available