getting into cs - massachusetts institute of...

16
Getting Into CS

Upload: others

Post on 30-Jan-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

  • Getting Into CS

  • 2

    Getting into CS

    Nowthatyouhavehadachancetoexperiencehowcomputersoperate,youaregoingtolearnsomespecificprogrammingconcepts.Theseinclude:

    •  Variables•  Datatypes•  Loops•  CondiBonalstatements

  • 3

    Data Types & Variables

    Let’sstartwithashortdebriefofthesandwichacBvity:•  WeusedtheEnglishlanguagetocreateaseriesofinstrucBons

    (theprogram)to‘acton’thematerialsor‘data’(e.g.,bread,jelly,andsoon)toproduceacertainoutputorresult(asandwich).

    •  Incomputerscienceaprogramminglanguagehelpsaprogrammermakeacomputerdothings.TheprogramisasetofinstrucBonsthattellsacomputerwhattodowithcertaindataandhowtodoittoproduceacertainoutput.

    •  Aprogramusuallycontainsdifferenttypesofdatatorepresentnumbers,leLersandsoon.Programsneedawayof‘remembering’thedatatheyaregiven;thisisusuallydonebycreaBngvariablesandpuOngdataintothem.

  • 4

    Variables

    Whatisavariable?• Imaginethatavariableisaboxthatcarriesacertainvalue.• Youcantakethevalueoutofthisvariable‘box’andputinanewone.• Youcanlookupthevalueintheboxanddothingswithit.• Youcantakethevaluesfromotherboxesandputtheminthisbox.

  • 5

    Declaring Variables & Data Types

    Inmostprogramminglanguages,youneedtodeclarevariablesbeforeyouusethem.

    •  Meansthatyoumaketheboxesthatyouwanttostorevaluesin.

    •  Justlikeinreallife,boxescomeinallshapesandsizes—andyouneedtherightboxfortheobjectyouaregoingtoputinit.•  Forexample:Youcannotfitabicycleintoajewelrybox,

    right?•  Inprogrammingyouhavetohavetheright“size”boxfor

    the“size”ofyourdata.•  Someboxescan'tstorecertainvaluesbeyondtheirlimits

    (datatype).

  • 6

    Activity

    Let’slookbackatGe#ngtoKnowtheZRIDE.Howwouldyougo

    aboutchangingthevalueofthevariablesinthistutorial?(Hint:Youassignitadifferentvalue!)

  • 7

    Answer

    voidloop(){Y=1.5;setPos(0,Y,0);return;}Becomesvoidloop(){Y=2.5;setPos(0,Y,0);return;}

  • 8

    More on Data Types

    •  InmathemaBcs,therearevariouswaysofrepresenBngnumbersandotherdata:•  differenttypesofnumbers(e.g.,wholenumbersanddecimals,posiBveandnegaBvenumbers)

    •  setsofdatalikecoordinatepointsandvectors•  Inprogramming,anypieceofdatausedmustbe‘declared’to

    tellthecomputertostoreanduseit.•  Ifyoudon’ttypeinadeclaraBonofyourdatapriortousingit,yourcomputerwilloutputanerror.

    •  AlldeclaraBonsusethefollowingstructure: DataTypeName;

  • 9

    More on Data Types: Example

    Example:floatdistance;• ThisdeclaraBon,istellingthecomputerthetypeofdataitwillprocess—somethingknownasa‘floatnumber’—andthenameofthedatathatwillbeprocessed—inthiscase,datameasuringadistance.• InthesandwichacBvity,thedataisthematerials(peanutbuLer,jelly,andbread),andtheinstrucBonstellingtheperformerwhattodowiththe‘data’consBtutesthe‘program’.

  • 10

    More on Variables

    •  Remembertheanalogyaboutboxes?Well,whenyouarewriBngacomputerprogram,youoeenwanttheprogramtobeableto“remember”avalue.Youcandothisbyassigningtheimportantvaluetoanamedvariable.

    •  Eachvariablehasanameandcontainsavalue.Forexample,avariablecanbenamedXandcontainthevalue2.ThusyoucansayX+Xequals4.Variablesarecategorizedbythetypesofdatatheystore.YoumusttellthecomputerwhattypeofdatayouwillbepuOnginthevariable.ALempBngtoputdataofthewrongtypeintoavariablewillcauseanerror.

  • 11

    Activity Introducing Loops

    InthelastacAvity,youwereaskedtowriteinstrucAonstomakeONEsandwich.HowwouldyouwriteinstrucAonsformaking

    hundredsofsandwiches,forexample,inacafeteria?(Tomakethingssimpler,youcanassumeyouhaveunlimited

    suppliesofthematerials!)

    Let’sstartbylookingatthefinalinstrucAonsformakingsandwichesagain.

  • 12

    Review Final Instructions for PBJ Sandwich

    a)  Takeasliceofbreadb)  OpenthejarofpeanutbuLerbytwisBngthelidcounter

    clockwisec)  Pickupaknifebythehandled)  InserttheknifeintothejarofpeanutbuLere)  PulltheknifeoutofthejarofpeanutbuLerandrunitacross

    thesliceofbreadf)  Takeasecondsliceofbreadg)  Repeatstepsa–ewiththesecondslideofbreadandthejarof

    jellyh)  Pressthetwoslicesofbreadtogethersuchthatthepeanut

    buLerandjellymeet

  • 13

    Activity Introducing Loops, continued

    CanyouthinkofoneaddiAonalinstrucAonyoucanaddtotheseinstrucAonstotellthe‘performer’oracomputerhowtomake

    100sandwiches?

  • 14

    Example Answer to Activity Introducing Loops

    a)  Takeasliceofbreadb)  OpenthejarofpeanutbuLerbytwisBngthelidcounter

    clockwisec)  Pickupaknifebythehandled)  InserttheknifeintothejarofpeanutbuLere)  PulltheknifeoutofthejarofpeanutbuLerandrunit

    acrossthesliceofbreadf)  Takeasecondsliceofbreadg)  Repeatstepsa–ewiththesecondslideofbreadandthejar

    ofjellyh)  Pressthetwoslicesofbreadtogethersuchthatthepeanut

    buLerandjellymeeti)  RepeatStepsa-h99more=mes

  • 15

    Activity Introducing Conditionals

    SupposethecafeteriaservesPB&Jsandwicheseverydayexcept

    onFridays,whenitservesbothsandwichesandpizza.

    Sincemoststudentswouldrathereatpizza,thecafeteriashouldmakefewersandwichesonFriday.

    Howcouldyouchangeyourprogramtomakefewersandwiches

    onlyonFridays?

  • 16

    Example Answer to Activity Introducing Conditionals

    a)  Takeasliceofbreadb)  OpenthejarofpeanutbuLerbytwisBngthelidcounter

    clockwisec)  Pickupaknifebythehandled)  InserttheknifeintothejarofpeanutbuLere)  PulltheknifeoutofthejarofpeanutbuLerandrunit

    acrossthesliceofbreadf)  Takeasecondsliceofbreadg)  Repeatstepsa–ewiththesecondslideofbreadandthejar

    ofjellyh)  Pressthetwoslicesofbreadtogethersuchthatthepeanut

    buLerandjellymeeti)  RepeatStepsa-h99more=mesunlesstodayisFriday

    IftodayisFriday,repeatstepsa-h29more=mes