Declaring Variable Types ► Dim var As String ► Dim var As Integer ► Dim var As Single
String Variables (Contd.) Sub Command1_Click () Dim message_part1 As String, message_part2 As String, Dim message As String Picture1.Cls Let message_part1 = “hello” Let message_part2 = “students of CS101” Let message= message_part1+message_part2 Picture1.Print message Picture1.Print message + “ on Sept. 19,2002” Let message= message_part1 & “ world” Picture1.Print message End Sub
String Variables (Contd.) ► Displayed Result hellostudents of CS101 hellostudents of CS101 on Sept. 19,2002 hello world
Numeric Variable ► Example program: Sub Command1_Click () Dim interestRate As Single Dim principal As Single Dim phrase as String Picture1.Cls Let InterestRate=.0655 Let principal =100 Let phrase =“The balance after a year is” Picture1.Print phrase;(1+interestRate)*principal End Sub
► Displayed Result: The balance after a year is 106.55 Numeric Variable (Contd.)
Input/Output Using Text Box ► Contents of a text box is always a string ► Output the string by Let Text1.Text= strvar ► Input the string by Let strVar =Text1.Text
Input/Output Using Text Box (Contd.) ► Numbers in a textbox are also stored as strings ► Input a number by Let numVar = Val(Text1.Text) ► Output a number by Let Text1.Text=Str(numVar)
Postage stamp problem: How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof
Pseudocode ► Abbreviated version of actual computer code in English-like statements
Pseudo code: Postage Stamp Problem ► Program – Determine the number of stamps for a letter Read Sheets Set the number of stamps to sheets / 5 Round the number of stamps Display the number of stamps
Flowcharts ► Special geometric symbols connected by arrows
Postage stamp problem Start Read sheets Set stamps = sheets/ 5 Display stamps Round stamps up to next whole number End input processing output processing
Elements of Flowcharts Symbol Name Flowline Terminal Input/Output Processing Decision
Continued… Symbol Name Connector Off page Connector Predefined process Annotation
Hierarchy chart ► Shows overall program structure
Hierarchy Charts: Postage Stamp Problem Postage Stamp Problem Read Sheets Calculate stamps Display stamps Set stamps = sheets/5 Round stamps to next whole number
Another problem: Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westbound Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westboundNote: Even numbered street: Eastbound Odd numbered street: Westbound
Decisions ► Sequence Structure – a sequence followed without skipping any line. ► Decision Structure – a structure which requires a decision for any lines of code to be executed.
Decision Structure: Pseudocode IF condition is TRUE THEN Process step(s) 1 ELSE Process step(s) 2 Process step(s) 2 END IF
Decision Structure: Flowchart Process Step (s) 2 Is Condition True Process Step (s) 1
Decision Structure: Pseudocode What in case of multiple conditions?
Decision Structure: Pseudocode IF condition 1 is TRUE THEN Process step(s) 1 ELSE IF condition 2 is TRUE THEN Process step(s) 2 Process step(s) 2ELSE Process step(s) 3 END IF
Pseudo-Code: Street Direction Problem Get Street IF Street is EVEN Display Eastbound ELSE Display Westbound END IF
Flow Chart: Street Direction Problem End Start Get Street Is street even? Display Westbound Display Eastbound
Hierarchy Chart: Street Direction Problem Street Direction Program Get Street Number Decide whether street number is odd or even Display direction
Still Another Problem ► Calculate and report the grade-point average of a class.
The Loop Structure ► A programming structure that executes instructions many times.
Flow chart: loop structure No Process Step(s) Is condition true ? Yes
Pseudo code: loop structure ► DO WHILE condition is TRUE Process Step(s) LOOP
Draw the Flowchart for the class average problem
Pseudo code: Class Average Problem INITIALIZE Counter and Sum to 0 DO WHILE there are more data Get the next Grade Add the Grade to the Sum Increment the Counter LOOP Compute Average = Sum/Counter Display Average
Hierarchy Chart: Class Average Problem Class average program Get grade Compute sum Display average Calculate average