Introduction to Computing Dr. Nadeem A Khan. Lecture 11.
-
date post
21-Dec-2015 -
Category
Documents
-
view
222 -
download
0
Transcript of Introduction to Computing Dr. Nadeem A Khan. Lecture 11.
Introduction to Introduction to ComputingComputing
Dr. Nadeem A KhanDr. Nadeem A Khan
Lecture 11Lecture 11
Data Types !Data Types !
► For Strings:For Strings:
Variable-Length StringVariable-Length String
Fixed-Length Strings Fixed-Length Strings
Data TypesData Types
► For Numbers (Whole numbers or not):For Numbers (Whole numbers or not):
Single (Precision Floating Point)Single (Precision Floating Point)
Double (Precision Floating Point)Double (Precision Floating Point)
Currency Currency
Data Types Data Types (Contd.)(Contd.)
► For Numbers (Whole numbers only):For Numbers (Whole numbers only):
IntegerInteger
Long (Integer)Long (Integer)
Byte Byte
Data Types Data Types (Contd.)(Contd.)
► For Boolean values: For Boolean values:
BooleanBoolean
Data Types Data Types (Contd.)(Contd.)
► Use Letters, digits and underscores onlyUse Letters, digits and underscores only No predefined keywords (integer, single, Dim etc.)No predefined keywords (integer, single, Dim etc.)
► Max. length: 255 chars. (Min. length: 1 char.)Max. length: 255 chars. (Min. length: 1 char.)
► Begin with a letter (e.g. a, A)Begin with a letter (e.g. a, A)
► Case-Case-InInsensitive (Game=GAME=gAme)sensitive (Game=GAME=gAme)
Variable NamesVariable Names
►Use meaningful namesUse meaningful names
►Good to follow the naming Good to follow the naming conventionsconventions
e.g:e.g:
totaltotal
numberOfCarsnumberOfCars
Variable Names Variable Names (Contd.)(Contd.)
Converting Data Converting Data Types !Types !
►Already know:Already know:
ValVal
Str$Str$
Conversion functionsConversion functions
► Conversion FunctionsConversion Functions Converts toConverts to CboolCbool BooleanBoolean CbyteCbyte ByteByte CcurCcur CurrencyCurrency CDblCDbl DoubleDouble CintCint IntegerInteger CLngCLng LongLong CsngCsng SingleSingle CstrCstr StringString
Conversion functions Conversion functions (Contd.)(Contd.)
► Converts the input value to a specific data Converts the input value to a specific data
typetype
► Only convert those input values that are Only convert those input values that are appropriateappropriate
for that data type for that data type
Conversion functions Conversion functions (Contd.)(Contd.)
Using commas: ExampleUsing commas: Example
►Picture1.Print “North”, “to”, “the”, Picture1.Print “North”, “to”, “the”, “future”“future”
►Picture1.Print “No:”, 6, Picture1.Print “No:”, 6,
More on Print methodMore on Print method
Using commas: ResultUsing commas: Result
►North North to to the the futurefuture
►No:No: 6 6 (‘6’ displayed 1 pos. (‘6’ displayed 1 pos. further further in zone) in zone)
More on Print method More on Print method (Contd.)(Contd.)
Using commas: RulesUsing commas: Rules
►Line of a Picture Box divided in zonesLine of a Picture Box divided in zones
►A Zone: 14 positionsA Zone: 14 positions
►Print Items displayed in consecutive Print Items displayed in consecutive zones zones
More on Print methodMore on Print method
Using the Tab function: ExampleUsing the Tab function: Example
► Picture1.Print “Hello”;Tab(10); “World”; Picture1.Print “Hello”;Tab(10); “World”; Tab(20);”!”Tab(20);”!”
► Picture1.Print “No:”;Tab(5);5Picture1.Print “No:”;Tab(5);5
More on Print methodMore on Print method
Using the Tab function: ResultUsing the Tab function: Result
►HelloHello WorldWorld !! (‘World’ at 10th (‘World’ at 10th position)position)
(‘!’ at 20th position)(‘!’ at 20th position)
►No: 5 No: 5 (‘5’ at 6th position)(‘5’ at 6th position)
More on Print method More on Print method (Contd.)(Contd.)
Using the Tab functions: RulesUsing the Tab functions: Rules
► Tab(n); (where n is a positive integer)Tab(n); (where n is a positive integer)
=>Following Item (if possible) displayed =>Following Item (if possible) displayed beginning at the nth position of the line beginning at the nth position of the line
More on Print methodMore on Print method
Using the Spc function: ExampleUsing the Spc function: Example
►Picture1.Print “Hello”;Spc(3); “World”Picture1.Print “Hello”;Spc(3); “World”
►Picture1.Print “No:”;Spc(3);5 Picture1.Print “No:”;Spc(3);5
More on Print methodMore on Print method
Using the Spc function: ResultUsing the Spc function: Result
►Hello WorldHello World (“World” after 3 (“World” after 3 spaces)spaces)
►No: 5 No: 5 (“5” after 4 (“5” after 4 spaces)spaces)
More on Print method More on Print method (Contd.)(Contd.)
Using the Spc functions: RulesUsing the Spc functions: Rules
►Spc(n); (where n is a positive integer)Spc(n); (where n is a positive integer)
=>Following Item printed after n spaces =>Following Item printed after n spaces
More on Print methodMore on Print method
Output to the Printer:Output to the Printer:
►Printer.Print Printer.Print exprexpr
=>sends expression =>sends expression exprexpr to the printer to the printer
=>works the same way as =>works the same way as Picture1.PrintPicture1.Print
More on Print methodMore on Print method
Output to the Printer:Output to the Printer:
Let Printer.FontName = “Script”Let Printer.FontName = “Script”Let Printer.FontBold = TrueLet Printer.FontBold = TrueLet Printer.FontSize = 12Let Printer.FontSize = 12
=> set properties => set properties
More on Print method More on Print method (Contd.)(Contd.)
Output to the Printer:Output to the Printer:
►Printer.NewPage Printer.NewPage (new page)(new page)►Printer.EndDoc Printer.EndDoc (document (document
ended)ended)►PrintForm (performs the screen PrintForm (performs the screen
dump)dump)
More on Print method More on Print method (Contd.)(Contd.)
►MsgBox MsgBox messagemessage, , “, , “CaptionCaption””
► A Message Box will appearA Message Box will appear displaying the string displaying the string messagemessage ‘‘Caption’Caption’ will appear in the title bar will appear in the title bar
The Message Box for OutputThe Message Box for Output
►Let Let stringVarstringVar = InputBox$( = InputBox$(messagemessage))
► =>An InputBox will appear =>An InputBox will appear displaying the string displaying the string message; message; Assigns Assigns the typed user response to the typed user response to stringVarstringVar after Enter is pressed or OK is clickedafter Enter is pressed or OK is clicked
The Input Box for InputThe Input Box for Input
► Open “Open “filespecfilespec” For Input As #” For Input As #nn
Opens a file for reading and assigns the reference Opens a file for reading and assigns the reference number number nn
► Input #Input #nn, , varvar Next available item is read from the file and Next available item is read from the file and
assigned to variable assigned to variable varvar
► Close #Close #nn Closes the file Closes the file
Reading Data from File Reading Data from File
► DATA.TXT file has two lines:DATA.TXT file has two lines:““September”September”
2626
=> items separated by commas or line-breaks;=> items separated by commas or line-breaks;
data item of same type as the data to bedata item of same type as the data to be
assigned to the corresponding variableassigned to the corresponding variable
Reading Data from File Reading Data from File (Contd.)(Contd.)
Example:Example:
Sub Command1_Click()Sub Command1_Click()Dim month As String, date As SingleDim month As String, date As SinglePicture1.ClsPicture1.ClsOpen “DATA.TXT” For Input As #1Open “DATA.TXT” For Input As #1Input #1, monthInput #1, monthInput #1, dateInput #1, datePicture1.Print “ Today is ”; month; datePicture1.Print “ Today is ”; month; dateClose #1Close #1
End SubEnd Sub
Reading Data from File Reading Data from File (Contd.)(Contd.)
The result:The result:
Today is September 26 Today is September 26
Reading Data from File Reading Data from File (Contd.)(Contd.)
► Rem Rem texttext ► ……………… ……………….. ‘.. ‘texttext
e.g:e.g:
Rem Compute weekly payRem Compute weekly pay
Dim hrs as Single ‘hours worked Dim hrs as Single ‘hours worked
Inclusion of CommentsInclusion of Comments
Reading for today Reading for today
►Schneider: Section 3.5Schneider: Section 3.5