Top Banner
Fundamental Programming Principles: Variables and Data Types Beyond the Mouse GEOS 436/636 Jeff Freymueller, Sep 5, 2017 “The Uncomfortable Truths Well”, hQp://xkcd.com/568 (April 13, 2009)
23

Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Feb 06, 2018

Download

Documents

lyquynh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

FundamentalProgrammingPrinciples:VariablesandDataTypes

BeyondtheMouseGEOS436/636

JeffFreymueller,Sep5,2017

“TheUncomfortableTruthsWell”,hQp://xkcd.com/568(April13,2009)

Page 2: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Today’sSchedule

•  Howdoescomputerprogrammingwork– Whatisaprogramminglanguage?– Whatisaprogram?

•  VariablesandDataTypes– Howdowestorevaluesofdifferentkinds?

•  Numbers•  Stringsoftext•  Morecomplicatedthings(likeimages,forexample)

Page 3: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Definiaons

•  Aprogramminglanguageisanunambiguousaraficiallanguagethatismadeupofasetofsymbols(vocabulary)andgrammaacalrules(syntax)toinstructamachine.

•  Aprogramisasetofinstrucaonsinoneormulapleprogramminglanguagesthatspecifiesthebehaviorofamachine.

•  Compila.onorinterpreta.onistheverificaaonofaprogramanditstranslaaonintomachinereadableinstrucaonsofaspecificplaborm.

Page 4: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

WhatLanguageDoestheCPUUnderstand?

•  TheCPU(CentralProcessingUnit)actuallyunderstandsonlyalanguagecomposedenarelyofnumbers,likethis:–  “157655302277854”(thisisamade-upexample)–  Thismeans“executeinstrucaon#157usinganargument65530,thenexecuteinstrucaon#22,thenexecuteinstrucaon#77usinganargument854”

–  Thelanguagedefiniaontellsthemachinethatinstrucaon#157takesoneargument,but#22doesnot.

•  Itispossibleforapersontowritecodeinthismachinelanguage,butalmostnobodydoesitanymorebecauseitissoinconvenient.

Page 5: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

IActuallyDidThis

Page 6: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

ProgrammingLanguages

•  Canbebrokenintotwolargefamilies:•  Interpretedlanguages.Aninterpreterprogramtakesincommands,checksyntaxandtranslatestomachinelanguageatruname(e.g.,Matlab,UnixShell)

•  Compiledlanguages.Programsaretranslatedandsavedinmachinelanguagebyacompiler.Atrunamenoaddiaonalinterpretaaonisnecessary(e.g.,FORTRAN,C/C++).–  Thesegenerallyrunmuchfasterthaninterpretedlanguages

Page 7: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

1.  Openatexteditor(MATLABeditor,vi,notepad,TextWrangler,…notMSWord)

2.  translateyour(physicalormental)flowchartintoasetofinstrucaonsaccordingtotherulesofaprogramminglanguage

3.  testyourprogramforsyntacacalcorrectness(asktheinterpreter/compiler)4.  iferrors,fixthemandgobackto(3)5.  testyourprogramforsemanacerrors(the“fun”part!)6.  iferrors,fixthemandgobackto(3)

Now,HowDoesProgrammingWork?

Page 8: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Example:HelloWorld

Page 9: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

TheMATLABEditorHelpsYou

Page 10: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

TheMATLABEditorHelpsYou

Page 11: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

TheMATLABEditorHelpsYou

Page 12: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

WhatisaVariable?

•  DonaldKnuth:Aquanatythatmaypossessdifferentvaluesasaprogramisbeingexecuted.

•  MehranSahami:Aboxinwhichwestuffthings–i.e.aboxwithvariablecontent.

•  Wikipedia:Userdefinedkeywordthatislinkedtoavaluestoredincomputer’smemory(runame).

•  Theconceptofavariableconsistsof:– Name–  Type–  Value

Page 13: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Variables:Name•  USEMEANINGFULNAMES!•  Mustfollowprogramminglanguagerules– MATLABvariablenamesmustbeginwithaleQer,followedbyanycombinaaonofleQers,digits,andunderscores.MATLABdisanguishesbetweenuppercaseandlowercase.Noreservedkeywords!

•  USEMEANINGFULNAMES,i.e.namesthatspeak:‘lengthGlacier’or‘glacier_length’NOTNOTNOT‘a’–avoidambiguity

