Computer Science 1 Week 3. CSc 1, Sacramento State This Week... QBasic Programming QBasic...
-
Upload
melissa-patterson -
Category
Documents
-
view
227 -
download
2
Transcript of Computer Science 1 Week 3. CSc 1, Sacramento State This Week... QBasic Programming QBasic...
Computer Science 1Week 3
CSc 1, Sacramento State
This Week ...This Week ...
• QBasic ProgrammingQBasic Programming VariablesVariables DIMDIM LETLET
• Computer ConceptsComputer Concepts Storage of numbers and stringsStorage of numbers and strings Inside your computerInside your computer Magnetic, solid state and optical Magnetic, solid state and optical
storage devicesstorage devices
CSc 1, Sacramento State
QBasicQBasicVariablesVariables
Using Memory for DataUsing Memory for Data
CSc 1, Sacramento State
Review of Data in Review of Data in QBasicQBasic
• Numeric DataNumeric Data IntegersIntegers e.g. 1846e.g. 1846
• Text DataText Data StringString e.g. e.g. ““Sac State"Sac State"
CSc 1, Sacramento State
Functions of a Functions of a ComputerComputer
Output Data Store Data
CSc 1, Sacramento State
VariablesVariables
• Are Are namesnames used to store data in memory used to store data in memory• Found in practically all programming Found in practically all programming
languageslanguages• Specified either:Specified either:
ExplicitlyExplicitly – declared before they are used – declared before they are used ImplicitlyImplicitly – created as they are "seen" – created as they are "seen"
CSc 1, Sacramento State
Variables in QBasicVariables in QBasic
• There are several types in QBasicThere are several types in QBasic String variables hold stringsString variables hold strings Integer variables hold Integer variables hold whole numberswhole numbers Floating point variables hold Floating point variables hold real numbersreal numbers
• You will only need to use You will only need to use twotwo types in types in this classthis class
CSc 1, Sacramento State
Variables in QBasicVariables in QBasic
• Integer Integer andand Double Double data types data types used to store a numberused to store a number they have an initial value of they have an initial value of 00 defaultdefault data type data type
• StringString data type data type used to store a piece of textused to store a piece of text they are initially blank – empty stringthey are initially blank – empty string
CSc 1, Sacramento State
QBasic Variable QBasic Variable NamesNames
• Naming rulesNaming rules letter followed by a series of letters, numbers, letter followed by a series of letters, numbers,
underscores, or periodsunderscores, or periods nono spaces spaces
• They are They are notnot case sensitive case sensitive uppercase or lowercase letters doesn't matteruppercase or lowercase letters doesn't matter e.g. e.g. CComputeromputer is the same as is the same as ccomputeromputer
CSc 1, Sacramento State
SacState
Test4
TotalCost
Some Valid Variable Some Valid Variable NamesNames
CSc 1, Sacramento State
First-Name
1040Form
Test 4
Some Some BadBad Variable Variable NamesNames
Dash – Not Valid
Starts with a number
A space
CSc 1, Sacramento State
DimDim Statement Statement
• Used to Used to dimdimensionalize a variableensionalize a variable creates space for a variablecreates space for a variable this is an older termthis is an older term
• Must be done Must be done beforebefore using the variable using the variable• Dim statement defaults to a Dim statement defaults to a doubledouble
you can manually specify it if you wishyou can manually specify it if you wish for strings, you for strings, you mustmust declare the type declare the type
CSc 1, Sacramento State
DIM Name AS Type
Dim Dim Statement SyntaxStatement Syntax
Variable Name
Optional
Integer, double, string
CSc 1, Sacramento State
DIM Age
Dim Statement Dim Statement ExampleExample
Creates a numeric variable
CSc 1, Sacramento State
DIM User AS String
Dim Statement Dim Statement Example 2Example 2
Creates a String
CSc 1, Sacramento State
LetLet Statement Statement
• Used to change the value Used to change the value of a variableof a variable
• Commonly known as an Commonly known as an "assignment""assignment" statement statement
• Used a lot!Used a lot!
CSc 1, Sacramento State
LET Variable = Value
LetLet Statement Syntax Statement Syntax
Starts with LET
CSc 1, Sacramento State
What Happens?What Happens?
Memory:Memory:
DIM Year
LET Year = 1947
CSc 1, Sacramento State
What Happens?What Happens?
Memory:Memory:
DIM Year
LET Year = 1947
0
Year
CSc 1, Sacramento State
0
Year
What Happens?What Happens?
Memory:Memory:
DIM Year
LET Year = 1947
1947
CSc 1, Sacramento State
What Happens?What Happens?
Memory:Memory:
DIM Year
LET Year = 1947
1947
Year
CSc 1, Sacramento State
What Happens?What Happens?
Memory:Memory:
DIM Score
LET Score = 98
LET Score = 81
CSc 1, Sacramento State
0
Score
What Happens?What Happens?
Memory:Memory:
DIM Score
LET Score = 98
LET Score = 81
CSc 1, Sacramento State
0
Score
What Happens?What Happens?
Memory:Memory:
DIM Score
LET Score = 98
LET Score = 81
98
CSc 1, Sacramento State
98
Score
DIM Score
LET Score = 98
LET Score = 81
What Happens?What Happens?
Memory:Memory:81
CSc 1, Sacramento State
What Happens?What Happens?
Memory:Memory:
DIM Score
LET Score = 98
LET Score = 81
81
Score
CSc 1, Sacramento State
String ExampleString Example
Memory:Memory:
DIM User AS String
LET User = "Kip"
CSc 1, Sacramento State
User
DIM User AS String
LET User = "Kip"
String ExampleString Example
Memory:Memory:
CSc 1, Sacramento State
User
String ExampleString Example
Memory:Memory:Kip
DIM User AS String
LET User = "Kip"
CSc 1, Sacramento State
What Happens?What Happens?
Memory:Memory:
DIM User AS String
LET User = "Kip"
Kip
User
CSc 1, Sacramento State
DIM User AS String
LET User = "Stewie"
PRINT "Hello "; User
DIM User AS String
LET User = "Stewie"
PRINT "Hello "; User
Example ProgramExample Program
Output the variable
CSc 1, Sacramento State
Hello Stewie
Example Program Example Program OutputOutput
CSc 1, Sacramento State
DIM Title AS String
LET Title = "Sac State"
PRINT Title ; " rocks!"
Example Program 2Example Program 2
CSc 1, Sacramento State
Sac State rocks!
Example Program 2Example Program 2OutputOutput
CSc 1, Sacramento State
REM How Tall Am I?
DIM Feet AS INTEGER
LET Feet = 6
PRINT "My Height: "; Feet
Example Program 3Example Program 3
CSc 1, Sacramento State
My Height: 6
Example Program 3Example Program 3OutputOutput
CSc 1, Sacramento State
REM What happens?
DIM Grade
PRINT Grade
LET Grade = 94
REM What happens?
DIM Grade
PRINT Grade
LET Grade = 94
Example Program 4Example Program 4... tricky... tricky
CSc 1, Sacramento State
0
Example Program 3Example Program 3OutputOutput
Zero? Why?
CSc 1, Sacramento State
REM What happens?
DIM Grade
PRINT Grade
LET Grade = 94
REM What happens?
DIM Grade
PRINT Grade
LET Grade = 94
Example Program 4Example Program 4... tricky... tricky
94 hadn't been assigned yet!
CSc 1, Sacramento State
DIM Pledge
Let Pledge = "Cuppa Kappa Chino"
Example Program 5Example Program 5
This is a numeric variable
This is text!
CSc 1, Sacramento State
Example Program 5Example Program 5OutputOutput
RUNTIME ERROR: Line 3
Type Mismatch
Data was not the same type as the variable
QBasic Lab 2QBasic Lab 2
Using Variables – Using Variables – My MajorMy Major
CSc 1, Sacramento State
Lab 2: Major PainLab 2: Major Pain
• ObjectivesObjectives use variablesuse variables assign and output dataassign and output data
• Your ProgramYour Program Store your name, major, career goal, Store your name, major, career goal,
and number of semester left into and number of semester left into variablesvariables
print the variables to the screenprint the variables to the screen
CSc 1, Sacramento State
Lab RequirementsLab Requirements
• RulesRules you can help other studentsyou can help other students however, you should figure this out on your ownhowever, you should figure this out on your own
• Turn your program in to:Turn your program in to: Submit it as an Submit it as an attachmentattachment to Lab 2 in SacCT to Lab 2 in SacCT make sure your saved program file has a “.qb” extension DUE DATE = see handout.DUE DATE = see handout.
CSc 1, Sacramento State
CharacterCharacterSetsSets
How Strings are Stored How Strings are Stored
CSc 1, Sacramento State
Numbers vs. StringsNumbers vs. Strings
• Numbers and strings are represented Numbers and strings are represented differentlydifferently in your computer in your computer Numbers are represented as Numbers are represented as binary valuesbinary values Strings are represented with Strings are represented with characterscharacters
• It is often useful to convert themIt is often useful to convert them
CSc 1, Sacramento State
CharactersCharacters
• Represents textRepresents text Punctuation & symbolsPunctuation & symbols Numerals 0 – 9Numerals 0 – 9 LettersLetters
• Each has a valueEach has a value computers think in numberscomputers think in numbers characters and their matching characters and their matching
values: a values: a character setcharacter set
CSc 1, Sacramento State
ASCII ASCII ((American Standard Code for Information InterchangeAmerican Standard Code for Information Interchange) )
Character SetCharacter Set
• ASCIIASCII 7 bits – 128 characters7 bits – 128 characters uses a full byte, one bit is not useduses a full byte, one bit is not used created in the 1967created in the 1967
• Extended ASCII Extended ASCII 8 bits – 256 characters – One full byte8 bits – 256 characters – One full byte used in 1980's computersused in 1980's computers
CSc 1, Sacramento State
ASCII ChartASCII Chart
NUL SOL STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
DLE CD1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
sp ! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o
p q r s t u v w x y z { | } ~ DEL
Control characters
CSc 1, Sacramento State
Unicode Unicode Character SetCharacter Set
• Created to support Created to support everyevery spoken language spoken language• Developed in Mountain View, CaliforniaDeveloped in Mountain View, California• Originally used 16 bitsOriginally used 16 bits
that's over 65,000 characters! that's over 65,000 characters! includes every character used in the Worldincludes every character used in the World
• Expanded to 21 bitsExpanded to 21 bits 2 million characters!2 million characters! now supports every character now supports every character everever created created
CSc 1, Sacramento State
The Secret of StringsThe Secret of Strings
M 0100 1101
o 0110 1111
e 0110 0101
LET Name = "Moe"
CSc 1, Sacramento State
Number vs. String Number vs. String RepresentationRepresentation
2 0011 0010
1 0011 0001
5 0011 0101
215215 "215""215"
Three BytesThree Bytes
One ByteOne Byte
1101 0111
CSc 1, Sacramento State
Inside Your Inside Your ComputerComputer
What are Computers Made Of?What are Computers Made Of?
CSc 1, Sacramento State
Hardware CategoriesHardware Categories
• Input devicesInput devices• ProcessorProcessor• Output devicesOutput devices• Storage devicesStorage devices
CSc 1, Sacramento State
MotherboardMotherboard
• Contains:Contains: Processor chip (CPU)Processor chip (CPU) RAM - memory chipsRAM - memory chips Circuits Circuits Chips for basic input & outputChips for basic input & output Expansion slotsExpansion slots
• Connects these togetherConnects these together
CSc 1, Sacramento State
Computer ChipsComputer Chips
• Perform some processingPerform some processing integrated circuitsintegrated circuits most components inside a most components inside a
computer computer • Chips are used for:Chips are used for:
MemoryMemory ProcessorsProcessors etc...etc...
CSc 1, Sacramento State
The ProcessorThe Processor
• Performs calculations & logicPerforms calculations & logic called the "Arithmetic Logic Unit"called the "Arithmetic Logic Unit" registersregisters hold data hold data
• Controls your computerControls your computer called the "Control Unit"called the "Control Unit" talks to other componentstalks to other components talks to portstalks to ports
CSc 1, Sacramento State
The ProcessorThe Processor
• ExamplesExamples Intel Pentium Intel Pentium IBM PowerPCIBM PowerPC MOS 6502MOS 6502 ... hundreds more... hundreds more
CSc 1, Sacramento State
DDual ual IInline nline MMemory emory MModule Chipodule Chip
• Computer's main Computer's main memory memory
• Very small circuit board Very small circuit board containing chipscontaining chips
• Plugs into your Plugs into your motherboardmotherboard
CSc 1, Sacramento State
The Data BusThe Data Bus
• Electronic pathwayElectronic pathway transports datatransports data connects board componentsconnects board components
• Think of it as a "highway"Think of it as a "highway" data moves on shared pathsdata moves on shared paths otherwise, the computer would otherwise, the computer would
be be veryvery complex complex
CSc 1, Sacramento State
Memory
Inside: Inside: MotherboardMotherboard
Motherboard
Processor
CSc 1, Sacramento State
Expansion SlotsExpansion Slots
• Allow you to add additional Allow you to add additional features to your computerfeatures to your computer
• Are used for componentsAre used for components that need to transmit data very that need to transmit data very
quicklyquickly should be should be inin the system unit the system unit but but notnot part of the motherboard part of the motherboard
CSc 1, Sacramento State
Expansion SlotsExpansion Slots
• Expansion slotExpansion slot long, narrow socket on the long, narrow socket on the
system boardsystem board normally in the back of your normally in the back of your
computercomputer• Expansion cardExpansion card
small circuit boardsmall circuit board plugs into the slotplugs into the slot
CSc 1, Sacramento State
Expansion card
Expansion slots
Inside: Inside: ExpansionExpansion
CSc 1, Sacramento State
MagneticMagneticStorageStorage
Using Magnets as DataUsing Magnets as Data
CSc 1, Sacramento State
Storage BasicsStorage Basics
• Storage mediumStorage medium substance that contains datasubstance that contains data e.g. disk, tape, CD, DVD, papere.g. disk, tape, CD, DVD, paper
• Storage deviceStorage device mechanical apparatus that records mechanical apparatus that records
and / or retrieves dataand / or retrieves data e.g. disk drive, printere.g. disk drive, printer
CSc 1, Sacramento State
How Magnetic How Magnetic Storage WorksStorage Works
• Data RepresentationData Representation Microscopic particles on the Microscopic particles on the
disk disk oror tape surface store bits tape surface store bits• Data TransferData Transfer
Read-write headRead-write head – mechanism – mechanism that magnetizes the particlesthat magnetizes the particles
• Magnets cause data loss!Magnets cause data loss!
CSc 1, Sacramento State
Hard DisksHard Disks
• Magnetic MediaMagnetic Media• Hard diskHard disk platter platter
Flat, rigid disk used to store bitsFlat, rigid disk used to store bits There are multiple platters in each hard driveThere are multiple platters in each hard drive
• Head crash Head crash The The read-writeread-write head hits into a dust particle or head hits into a dust particle or
other contaminant on the diskother contaminant on the disk Head crash damages some data on diskHead crash damages some data on disk
CSc 1, Sacramento State
Inside a Hard DiskInside a Hard Disk
Read-Write Head
CSc 1, Sacramento State
Tape StorageTape Storage
• Magnetic MediaMagnetic Media• Long continuous tapeLong continuous tape
Sequential AccessSequential Access Finding data requires seekingFinding data requires seeking
• InexpensiveInexpensive Lots of storage!Lots of storage! Often used for backupsOften used for backups Primarily used in businessPrimarily used in business
CSc 1, Sacramento State
CSc 1, Sacramento State
Solid State Solid State StorageStorage
Using Chips for DataUsing Chips for Data
CSc 1, Sacramento State
Solid State StorageSolid State Storage
• Data RepresentationData Representation Stores data on a low-power chipStores data on a low-power chip Data non-volatile, erasableData non-volatile, erasable
• Data TransferData Transfer Information is simply read from the chipInformation is simply read from the chip Sometimes a Sometimes a card readercard reader is needed is needed
CSc 1, Sacramento State
Solid State StorageSolid State Storage
• AdvantagesAdvantages PortablePortable Provides faster access than magnetic or Provides faster access than magnetic or
opticaloptical Versatile – used from digital cameras to Versatile – used from digital cameras to
computerscomputers
CSc 1, Sacramento State
Solid State Solid State TechnologyTechnology
• CardsCards Compact FlashCompact Flash MMCMMC Secure DigitalSecure Digital Smart MediaSmart Media
• USB Flash DriveUSB Flash Drive Plugs into any USB portPlugs into any USB port Acts like a hard driveActs like a hard drive
CSc 1, Sacramento State
Optical StorageOptical Storage
Using Light as DataUsing Light as Data
CSc 1, Sacramento State
Optical StorageOptical Storage
• Data RepresentationData Representation disk surface stores bitsdisk surface stores bits light spots are called light spots are called landslands dark spots are called dark spots are called pitspits
• Data TransferData Transfer read using laser refraction read using laser refraction written by written by burningburning pits pits
CSc 1, Sacramento State
Optical StorageOptical Storage
• Safer than magnetic media Safer than magnetic media does not lose data over timedoes not lose data over time safe from magnetssafe from magnets resists the other elementsresists the other elements
• HoweverHowever not quite as much storagenot quite as much storage not as fastnot as fast
CSc 1, Sacramento State
CD vs. DVDCD vs. DVD
• CDCD CCompact ompact DDiskisk holds holds 700 MB700 MB of data – of data – 8080 minutes minutes
• DVDDVD DDigital igital VVersatile ersatile DDiskisk holds about holds about 4.7 GB4.7 GB of data of data double layer DVD can store double layer DVD can store 8.5 GB8.5 GB
CSc 1, Sacramento State
Hard Drive
Removable Media
Inside: Inside: Secondary StorageSecondary Storage
CSc 1, Sacramento State
Secondary Storage Secondary Storage SummarySummary
MagnetsMagnets
ChipChip
Pits/RunsPits/Runs
• Magnetic StorageMagnetic Storage
• Flash StorageFlash Storage
• Optical StorageOptical Storage