•  useconsistentformaqng,i.e.:‘my_cool_var’or‘myCoolVar’–thisiseasiertoread

•  agazillionstyleguidesexist–punchline:usemeaningfulnames,beconsistent(that’shardenough)!

Page 14: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Variables:Type

•  Whatisatype?–Thinkofsetsofnumbersinmath:N,R,Z,...Thetypereferstohownumbersarebeingrepresentedinacomputer’smemory,i.e.whichbithaswhichmeaning,andhowmanybitsarenecessary

•  primiave,builtintypes–forMATLABe.g.:‘int32’,‘double’,‘boolean’(importantfor*printf funcaons)

•  complex,homemadetypes–(arrays,)structs,cellarrays(Matlab),classes

Page 15: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Variables:TypeandTypeConversion

•  somelanguages,e.g.MATLAB,shells,Perlareweaklytyped:theydoautomaactypeconversions(onetypecanbetreatedasanother)–  thisisniceatfirst,occasionallythisleadstonasty/hardtofindproblems(e.g.stringinterpretedasnumber,etc.)

•  Otherlanguagesareverypickyandwilltellyouthatyoucan’taddarealnumbertoacomplexnumberwithoutexplicitlyconverang.– Why?Itcanproducemoreefficientmachinecode.–  Pickyvslooseisadesigndecision

Page 16: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Variables:Value

•  Avalueofthetypeofthevariable:42,3.1415926...,false,‘textstring’,i.e.,thethingwestuffinthebox

•  Valuescan/shouldchangeduringtherunameoftheprogram.Somelanguages(notMATLAB)allowyoutodefineanamedconstant,forvaluesthatcan’tchange.

•  Weneedtobeabletoassignvaluestovariables,andalsoaccess(dereference)thevalues.

Page 17: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

AssignmentandAccess

•  Assignment:setthevalueofavariable– MATLAB:num_glaciers = 105–  tcshscripang:set filename = “12jun30dena.dat”

•  Access:getthevalueofavariable– MATLAB:disp( num2str(num_glaciers) )–  tcshscripang:echo $filename

•  Whatdoesthisdo?(MATLAB)–  num_glaciers = num_glaciers + 1

Page 18: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

MATLABTreatsEverythingasaMatrix

•  Arraysormatricesarelists,vectors,matricesofdata(1tondimensional)

•  Thereforeinsteadofonevaluetheyholdalistofvalueslinkedtoachunkofmemory(asequenceofboxes)

•  Accessbyindexnumber:glaciers(5), cov(3,2)

•  Shellsallowonlyvectors(1-Darrays).

Page 19: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

ExampleArrays•  Anumericarray:

•  Valuescanbeamixofintegers,realandcomplexnumbers.>> foo = [1; 2; 3+i; 4]

foo = 1.0000 2.0000 3.0000 + 1.0000i 4.0000 •  Youcanbrowsethesevaluesinthevariablebrowserwithin

theMATLABGUI.

Index 1 2 3 4 5 6 7 8 9 10

Value 0 -3.2 1000 NaN 1 5 -90 9999 3.141 0

Page 20: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

ExampleArrays•  Astringarray:

•  Examplesofassigningandaccessingstrings:>> foo = ‘Hello Work’>> foo(4)ans =

‘l’ >> foo(1)ans =

‘H’ >> foo(1) + 1ans =

73

Index 1 2 3 4 5 6 7 8 9 10

Value H e l l o W o r k

Whatisgoingonhere!?

Page 21: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

Anexample

Jeff0:101:00Noway,José!

Page 22: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

HowtoMaketheTable

Page 23: Fundamental Programming Principles: Variables and …636/02_fundamentals.pdf · Fundamental Programming Principles: Variables and Data Types Beyond the Mouse ... , C/C++). – These

TheImportanceofPlayingAround•  Youwilllearnmoreifyouspendameplayingaroundwiththecomputer,tryingtomakeitdosomethinginteresangtoyou.

•  Youcanstartwiththeexercises,typingthemfromthelecturenotesorevendoingacopyandpaste–  Youdohavetowatchoutforapostrophes:thestraightapostropheandthecurlyones(‘’)areactuallydifferentcharacters!

– Wordprocessorstoday“help”youbyautomaacallymakingcurlyapostrophesandquotaaonmarksbecauseitlooksfancier.